apache commons学习系列之Email组件
来源:互联网 发布:程序员适合写那些博客 编辑:程序博客网 时间:2024/05/22 08:14
基于commons Email version 1.3
主要类:
SimpleEmail - This class is used to send basic text based emails.
MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
ImageHtmlEmail - This class is used to send HTML formatted emails with inline images. It has all of the capabilities as HtmlEmail but transform all image references to inline images.
EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
还有一个jdk中的类DefaultAuthenticator,用与用户验证
应用示例:
SimpleEmail发送普通文本邮件:
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);//使用SSL加密发送
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");//使用addTo可以添加多个目的地
email.send();
使用MultiPartEmail发送带附件的邮件:// Create the attachment EmailAttachment attachment = new EmailAttachment();//路径可以为绝对或相对地址,也可以使用setURL attachment.setPath("mypictures/john.jpg");//附件处理方式,作为附件或内嵌为内容 attachment.setDisposition(EmailAttachment.ATTACHMENT);// attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif")); 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();发送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();
也可以使用如下相对路径形式使用ImageHtml:// 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();//指定html模板中的baseUrl email.setDataSourceResolver(new DataSourceUrlResolverl(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();补充几点:
1.开启email组件的调试,以便出现问题时能够sysout相关信息。
email.setDebug(true);
2.邮件安全:
开启STARTTLS or SSL检察:
Email.setSSLCheckServerIdentity(true)
开启:STARTTLS:Email.setStartTLSRequired(true)
开启SSL连接支持email.setSSLOnConnect(true);
我的博客小站文章:
- apache commons学习系列之Email组件
- apache commons组件学习系列记录之lang3
- apache commons组件学习系列之configuration库
- apache commons组件学习之FileUpload
- 使用Apache Commons-email组件发送邮件
- 使用Apache Commons-email组件发送邮件
- 利用Apache commons-email组件发送邮件
- Apache Commons Email组件介绍使用
- apache commons学习系列记录之IO组件version2.4之二
- apache commons学习系列记录之IO组件version2.4之一
- Apache Commons Email教程
- Apache Commons Email使用
- apache-comnons系列之commons-io-2.5 org.apache.commons.io.IOUtils学习笔记
- apache-comnons系列之commons-io-2.5 org.apache.commons.io.FileUtils学习笔记
- apache-comnons系列之commons-collections4 org.apache.commons.collections4.CollectionUtils学习笔记
- apache-comnons系列之commons-lang3 学习笔记1
- apache-comnons系列之commons-codec1.11 学习笔记
- apache-comnons系列之commons-dbutils1.7 学习笔记
- 如何测试程序在DSP上的运行时间?
- PHP如何在请求完毕后继续处理耗时任务
- 为什么会有hibernate
- hibernate 的 CascadeType 属性 和 FetchType属性
- 网络编程基础(2)----------UDP客户端与服务器
- apache commons学习系列之Email组件
- 3个点的action button 不显示问题
- Linux下套接字编程简例
- Ultimaker2 3D打印机源文件在线公布
- android4.2源代码编译环境搭建
- Arduino舵机控制1
- Linux下基于EPOLL 模型,实现用户登录,客户端采用QT
- maven学习总结初步
- rpm,yum和apt使用详解