2 minutes
Java发邮件
2012年10月12日
今天研究了邮件协议,有点懒,随便写点什么留个回忆线索引子。
// 今天研究的邮件协议,觉得内容太多,
在apache的commons项目中有个Email的项目,他封装了JAVA原生的javamail组件。让发送Email变的十分简单。
需要的包有activation、commons-email和javamail。
先说下这三个包在哪下载。
javamail包在Oracle官网(http://www.oracle.com)搜索javamail下载(这是原生javamail组件)。
commons-email包在http://commons.apache.org/email/download_email.cgi下载(这是commons封装的simpleEmail)
activation组件在Oracle官网(http://www.oracle.com)搜索JAF下载下来,里面有activation.jar文件(邮件发送时需要补充使用到的jar包)
三个都下好之后加eclipse的buildPath里添加上这三个包(就是加到CLASSPATH)。
然后新建一个java文件。
首先,这是发送一个简单的TEXT文本邮件的示例(源码自commons官网):
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");//设置收件人
email.send();//发送
这是发送带附件的邮件的示例(附件在本地)(源码自commons官网):
import org.apache.commons.mail.*;
...//引入包,eclipse会自动引包,不用管。
// 创建一个新的附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");//设置附件的文件路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);//部署附件
attachment.setDescription("Picture of John");//附件的描述
attachment.setName("John");//设置附件名
//创建一个带有附件的邮件
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");//邮件内容
//设置用户名密码啥的参照第一个示例
// 给邮件添加附件
email.attach(attachment);
// 发送
email.send();
这是发送带附件的邮件的示例(附件在网络)(会自动从网上下载文件并添加到附件)(源码自commons官网):
import org.apache.commons.mail.*;
...//引入包,eclipse会自动引包,不用管。
// 创建一个新的附件
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");//名字
//创建一个带附件的邮件
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");//内容
//设置用户名密码啥的参照第一个示例
// 添加附件到邮件
email.attach(attachment);
// 发送
email.send();
这是发送具有HTML格式的并内嵌图像的邮件(源码自commons官网):
import org.apache.commons.mail.HtmlEmail;
...
// 创建一个具有HTML格式的邮件
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");
// 嵌入图获得图像ID
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");
// HTML内容(添加图像ID)
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
// 邮件内容
email.setTextMsg("Your email client does not support HTML messages");
//用户名密码什么的参见第一例
// 发送
email.send();
这是发送具有HTML格式的并外联图像的邮件(源码自commons官网):
import org.apache.commons.mail.HtmlEmail;
...
// 载入HTML模板
String htmlEmailTemplate = ....
// 定义资源URL地址
URL url = new URL("http://www.apache.org");
// 创建电邮
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");
// 设置内容
email.setHtmlMsg(htmlEmailTemplate);
// 设置替代信息
email.setTextMsg("Your email client does not support HTML messages");
// 发送
email.send();
232 Words
2012-10-12 08:00 +0800