使用Commons-mail发送邮件:更合逻辑的实例

先定义mail的一个bean:
public class Mail {

private String toAddress; // 邮件接收者
private String nickname; // 收件人昵称
private String subject; // 邮件主题
private String content; // 邮件内容
private String ChartSet; //字符集
private Map<String, String> AttachmentsPath; //附件路径列表


/setter() and getter()...
}

有了bean后,发送文本形式邮件。发件人在类初始化时完成,然后接下来就可以不断发送而不需再定义。
public class TextMailSender {

private String hostname;
private String username;
private String password;
private String address;
private Boolean TLS;

public TextMailSender(String hostname, String address, String username, String password, Boolean TLS) {
this.hostname = hostname;
this.username = username;
this.password = password;
this.address = address;
this.TLS = TLS;
}

/**
* @throws EmailException
*
* **/
public void execute(Mail mail) throws EmailException{

SimpleEmail email = new SimpleEmail();
email.setTLS(TLS);
email.setHostName(hostname);
email.setAuthentication(username, password); // 用户名和密码
email.setFrom(address); // 发送地址

email.addTo(mail.getToAddress()); // 接收地址
email.setSubject(mail.getSubject()); // 邮件标题
email.setCharset(mail.getChartSet());
email.setMsg(mail.getContent()); // 邮件内容
email.send();
}


public static void main(String[] args) {
TextMailSender sender = new TextMailSender("smtp.qq.com", "cesul@qq.com", "cesul", "******", true);

Mail mail = new Mail();
mail.setToAddress("cesul@qq.com");
mail.setSubject("这又是一封测试邮件!");
mail.setContent("呵呵呵呵呵");
mail.setChartSet("utf-8");
try {
sender.execute(mail);
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}


如果需要添加(多个)附件,使用commons-mail的另一个类:
public class AttachmentSender {

private String hostname; //"SMTP服务器"
private String username;
private String password;
private String address;
private String nickname;
private Boolean TLS;

public AttachmentSender(String hostname, String address, String nickname, String username, String password, Boolean TLS) {
this.hostname = hostname;
this.nickname = nickname;
this.username = username;
this.password = password;
this.address = address;
this.TLS = TLS;
}

/**
* @throws UnsupportedEncodingException
* @throws EmailException
*
* **/
@SuppressWarnings("unchecked")
public void execute(Mail mail) throws UnsupportedEncodingException, EmailException{

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName(hostname);
email.setAuthentication(username, password);
email.setFrom(address, nickname); //可以加入发信人称呼
email.setTLS(TLS);

email.setCharset(mail.getChartSet());
email.addTo(mail.getToAddress(), mail.getNickname());
email.setSubject(mail.getSubject());
email.setMsg(mail.getContent());

EmailAttachment attachment;
Iterator<? extends Object> it = mail.getAttachmentsPath().entrySet().iterator(); //附件是多个 ,遍历
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>)it.next();
attachment = new EmailAttachment();
attachment.setPath(entry.getKey()); //键是附件路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(MimeUtility.encodeWord("附件","UTF-8",null));
attachment.setName(MimeUtility.encodeWord(entry.getValue(),"UTF-8",null)); //值是附件描述名
email.attach(attachment); // add the attachment
}
email.send(); // send the email
}


public static void main(String[] args) {

AttachmentSender sender = new AttachmentSender("smtp.qq.com", "cesul@qq.com", "陈志钊", "cesul", "******", true);

Mail mail = new Mail();
mail.setToAddress("cesul@qq.com");
mail.setNickname("你好");
mail.setSubject("Here is the picture you wanted");
mail.setContent("呵呵呵呵呵");
mail.setChartSet("utf-8");

Map<String, String> attachment = new HashMap<String, String>();
attachment.put("E:\\Photos\\2010-08-17-0.bmp", "这是你要的图片.bmp");
attachment.put("E:\\Photos\\2010-07-19-1.bmp", "这也是你要的图片.bmp"); //不要把相同路径的文件发两次
mail.setAttachmentsPath(attachment);

try {
sender.execute(mail);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}


总结:请参看各“发送类”的构造方法:-)
====================================

要用到的最新jar包下载:

* Commons-email-1.2.zip:
[url]http://commons.apache.org/email/download_email.cgi[/url]
* JavaBeans(TM) Activation Framework 1.1.1:
[url]https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SimpleBundleDownload[/url]
* JavaMail(TM) API 1.4.4 for All Supported Platforms:

[url]https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值