java Apache Commons Mail 使用记录

Apache commons mail 是对java自带的 javax.mail 模块的封装

步骤如下:
1.Maven依赖

<dependency>  
    <groupId>org.apache.commons</groupId>  
    <artifactId>commons-email</artifactId>  
    <version>1.3.3</version>  
</dependency>  

2.HtmlEmail的初始化

HtmlEmail mail = new HtmlEmail();  
mail.setHostName(hostname); // 邮件服务器域名  
mail.setSmtpPort(smtpPort); // 邮件服务器smtp协议端口  
mail.setAuthentication(username, password); // 邮箱账户  
mail.setCharset(charset); // 邮件的字符集  

mail.setSSLOnConnect(true); // 是否启用SSL  
mail.setSslSmtpPort(sslSmtpPort); // 若启用,设置smtp协议的SSL端口号  

mail.setFrom(from); // 发件人地址  
for (String to : toList) {  
    mail.addTo(to); // 收件人地址,可以设置多个  
}  

3.下边为简单的邮件发送实例
包括:发送普通邮件、发送带附件的邮件及发送内嵌图片的邮件

3.1 发生普通邮件

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

3.2 发送带附件的邮件
得用MultiPartEmail 类来给邮件添加附件。除过覆盖attach()方法来给邮件添加附件外,这个类就和SimpleEmail类差不多。对于内联或是加入附件的个数是没有限制的。但附件必须是MIME编码。

最简单的添加附件的方式是用 EmailAttachment类。

下面的例子是将图片作为邮件的附件,并发送出去。

import org.apache.commons.mail.*;
...

  // Create the attachment
  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath("mypictures/john.jpg");
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Picture of John");
  attachment.setName("John");

  // Create the email message
  MultiPartEmail email = new MultiPartEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("The picture");
  email.setMsg("Here is the picture you wanted");

  // add the attachment
  email.attach(attachment);

  // send the email
  email.send();

如果你没有本地文件,你可以用 EmailAttachment 添加任何可用的URL。当邮件发送后,文件会自动加载并加入到邮件内容。
下面的例子是将Apache logo图片发给John。

import org.apache.commons.mail.*;
...

  // Create the attachment
  EmailAttachment attachment = new EmailAttachment();
  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Apache logo");
  attachment.setName("Apache logo");

  // Create the email message
  MultiPartEmail email = new MultiPartEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("The logo");
  email.setMsg("Here is Apache's logo");

  // add the attachment
  email.attach(attachment);

  // send the email
  email.send();

3.3 发送带内嵌图片的邮件

String cid = mail.embed(picFile); // 将图片嵌入邮件中,返回cid  
String img = "<img src='cid:" + cid + "' />"; // 构造img标签,图片源为cid  
htmlMsg = htmlMsg.replace(placeHolder, img); // 替换html邮件正文中的占位符  

mail.setSubject(subject);  
mail.setHtmlMsg(htmlMsg);  
mail.send();  

3.4 发送带HTML格式的邮件

import org.apache.commons.mail.HtmlEmail;
...

  // Create the email message
  HtmlEmail email = new HtmlEmail();
  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");

  // embed the image and get the content id
  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed(url, "Apache logo");

  // set the html message
  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

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

  // send the email
  email.send();

请注意, embed方法返回的是字串。这个字串是随机生成的标识,将来用到图片标签里。

接下来没有调用HtmlEmail类的setMsg方法,因为如果HTML内容里有内联图片的话,这个方法是不能用的。这样我们可以用setHtmlMsg和setTextMsg方法。


3.5 发送带嵌入图片的HTML文本
前面讲过如何创建带嵌入图片的HTML邮件,但是你得知道用HTML邮件模板来处理图片是很麻烦的。ImageHtmlEmail类能解决这个问题,它能很方便的让你将所有外部图片转化为内联图片

import org.apache.commons.mail.HtmlEmail;
...

  // 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();

首先我们创建了带图片的HTML邮件模板。所有涉及的图片都会在当前目录里自动转化为内联图片。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值