使用commons-email-1.3.2.jar包发送邮件

来源:互联网 发布:康得新裸眼3d知乎 编辑:程序博客网 时间:2024/05/16 11:57

http://blog.csdn.net/liubenlong007/article/details/18663747

package com.lbl.email;import java.io.File;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import org.apache.commons.mail.EmailAttachment;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.HtmlEmail;import org.apache.commons.mail.MultiPartEmail;import org.apache.commons.mail.SimpleEmail;import org.junit.Test;import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility; /** * 使用commons-email-1.3.2.jar包发送邮件 * 一定要有这两个包:mail-1.4.1.jar;activation-1.1.1.jar。 * @author liubenlong * */public class TestEmail { /** * 用于发送邮件的服务器信息 */ private String emailServer = "smtp.163.com"; private String userName = "邮箱用户名"; private String password = "邮箱密码"; private String emailEncoding = "GBK";  //Email编码 /**   * 测试发送不包含附件的简单纯文本邮件   * @throws EmailException   */ @Test public void testSimpleEmail() throws EmailException{   SimpleEmail email = new SimpleEmail();   email.setHostName(emailServer);     //服务器名   email.setAuthentication(userName, password);    //用户名,密码   email.setCharset(emailEncoding);    //邮件编码方式      email.addTo("收件人邮箱","收信人名称"); //收信人   email.setFrom("发件人邮箱","发信人名称"); //发信人   email.setSubject("测试发送不包含附件的简单纯文本邮件");  //标题   email.setMsg("测试发送不包含附件的简单纯文本邮件");  //正文    email.send();         //发送 }  /**   * 测试发送包含附件的邮件   * @throws UnsupportedEncodingException   * @throws EmailException   * @throws MalformedURLException   */ @Test public void testMultiPartEmail(){   try {//本地附件   EmailAttachment att1 = new EmailAttachment();      att1.setPath("F:/表单设计器功能设计.doc");      //附件原始路径   att1.setDisposition(EmailAttachment.ATTACHMENT);   //是否乱码无法测试,若乱码可参考下一句解决方法   att1.setDescription("attachemnt description gril 中文"); //附件描述   //防止附件名乱码   att1.setName(MimeUtility.encodeText("表单设计器功能设计.doc")); //附件名称      //网络附件   EmailAttachment att2 = new EmailAttachment();   att2.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));   att2.setDisposition(EmailAttachment.ATTACHMENT);   att2.setDescription("attachemnt description logo 中文");   att2.setName(MimeUtility.encodeText("logo 中文.gif"));      MultiPartEmail  email = new MultiPartEmail();   email.setHostName(emailServer);   email.setAuthentication(userName, password);   email.setCharset(emailEncoding);      email.addTo("收件人邮箱","收信人名称"); //收信人   email.setFrom("发件人邮箱","发信人名称"); //发信人   email.setSubject("测试发送包含附件的邮件");  //标题   email.setMsg("jklhIHklhl;kkl;hikl;hl;kh看了航空路建行卡hi就");  //正文   email.attach(att1);  //发送   email.attach(att2);      email.send();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (MalformedURLException e) {e.printStackTrace();} catch (EmailException e) {e.printStackTrace();} }  /**   * 测试发送HTML格式的邮件   * @throws UnsupportedEncodingException   * @throws EmailException   * @throws MalformedURLException   */ @Test public void testHtmlEmail(){   try {HtmlEmail email = new HtmlEmail();   email.setHostName(emailServer);   email.setAuthentication(userName, password);   email.setCharset(emailEncoding);      email.addTo("收件人邮箱","收信人名称"); //收信人   email.setFrom("发件人邮箱","发信人名称"); //发信人   email.setSubject("测试发送HTML格式的邮件");  //标题      //本地图片   File file = new File("F:/20130815132916-1917359645.jpg");   String cid1 = email.embed(file, "测试邮件.pdf");   //网络图片   URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");   String cid2 = email.embed(url, "logo 中文.gif");      email.setHtmlMsg("<html>本地图片: - <img src=\"cid:"+cid1+"\"><br>The apache logo - <img src=\"cid:"+cid2+"\"></html>");     email.setTextMsg("Your email client does not support HTML messages");      email.send();} catch (MalformedURLException e) {e.printStackTrace();} catch (EmailException e) {e.printStackTrace();} } }


0 0