Spring发送邮件

JavaMail API为Java提供了邮件发送和接受服务,支持常见的邮件协议 SMTP IMAP POP3, 

发送纯文本邮件 

Java代码  收藏代码
  1. public class Main {  
  2.   public static void main(String[] args) throws AddressException,  
  3.       MessagingException {  
  4.     // 直接使用JavaMail API:  
  5.     Properties props = new Properties();  
  6.     // 设置SMTP服务器:  
  7.     props.put("mail.smtp.host""smtp.gmail.com");  
  8.     props.put("mail.smtp.port""465");  
  9.     props.put("mail.smtp.auth""true");  
  10.     // 使用SSL安全连接:  
  11.     props.put("mail.smtp.starttls.enable""true");  
  12.     props.put("mail.smtp.socketFactory.class""javax.net.ssl.SSLSocketFactory");  
  13.     send(props, "livebookstore"// Username  
  14.         "LiVe-BoOkStOrE"// Password  
  15.         "livebookstore@gmail.com"// From  
  16.         new String[] { "livebookstore2@gmail.com" }, // To  
  17.         "A test mail"// Subject  
  18.         "测试使用JavaMail API发送邮件" // Text  
  19.     );  
  20.   }  
  21.   public static void send(Properties props, final String username,  
  22.       final String password, String from, String[] to, String subject,  
  23.       String text) throws AddressException, MessagingException {  
  24.     Session session = Session.getInstance(props, new Authenticator() {  
  25.       protected PasswordAuthentication getPasswordAuthentication() {  
  26.         return new PasswordAuthentication(username, password);  
  27.       }  
  28.     });  
  29.     session.setDebug(true);  
  30.     Message message = new MimeMessage(session);  
  31.     message.setFrom(new InternetAddress(from));  
  32.     message.setSubject(subject);  
  33.     message.setText(text);  
  34.     message.setSentDate(new Date());  
  35.     Address[] addressTo = new Address[to.length];  
  36.     for (int i = 0; i < to.length; i++) {  
  37.       addressTo[i] = new InternetAddress(to[i]);  
  38.     }  
  39.     message.setRecipients(Message.RecipientType.TO, addressTo);  
  40.     message.saveChanges();  
  41.     Transport.send(message, addressTo);  
  42.   }  
  43. }  



Spring提供了非常方便的Mail抽象层,它通过一个MailSender接口,封装了邮件发送Bean, 
SimpleMailMessage封装了纯文本的简单邮件 

把发送邮件的STMP配置注入Bean中 

Xml代码  收藏代码
  1. <!-- 配置MailSender -->  
  2. <bean id="mailSender"    
  3.   class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  4.   <property name="host" value="smtp.gmail.com" />  
  5.   <property name="port" value="465" />  
  6.   <property name="username" value="livebookstore" />  
  7.   <property name="password" value="LiVe-BoOkStOrE" />  
  8.   <property name="javaMailProperties">  
  9.     <props>  
  10.       <prop key="mail.smtp.auth">true</prop>  
  11.       <prop key="mail.smtp.starttls.enable">true</prop>  
  12.       <prop key="mail.smtp.socketFactory.class">  
  13.         javax.net.ssl.SSLSocketFactory  
  14.       </prop>  
  15.    </props>  
  16.   </property>  
  17. </bean>  



Java代码  收藏代码
  1. // 使用Spring提供的MailSender:  
  2. ApplicationContext context = new ClassPathXmlApplicationContext(  
  3.     "config.xml");  
  4. JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");  
  5. sentBySpring(mailSender);  
  6.   
  7. private static void sentBySpring(JavaMailSender mailSender) {  
  8.   // 创建一个纯文本邮件:  
  9.   SimpleMailMessage mail = new SimpleMailMessage();  
  10.   mail.setFrom("livebookstore@gmail.com");  
  11.   mail.setTo("livebookstore2@gmail.com");  
  12.   mail.setSubject("Another test mail");  
  13.   mail.setText("测试使用Spring MailSender发送邮件");  
  14.   // 发送:  
  15.   mailSender.send(mail);  
  16. }  





发送Mime邮件 
Spring提供助手类MimeMessageHepler,能方便的操作MimeMessage 
 

Java代码  收藏代码
  1. private static void sentMime(JavaMailSender mailSender)  
  2.       throws MessagingException {  
  3.     // 发送Mime邮件:  
  4.     MimeMessage mime = mailSender.createMimeMessage();  
  5.     MimeMessageHelper helper = new MimeMessageHelper(mime, true"UTF-8");  
  6.     helper.setFrom("livebookstore@gmail.com");  
  7.     helper.setTo("livebookstore2@gmail.com");  
  8.     helper.setSubject("Test mime mail");  
  9.     // 设定为HTML格式:  
  10.     helper.setText("<html><body>访问Live在线书店:<br>"  
  11.         + "<a href='http://www.livebookstore.net' target='_blank'>"  
  12.         + "<img src='cid:logo'></a></body></html>"true);  
  13.     // 添加附件嵌入HTML中显示,在img标签中用"cid:xxx",  
  14.     // 对应附件通过addInline()添加  
  15.     helper.addInline("logo"new ClassPathResource("logo.gif"));  
  16.     helper.addAttachment("freebsd.gif"new ClassPathResource("freebsd.gif"));  
  17.     // 发送:  
  18.     mailSender.send(mime);  
  19.   }  
posted @ 2012-03-03 19:15  张良  阅读(609)  评论(0编辑  收藏  举报