场景:
发送email
依赖:
junit junit 4.4 test org.apache.commons commons-email 1.2 compile commons-io commons-io 2.1 compile
实现:
基于commons-email
SimpleEmail:普通文本格式邮件
MultiPartEmail:附件+普通文本格式邮件
HtmlEmail:MultiPartEmail的子类,附件+HTML格式邮件
代码:
private static final Logger logger = Logger.getLogger(EmailTest.class.getName());private static final String emailServer = "smtp.163.com";private static final String emailServerUserEmail = "xxx@163.com";private static final String emailServerUserName = "xx";private static final String emailServerUserPassword = "pwd";private static final String emailEncoding = "utf-8";public void testSimpleEmail(String toEmail, String toName, String subject, String msg) { try { SimpleEmail email = new SimpleEmail(); email.setHostName(EmailTest.emailServer); email.setAuthentication(EmailTest.emailServerUserName, EmailTest.emailServerUserPassword); email.setCharset(EmailTest.emailEncoding); email.setSubject("testSimpleEmail:"+subject); email.setMsg(msg); email.setFrom(EmailTest.emailServerUserEmail); email.addTo(toEmail, toName); String messageId = email.send(); logger.info("*testSimpleEmail:" + messageId); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); }}public void testMultiPartEmail(String toEmail, String toName, String attachmentPath, String subject, String msg) { String attachmentText = FilenameUtils.getName(attachmentPath); try { EmailAttachment emailAttachment = new EmailAttachment(); emailAttachment.setPath(attachmentPath); emailAttachment.setDisposition(EmailAttachment.ATTACHMENT); emailAttachment.setDescription(attachmentText); emailAttachment.setName(MimeUtility.encodeText(attachmentText)); MultiPartEmail email = new MultiPartEmail(); email.setHostName(emailServer); email.setAuthentication(EmailTest.emailServerUserEmail, EmailTest.emailServerUserPassword); email.setCharset(emailEncoding); email.addTo(toEmail, toName); email.setFrom(EmailTest.emailServerUserEmail, EmailTest.emailServerUserName); email.setSubject("testMultiPartEmail:"+subject); email.setMsg(msg); email.attach(emailAttachment); String messageId = email.send(); logger.info("*testMultiPartEmail:" + messageId); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); }}public void testHtmlEmail(String toEmail, String toName, String attachmentPath, String subject, String msg){ String attachmentText = FilenameUtils.getName(attachmentPath); try { EmailAttachment emailAttachment = new EmailAttachment(); emailAttachment.setPath(attachmentPath); emailAttachment.setDisposition(EmailAttachment.ATTACHMENT); emailAttachment.setDescription(attachmentText); emailAttachment.setName(MimeUtility.encodeText(attachmentText)); HtmlEmail email = new HtmlEmail(); email.setHostName(emailServer); email.setAuthentication(EmailTest.emailServerUserName, EmailTest.emailServerUserPassword); email.setCharset(emailEncoding); email.addTo(toEmail); email.setFrom(EmailTest.emailServerUserEmail, EmailTest.emailServerUserName); email.setSubject("testHtmlEmail:"+subject); email.attach(emailAttachment); email.setHtmlMsg(msg); String messageId = email.send(); logger.info("*testHtmlEmail:" + messageId);} catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e);}}public static void main(String[] args) { EmailTest test = new EmailTest(); String attachmentPath = "G:/hexun.rar"; String subject = "公司通知"; String msg = "hi 好消息! 今天天气挺好的适合运动 !"; String toEmail = "toemail@163.com"; String toName = "toemail"; try { test.testHtmlEmail(toEmail, toName, attachmentPath, subject, msg); test.testMultiPartEmail(toEmail, toName, attachmentPath, subject, msg); test.testSimpleEmail(toEmail, toName, subject, msg); } catch (Exception e) { e.printStackTrace(); }}