在Spring框架的spring-context-support.jar中有对电子邮件发送功能的封装;
基于Spring开发简单省事,而且更稳定。需要mail.jar包支持
1 @Component 2 public class EmailSendService implements IEmailSendService { 3 private String host; 4 private String username; 5 private String password; 6 7 public boolean sendText(String email[], String title, String text) { 8 try { 9 JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 10 // 设定mail server 11 senderImpl.setHost(this.host); 12 13 // 建立邮件消息 14 SimpleMailMessage mailMessage = new SimpleMailMessage(); 15 // 设置收件人,寄件人 用数组发送多个邮件 16 // String[] array = new String[] 17 // {"sun111@163.com","sun222@sohu.com"}; 18 // mailMessage.setTo(array); 19 mailMessage.setTo(email); 20 mailMessage.setFrom(IDNMailHelper.toIdnAddress(this.username)); 21 mailMessage.setSubject(title); 22 mailMessage.setText(text); 23 24 senderImpl.setUsername(this.username); // 根据自己的情况,设置username 25 senderImpl.setPassword(this.password); // 根据自己的情况, 设置password 26 27 Properties prop = new Properties(); 28 prop.put("mail.smtp.auth", "true"); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 29 prop.put("mail.smtp.timeout", "25000"); 30 senderImpl.setJavaMailProperties(prop); 31 // 发送邮件 32 // senderImpl.send(mailMessage); 33 senderImpl.send(mailMessage); 34 System.out.println(" 邮件发送成功.. "); 35 return true; 36 } catch (Exception e) { 37 e.printStackTrace(); 38 return false; 39 } 40 } 41 42 public boolean sendHtml(String email[], String title, String html) { 43 try { 44 JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 45 46 // 设定mail server 47 senderImpl.setHost(this.host); 48 49 // 建立邮件消息,发送简单邮件和html邮件的区别 50 MimeMessage mailMessage = senderImpl.createMimeMessage(); 51 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); 52 53 // 设置收件人,寄件人 54 messageHelper.setTo(email); 55 messageHelper.setFrom(this.username); 56 messageHelper.setSubject(title); 57 // true 表示启动HTML格式的邮件 58 messageHelper.setText(html, true); 59 senderImpl.setUsername(this.username); // 根据自己的情况,设置username 60 senderImpl.setPassword(this.password); // 根据自己的情况, 设置password 61 Properties prop = new Properties(); 62 prop.put("mail.smtp.auth", "true"); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 63 prop.put("mail.smtp.timeout", "25000"); 64 senderImpl.setJavaMailProperties(prop); 65 // 发送邮件 66 senderImpl.send(mailMessage); 67 68 System.out.println("邮件发送成功.."); 69 return true; 70 } catch (Exception e) { 71 e.printStackTrace(); 72 return false; 73 } 74 } 75 76 public void setHost(String host) { 77 this.host = host; 78 } 79 80 public void setUsername(String username) { 81 this.username = username; 82 } 83 84 public void setPassword(String password) { 85 this.password = password; 86 } 87 88 public boolean sendText(String email, String title, String text) { 89 return this.sendText(new String[]{email}, title, text); 90 } 91 92 public boolean sendHtml(String email, String title, String html) { 93 return this.sendHtml(new String[]{email}, title, html); 94 } 95 96 }
在这里如果不注意会有一个IDN编码问题。我是经历之后在so上找到了相关的解决方法。详见以下代码
可能出现的异常:JavaMail: “Domain contains control or whitespace in string” errormessage because of domain with danish charaters
1 public class IDNMailHelper { 2 3 public static String toIdnAddress(String mail) { 4 if (mail == null) { 5 return null; 6 } 7 int idx = mail.indexOf('@'); 8 if (idx < 0) { 9 return mail; 10 } 11 return localPart(mail, idx) + "@" + IDN.toASCII(domain(mail, idx)); 12 } 13 14 private static String localPart(String mail, int idx) { 15 return mail.substring(0, idx); 16 } 17 18 private static String domain(String mail, int idx) { 19 return mail.substring(idx + 1); 20 } 21 22 }