博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java通过apache的common包发送email
阅读量:4112 次
发布时间:2019-05-25

本文共 4071 字,大约阅读时间需要 13 分钟。

场景:

发送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(); }}

 

转载地址:http://hiqsi.baihongyu.com/

你可能感兴趣的文章
codeforces 420-C. Okabe and Boxes
查看>>
数据结构--线性表的顺序表示及操作
查看>>
UVA 658 It's not a Bug, it's a Feature!
查看>>
Educational Codeforces Round 27-C. Two TVs
查看>>
UVA-1658 Admiral
查看>>
二维几何基础--向量的表示及简单运算
查看>>
向量运算-叉积,点积
查看>>
点-线,线-线
查看>>
That Nice Euler Circuit UVALive - 3263
查看>>
7-9 拯救007
查看>>
7-3 古风排版
查看>>
7-14 最小生成树的唯一性
查看>>
7-11 肿瘤诊断
查看>>
7-8 整除光棍
查看>>
7-16 喊山
查看>>
7-13 地下迷宫探索
查看>>
Tree UVA - 548
查看>>
L2-006. 树的遍历
查看>>
L2-011. 玩转二叉树
查看>>
A - Buy or Build UVA - 1151
查看>>