Java发送Email—使用org.apache.commons.mail

本文介绍了如何使用Apache Commons Email库发送Java邮件,包括设置jar包、发送普通邮件和HTML类型邮件的步骤,以及所需的SMTP服务器配置和身份验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


apache的commons项目下有个email子项目,它对JavaMail API进行了封装,用起来特变方便。下面就简单介绍。


1.    首先配置需要的jar包


  需要将mail.jar和commons-email.jar添加到我们的CLASSPATH中即可,如下图:


  

如果您的项目是gradle项目,那么配置commons-email可以更简洁了:

compile('org.apache.commons:commons-email:1.4')


2.    发送普通邮件


  1. /** 
  2.  * 用org.apache.commons.mail发送普通邮件 
  3.  *  
  4.  * @author wangzhipeng 
  5.  *  
  6.  */  
  7. public class TestCommon {  
  8.     public TestCommon() {  
  9.     }  
  10.   
  11.     public static void main(String[] args) {  
  12.         SimpleEmail email = new SimpleEmail();  
  13.         email.setHostName("smtp.qq.com");// 设置使用发电子邮件的邮件服务器,这里以qq邮箱为例(其它例如:【smtp.163.com】,【smtp.sohu.com】)  
  14.         try {  
  15.             // 收件人邮箱  
  16.             email.addTo("1115366817@qq.com");  
  17.             // 邮箱服务器身份验证  
  18.             email.setAuthentication("你的邮箱地址""你的邮箱授权码");  
  19.             // 发件人邮箱  
  20.             email.setFrom("你的邮箱地址");  
  21.             // 邮件主题  
  22.             email.setSubject("zhipeng-JavaMail");  
  23.             // 邮件内容  
  24.             email.setMsg("Kobe Bryante Never Stop Trying");  
  25.             // 发送邮件  
  26.             email.send();  
  27.         } catch (EmailException ex) {  
  28.             ex.printStackTrace();  
  29.         }  
  30.     }  
  31. }  

3.    发送HTML类型邮件


  1. /** 
  2.  * 用org.apache.commons.mail发送HTML邮件 
  3.  *  
  4.  * @author wangzhipeng 
  5.  *  
  6.  */  
  7. public class TestCommonHTML {  
  8.     public TestCommonHTML() {  
  9.     }  
  10.   
  11.     public static void main(String[] args) {  
  12.         // 不要使用SimpleEmail,会出现乱码问题  
  13.         HtmlEmail email = new HtmlEmail();  
  14.         // SimpleEmail email = new SimpleEmail();  
  15.         try {  
  16.             // 这里是SMTP发送服务器的名字:qq的如下:  
  17.             email.setHostName("smtp.qq.com");  
  18.             // 字符编码集的设置  
  19.             email.setCharset("gbk");  
  20.             // 收件人的邮箱  
  21.             email.addTo("你的邮箱地址");  
  22.             // 发送人的邮箱  
  23.             email.setFrom("379275614@qq.com""wangzhipeng");  
  24.             // 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码  
  25.             email.setAuthentication("你的邮箱地址""你的邮箱授权码");  
  26.             email.setSubject("下午3:00会议室讨论,请准时参加");  
  27.             // 要发送的信息,由于使用了HtmlEmail,可以在邮件内容中使用HTML标签  
  28.             email.setMsg("<h1 style='color:red'>下午3:00会议室讨论</h1>" + " 请准时参加!");  
  29.             // 发送  
  30.             email.send();  
  31.   
  32.             System.out.println("邮件发送成功!");  
  33.         } catch (EmailException e) {  
  34.             e.printStackTrace();  
  35.             System.out.println("邮件发送失败!");  
  36.         }  
  37.   
  38.     }  
  39. }  

结果如下:



4、发送带图片的HTML格式邮件

// load your HTML email template
  String htmlEmailTemplate = ....

  // define you base URL to resolve relative resource locations
  URL url = new URL("http://www.apache.org");

  // create the email message
  HtmlEmail email = new ImageHtmlEmail();
  email.setDataSourceResolver(new DataSourceResolverImpl(url));
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("Test email with inline image");
  
  // set the html message
  email.setHtmlMsg(htmlEmailTemplate);

  // set the alternative message
  email.setTextMsg("Your email client does not support HTML messages");

  // send the email
  email.send();

参考http://blog.youkuaiyun.com/wang379275614/article/details/46624889

http://www.open-open.com/lib/view/open1410621206648.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值