使用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()...}
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");}}
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");}}