- 论坛徽章:
- 0
|
/* 试用 Apache Commons Email
*
* Apache Commons Email是对 JavaMail进行封装从而简化其操作,这里对参考官方示例和网友的其他一些示例
* 来进行简单的测试,熟悉下其的使用方法。主要是使用通过登录QQ邮件服务,向自己发送一封Email。前提需要先
* 登录的QQ优先将中,设置->帐户->勾上"开启POP3/SMTP服务"。以下代码已经过测试。(其他邮件服务器未测试!)
*
* 环境要求: JRE 1.4,下载 Apache Commons Email、JavaBean Activation Framework 和 JavaMail
*
* 官方示例: http://commons.apache.org/email/userguide.html
*
* */
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
public class TestEmail {
private String emailServer = "pop.qq.com"; //经测试 pop.qq.com 和 smtp.qq.com 均可使用
private String userName = "103430585"; //QQ号码
private String password = "*********"; //QQ密码
private String emailEncoding = "UTF-8"; //Email编码
public static void main(String args[]){
TestEmail test = new TestEmail();
try {
test.testSimpleEmail();
test.testMultiPartEmail();
test.testHtmlEmail();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试发送不包含附件的简单纯文本邮件
* @throws EmailException
*/
public void testSimpleEmail() throws EmailException{
SimpleEmail email = new SimpleEmail();
email.setHostName(emailServer); //服务器名
email.setAuthentication(userName, password); //用户名,密码
email.setCharset(emailEncoding); //邮件编码方式
email.addTo("103430585@qq.com","btpka3 中文"); //收信人
email.setFrom("103430585@qq.com","zll 中文"); //发信人
email.setSubject("Subjec SimpleEmail 中文"); //标题
email.setMsg("Message SimpleEmail 中文"); //正文
email.send(); //发送
}
/**
* 测试发送包含附件的邮件
* @throws UnsupportedEncodingException
* @throws EmailException
* @throws MalformedURLException
*/
public void testMultiPartEmail() throws UnsupportedEncodingException, EmailException, MalformedURLException{
//本地附件
EmailAttachment att1 = new EmailAttachment();
att1.setPath("C:/ooogo010.jpg"); //附件原始路径
att1.setDisposition(EmailAttachment.ATTACHMENT);
//是否乱码无法测试,若乱码可参考下一句解决方法
att1.setDescription("attachemnt description gril 中文"); //附件描述
//防止附件名乱码
att1.setName(MimeUtility.encodeText("gril 中文.jpg")); //附件名称
//网络附件
EmailAttachment att2 = new EmailAttachment();
att2.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
att2.setDisposition(EmailAttachment.ATTACHMENT);
att2.setDescription("attachemnt description logo 中文");
att2.setName(MimeUtility.encodeText("logo 中文.gif"));
MultiPartEmail email = new MultiPartEmail();
email.setHostName(emailServer);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo("103430585@qq.com","btpka3 中文");
email.setFrom("103430585@qq.com","zll 中文");
email.setSubject("Subjec MultiPartEmail 中文");
email.setMsg("Message MultiPartEmail 中文");
email.attach(att1); //发送
email.attach(att2);
email.send();
}
/**
* 测试发送HTML格式的邮件,经测试发现:用到的图片在QQ的邮箱界面中没有附件的图标
* @throws UnsupportedEncodingException
* @throws EmailException
* @throws MalformedURLException
*/
public void testHtmlEmail() throws UnsupportedEncodingException, EmailException, MalformedURLException{
HtmlEmail email = new HtmlEmail();
email.setHostName(emailServer);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo("103430585@qq.com","btpka3 中文");
email.setFrom("103430585@qq.com","zll 中文");
email.setSubject("Subjec HtmlEmail 中文");
//本地图片
File file = new File("C:/ooogo010.jpg");
String cid1 = email.embed(file, "gril 中文.jpg");
//网络图片
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid2 = email.embed(url, "logo 中文.gif");
email.setHtmlMsg("pretty gril - "
+ "
"
+ "The apache logo - "
+ "
");
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25218/showart_1093407.html |
|