Skip to content

Commit

Permalink
Merge pull request #2636 from dxmaxwell/issue_2635
Browse files Browse the repository at this point in the history
Fix usage of the Apache commons SMTP client library
  • Loading branch information
shroffk authored Jul 20, 2020
2 parents f3c14fc + 35b687f commit 0f5ae62
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
******************************************************************************/
package org.csstudio.email.ui;

import java.util.logging.Logger;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;

Expand All @@ -19,6 +21,8 @@ public class Activator
/** Plugin ID defined in MANIFEST.MF */
final public static String ID = "org.csstudio.email.ui";

final private static Logger logger = Logger.getLogger(ID);

/** @return Returns an image descriptor for the image file at the given plug-in
* relative path.
* @param path The path
Expand All @@ -27,4 +31,10 @@ public static ImageDescriptor getImageDescriptor(final String path)
{
return AbstractUIPlugin.imageDescriptorFromPlugin(ID, path);
}

/** @return Logger for plugin ID */
public static Logger getLogger()
{
return logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import java.util.logging.Level;

/** Dialog for entering EMail and sending it.
* @author Kay Kasemir
Expand Down Expand Up @@ -262,7 +263,13 @@ protected void okPressed()
}
catch (Exception ex)
{
setErrorMessage(ex.getMessage());
String msg = ex.getMessage();
if (msg != null) {
setErrorMessage(msg);
} else {
setErrorMessage("Unknown error sending email");
}
Activator.getLogger().log(Level.WARNING, "Error sending email", ex);
return;
}
super.okPressed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.csstudio.email.encoder.Base64Encoder;

import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;

/** Send EMail with text or image attachments.
* <p>
Expand Down Expand Up @@ -52,18 +53,26 @@ public static boolean isEmailSupported()
public EMailSender(final String host, final String from, final String to,
final String subject) throws IOException
{
smtp = new SMTPClient(host);
smtp = new SMTPClient();
smtp.connect(host);
if (!SMTPReply.isPositiveCompletion(smtp.getReplyCode()))
{
smtp.disconnect();
throw new IOException("SMTP server refused connection: " + host);
}

smtp.login();
smtp.setSender(from);
smtp.addRecipient(to);
message = smtp.sendMessageData();

message.write("To: " + to);
message.write("From: " + from);
message.write("Subject: " + subject);
message.write("To: " + to + "\n");
message.write("From: " + from + "\n");
message.write("Subject: " + subject + "\n");

message.write("Content-Type: multipart/mixed; boundary=\"" + boundary + "\"");
message.write("Content-Type: multipart/mixed; boundary=\"" + boundary + "\"\n");
message.write("\n");
message.write("This is a multi-part message in MIME format.");
message.write("This is a multi-part message in MIME format.\n");
message.write("\n");
}

Expand All @@ -73,11 +82,11 @@ public EMailSender(final String host, final String from, final String to,
*/
public void addText(final String text) throws IOException
{
message.write("--" + boundary);
message.write("Content-Type: text/plain");
message.write("Content-Transfer-Encoding: binary");
message.write("--" + boundary + "\n");
message.write("Content-Type: text/plain\n");
message.write("Content-Transfer-Encoding: binary\n");
message.write("\n");
message.write(text);
message.write(text + "\n");
message.write("\n");
}

Expand All @@ -87,10 +96,10 @@ public void addText(final String text) throws IOException
*/
public void attachText(final String filename) throws FileNotFoundException, IOException
{
message.write("--" + boundary);
message.write("Content-Type: text/plain");
message.write("Content-Transfer-Encoding: base64");
message.write("Content-Disposition: attachment; filename=\"" + basename(filename) + "\"");
message.write("--" + boundary + "\n");
message.write("Content-Type: text/plain\n");
message.write("Content-Transfer-Encoding: base64\n");
message.write("Content-Disposition: attachment; filename=\"" + basename(filename) + "\"\n");
message.write("\n");
final Base64Encoder encoder = new Base64Encoder(message);
encoder.encode(filename);
Expand All @@ -110,10 +119,10 @@ public void attachImage(final String filename) throws FileNotFoundException, Exc
throw new Exception("Missing file ending, required to determine image type");
}
final String type = filename.substring(end + 1).toLowerCase();
message.write("--" + boundary);
message.write("Content-Type: image/" + type);
message.write("Content-Transfer-Encoding: base64");
message.write("Content-Disposition: attachment; filename=\"" + basename(filename) + "\"");
message.write("--" + boundary + "\n");
message.write("Content-Type: image/" + type + "\n");
message.write("Content-Transfer-Encoding: base64\n");
message.write("Content-Disposition: attachment; filename=\"" + basename(filename) + "\"\n");
message.write("\n");
final Base64Encoder encoder = new Base64Encoder(message);
encoder.encode(filename);
Expand All @@ -134,7 +143,10 @@ private String basename(final String filename)
*/
public void close() throws IOException
{
message.write("--" + boundary + "--");
message.write("--" + boundary + "--\n");
message.close();
smtp.completePendingCommand();
smtp.logout();
smtp.disconnect();
}
}

0 comments on commit 0f5ae62

Please sign in to comment.