先定义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", "[email protected]", "cesul", "******", true);
Mail mail = new Mail();
mail.setToAddress("[email protected]");
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", "[email protected]", "陈志钊", "cesul", "******", true);
Mail mail = new Mail();
mail.setToAddress("[email protected]");
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:
http://commons.apache.org/email/download_email.cgi
* JavaBeans(TM) Activation Framework 1.1.1:
https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SimpleBundleDownload
* JavaMail(TM) API 1.4.4 for All Supported Platforms:
https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter