利用Commons-email组件发送邮件
2023-04-21 03:15:55 阅读次数:121
1.所用的jar包
Commons-email.jar 2.编写测试类(采用了功能齐全的发送方式解决了发送附件有乱码出现的问题)
package com.yun.action;
import java.io.UnsupportedEncodingException;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
public class TestEmail {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MultiPartEmail email = new MultiPartEmail();
try {
email.setCharset("UTF-8");
// smtp host
email.setHostName("smtp.163.com");
// 登陆邮件服务器的用户名和密码
email.setAuthentication("yunets@163.com", "1122");
// 发送到
email.addTo("724547564@qq.com", "刘云生");
email.setFrom("yunets@163.com", "来自刘云生");
// 抄送
email.addCc("724547564@qq.com");
// 密送
email.addBcc("724547564@qq.com");
email.setSubject("subject中文");
email.setMsg("setMsg中文");
// 添加附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:\\刘云生.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
// 防止发送的邮件乱码
try {
attachment.setName(MimeUtility.encodeText("刘云生.pdf"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
attachment.setDescription("所发送的附件");
email.attach(attachment);
//发送邮件
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/liuyunshengsir/5998394,作者:liuyunshengsir,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:aircrack-ng命令使用详解
下一篇:Elasticsearch Painless Script详解