import java.net.URL;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
public class SendTest {
public static void main(String[] args) throws Exception {
HtmlEmail email = new HtmlEmail(); //创建HTML类型邮件
email.setCharset("utf-8");
email.setHostName("smtp.163.com"); // 设定smtp服务器
email.setAuthentication("帐号","密码");
email.addTo("XXXXX@qq.com", "To"); // 设定收件人
email.setFrom("chen3969@163.com", "test");
email.setSubject("Chen Test email");
setFile(email);
email.setHtmlMsg(setImg(email));
email.send();
}
private static HtmlEmail setFile(HtmlEmail email) throws Exception{
EmailAttachment attachment= new EmailAttachment();
attachment.setURL(new URL("附件的路径-----http://localhost:8080/附件文件所在文件夹"));
attachment.setName(MimeUtility.encodeText("XXX.txt")); //附件名编码,防止乱码
attachment.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attachment);
return email;
}
private static String setImg(HtmlEmail email) throws Exception{
URL url=new URL("http://localhost:8080/文件路径.jpg");
String cid = email.embed(url, "文件名.jpg");
URL url2=new URL("http://localhost:8080/文件路径.jpg");
String cid2 = email.embed(url2, "文件名.jpg");
String content= "<html>"+
"<img src='cid:"+cid+"'/>"+ //邮件保存在邮件服务器,发送后删除本地的图片,已发送邮件依旧可以显示图片
"<img src='cid:"+cid2+"'/>"+
"<h1>是一个发送邮件的使用教程。。。。</h1>"+
"</html>";
return content;
}
}
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
public class SendTest {
public static void main(String[] args) throws Exception {
HtmlEmail email = new HtmlEmail(); //创建HTML类型邮件
email.setCharset("utf-8");
email.setHostName("smtp.163.com"); // 设定smtp服务器
email.setAuthentication("帐号","密码");
email.addTo("XXXXX@qq.com", "To"); // 设定收件人
email.setFrom("chen3969@163.com", "test");
email.setSubject("Chen Test email");
setFile(email);
email.setHtmlMsg(setImg(email));
email.send();
}
private static HtmlEmail setFile(HtmlEmail email) throws Exception{
EmailAttachment attachment= new EmailAttachment();
attachment.setURL(new URL("附件的路径-----http://localhost:8080/附件文件所在文件夹"));
attachment.setName(MimeUtility.encodeText("XXX.txt")); //附件名编码,防止乱码
attachment.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attachment);
return email;
}
private static String setImg(HtmlEmail email) throws Exception{
URL url=new URL("http://localhost:8080/文件路径.jpg");
String cid = email.embed(url, "文件名.jpg");
URL url2=new URL("http://localhost:8080/文件路径.jpg");
String cid2 = email.embed(url2, "文件名.jpg");
String content= "<html>"+
"<img src='cid:"+cid+"'/>"+ //邮件保存在邮件服务器,发送后删除本地的图片,已发送邮件依旧可以显示图片
"<img src='cid:"+cid2+"'/>"+
"<h1>是一个发送邮件的使用教程。。。。</h1>"+
"</html>";
return content;
}
}