一:Quick Start
通过SimpleEmail发送邮件
就如代码里字面上的意思一样简单:
1:创建以SimpleEmail对象
2:设定发送信件的smtp服务器,如果没有设定,会寻找系统变量中mail.host值。
3:设定smtp的用户和密码
4:收件人
5:发件人
6:主题
7:内容
8:发送
二:发送带附件的邮件
我们可以发送本机的附件,当然我们也可以发送非本机的附件,如果发送的是一个存在网络上的附件的url,则邮件发送的时候会自动下载,添加到附件中。
1:)发送本地附件:
2:)发送不存在本地的附件
next,添加附件到我们的邮件中
如果需要发送多个附件,只需创建多个EmailAttachement,即可
三:发送html格式的邮件
通过HtmlEmail我们可以发送Html格式的邮件:
如下:
四:最后一步
如果需要实现更复杂authenticator 你可以extends javax.mail.Authenticator ,实现你自己的东西,然后调用 Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可
这一点jakarta也做了,给我们提供了一个defaultAuthenticator
覆盖掉该方法,实现你自己的东东 o_o
通过SimpleEmail发送邮件
1
java.lang.Object
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.SimpleEmail
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.SimpleEmail
1
SimpleEmail email =
new SimpleEmail();
2 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
4 email.addTo("martin.xus@gmail.com", "martin");
5 email.setFrom("martin@4ya.cn", "martin");
6 email.setSubject("测试主题");
7 email.setMsg("这里是邮件内容");
8 email.send();
2 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
4 email.addTo("martin.xus@gmail.com", "martin");
5 email.setFrom("martin@4ya.cn", "martin");
6 email.setSubject("测试主题");
7 email.setMsg("这里是邮件内容");
8 email.send();
就如代码里字面上的意思一样简单:
1:创建以SimpleEmail对象
2:设定发送信件的smtp服务器,如果没有设定,会寻找系统变量中mail.host值。
3:设定smtp的用户和密码
4:收件人
5:发件人
6:主题
7:内容
8:发送
二:发送带附件的邮件
我们可以发送本机的附件,当然我们也可以发送非本机的附件,如果发送的是一个存在网络上的附件的url,则邮件发送的时候会自动下载,添加到附件中。
1:)发送本地附件:
1
EmailAttachment attachment =
new EmailAttachment();
2 attachment.setPath("test/test.rar");
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription("python resource");
5 attachment.setName("resource");
2 attachment.setPath("test/test.rar");
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription("python resource");
5 attachment.setName("resource");
2:)发送不存在本地的附件
1
EmailAttachment attachment =
new EmailAttachment();
2 attachment.setURL( new URL("http://www.smilinglibrary.org/sldoc/pics/index03.jpg"));
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription("微笑图书馆");
5 attachment.setName("微笑图书馆");
2 attachment.setURL( new URL("http://www.smilinglibrary.org/sldoc/pics/index03.jpg"));
3 attachment.setDisposition(EmailAttachment.ATTACHMENT);
4 attachment.setDescription("微笑图书馆");
5 attachment.setName("微笑图书馆");
next,添加附件到我们的邮件中
1
MultiPartEmail email =
new MultiPartEmail();
2 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
4 email.addTo("martin.xus@gmail.com", "martin");
5 email.setFrom("martin@4ya.cn", "martin");
6 email.setSubject("邮件主题");
7 email.setMsg("邮件内容");
8 // 添加附件
9 email.attach(attachment);
10
11 // 发送邮件
12 email.send();
2 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
4 email.addTo("martin.xus@gmail.com", "martin");
5 email.setFrom("martin@4ya.cn", "martin");
6 email.setSubject("邮件主题");
7 email.setMsg("邮件内容");
8 // 添加附件
9 email.attach(attachment);
10
11 // 发送邮件
12 email.send();
如果需要发送多个附件,只需创建多个EmailAttachement,即可
1
email.attach(attachment1)
2 email.attach(attachment2)
2 email.attach(attachment2)
三:发送html格式的邮件
通过HtmlEmail我们可以发送Html格式的邮件:
1
java.lang.Object
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.MultiPartEmail
4 org.apache.commons.mail.HtmlEmail
5
2 org.apache.commons.mail.Email
3 org.apache.commons.mail.MultiPartEmail
4 org.apache.commons.mail.HtmlEmail
5
如下:
1
//
HtmlEmail!
2 HtmlEmail email = new HtmlEmail();
3 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
5 email.addTo("martin@4ya.cn"martin");
6 email.setFrom("martin.xus@gmail.com"martin");
7 email.setSubject("主题:该邮件包括html格式内容");
8 // embed the image and get the content id
9 // 注意这里:embed 将帮助我们创建标签如:cid:xxx url
10 URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
11 String cid = email.embed(url, "Apache logo");
12
13 /** */ /**
14set the html message
15我们看到HtmlEmail extends Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码
16**/
17 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
18
19 // set the alternative message
20 email.setTextMsg("Your email client does not support HTML messages");
21
22 // set mail
23 email.send();
24
2 HtmlEmail email = new HtmlEmail();
3 email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
5 email.addTo("martin@4ya.cn"martin");
6 email.setFrom("martin.xus@gmail.com"martin");
7 email.setSubject("主题:该邮件包括html格式内容");
8 // embed the image and get the content id
9 // 注意这里:embed 将帮助我们创建标签如:cid:xxx url
10 URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
11 String cid = email.embed(url, "Apache logo");
12
13 /** */ /**
14set the html message
15我们看到HtmlEmail extends Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码
16**/
17 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
18
19 // set the alternative message
20 email.setTextMsg("Your email client does not support HTML messages");
21
22 // set mail
23 email.send();
24
四:最后一步
如果需要实现更复杂authenticator 你可以extends javax.mail.Authenticator ,实现你自己的东西,然后调用 Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可
这一点jakarta也做了,给我们提供了一个defaultAuthenticator
1
java.lang.Object
2 javax.mail.Authenticator
3 org.apache.commons.mail.DefaultAuthenticator
2 javax.mail.Authenticator
3 org.apache.commons.mail.DefaultAuthenticator
覆盖掉该方法,实现你自己的东东 o_o
1
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
转自:http://www.blogjava.net/martinx/archive/2005/09/29/14386.html
--------------------------------------------------------------------------------------------------------------------
addCc(“10792402@qq.com”):添加抄送人地址
addBcc(“10792402@qq.com”):添加密送人地址
addReplyTo(“10792402@qq.com”):添加回复人地址