java Apache Commons Mail 使用记录
2017-10-18 19:54
453 查看
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邮件模板。所有涉及的图片都会在当前目录里自动转化为内联图片。
相关文章推荐
- 地址主题java项目中添加邮件发送功能(使用apache commons mail开源项目)
- java 发送邮件 例子 (带附件的) 使用org.apache.commons.mail
- Java发送Email—使用org.apache.commons.mail
- java项目中添加邮件发送功能(使用apache commons mail开源项目)
- java 发邮件 例子(HTML内容) 使用org.apache.commons.mail
- Java发送Email—使用org.apache.commons.mail
- java 发送邮件 例子 (带附件的) 使用org.apache.commons.mail
- [Java] 使用 Apache的 Commons-net库 实现FTP操作
- Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用
- 使用org.apache.commons.mail和javax.mail发送邮件
- java 发邮件 用org.apache.commons.mail (jar包为commons-email-1.2.jar,mail.jar)
- java使用apache commons codec来进行Base64加密解密
- 异常记录与处理 - java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
- JAVA 命令行参数解析,org.apache.commons.cli的使用
- 使用org.apache.commons.mail发送邮件
- 使用org.apache.commons.mail 发送邮件
- 使用org.apache.commons.mail 发送邮件(转)
- http://commons.apache.org/email/userguide.html 关于javamail
- 使用apache.commons.betwixt实现XML与Java对象互转
- java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案