Spring发送邮件_javax.mail.AuthenticationFailedException异常已解决

在Spring项目中须要增加监控功能。监控过程中发现异常时。须要邮件报警。最初选择用javamail发送,代码量比較大(相对于spring发送)。终于选择Spring邮件发送~

以下贴一下实现的代码以及注意事项;

代码结构非常easy一个java类和一个xml文件,用到三个jar包,demo下载地址http://download.csdn.net/detail/jeofey/8892859

Spring发送邮件_javax.mail.AuthenticationFailedException异常已解决_第1张图片

这里仅仅是简单的发送,假设须要发送附件或者HTML格式的邮件的话,代码在文章末尾

[java]  view plain copy
  1. package mail;  
  2. import javax.mail.MessagingException;  
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6. import org.springframework.mail.SimpleMailMessage;  
  7. import org.springframework.mail.javamail.JavaMailSender;  
  8. /** 
  9.  *  @author Owner 
  10.  *  springMail发送邮件 
  11.  *  SendMail.java 
  12.  */  
  13. public class SendMail {  
  14.     public ApplicationContext ctx = null;  
  15.     public SendMail() {  
  16.         ctx = new FileSystemXmlApplicationContext("src/mail/applicationContext-mail.xml");  
  17.     }  
  18.     /** 
  19.      * 主測试方法 
  20.      *  
  21.      * @throws MessagingException 
  22.      */  
  23.     public static void main(String[] args) {  
  24.         new SendMail().sendMail();  
  25.     }  
  26.     /** 
  27.      * 发送简单邮件 
  28.      */  
  29.     public void sendMail() {  
  30.         JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender  
  31.         SimpleMailMessage mail = new SimpleMailMessage();  
  32.         try {  
  33.             mail.setTo("[email protected]");// 接受者  
  34.             mail.setFrom("[email protected]");// 发送者  
  35.             mail.setSubject("s邮件主题");// 主题  
  36.             mail.setText("springMail 的简单发送測试");// 邮件内容  
  37.             sender.send(mail);  
  38.             System.out.println("发送完成");  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43.       
  44. }  

注意事项:在读取xml配置文件时,用的是FileSystemXmlApplicationContext实现类,在书写路径的时候不能仅仅写文件名称,虽然java文件和xml文件是在同一文件夹下。

假设收件人有多个时,mail.setTo("[email protected]")。mail.setTo("[email protected]")....

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:jee="http://www.springframework.org/schema/jee"  
  8.     xmlns:tx="http://www.springframework.org/schema/tx"  
  9.     xsi:schemaLocation="  
  10.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  12.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  13.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  
  14.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  15.           
  16.      <bean id="mailSender"  
  17.         class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  18.         <property name="host">  
  19.              <value>smtp.163.comvalue>  
  20.         property>  
  21.         <property name="javaMailProperties">  
  22.             <props>  
  23.                <prop key="mail.smtp.auth">trueprop>  
  24.                 <prop key="mail.smtp.timeout">25000prop>  
  25.                 <prop key="mail.smtp.socketFactory.fallback">falseprop>  
  26.                 <prop key="mail.smtp.starttls.enable">trueprop>  
  27.                   
  28.         property>  
  29.         <property name="password">  
  30.             <value>*****value>   
  31.         property>  
  32.          <property name="port">  
  33.         <value>25value>  
  34.         property>  
  35.           
  36.     bean>  
  37.     beans>  

注意问题:username须要书写正确,必须是发送邮件中设定好的username,假设没设置。默认是邮件地址。当初这里没写正确,验证时出现了异常;

[plain]  view plain copy
  1. 七月 12, 2015 8:02:57 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. 信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@16d835f: display name [org.springframework.context.support.FileSystemXmlApplicationContext@16d835f]; startup date [Sun Jul 12 20:02:57 CST 2015]; root of context hierarchy  
  3. 七月 12, 2015 8:02:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. 信息: Loading XML bean definitions from file [D:\liu\workspace\myspmain\src\mail\applicationContext-mail.xml]  
  5. 七月 12, 2015 8:02:58 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
  6. 信息: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@16d835f]: org.springframework.beans.factory.support.DefaultListableBeanFactory@13911fe  
  7. 七月 12, 2015 8:02:58 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  8. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@13911fe: defining beans [mailSender]; root of factory hierarchy  
  9. org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException  
  10.     at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:416)  
  11.     at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:308)  
  12.     at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)  
  13.     at mail.SendMail.sendMail(SendMail.java:41)  
  14.     at mail.SendMail.main(SendMail.java:28)  
  15. Caused by: javax.mail.AuthenticationFailedException  
  16.     at javax.mail.Service.connect(Service.java:306)  
  17.     at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)  
  18.     ... 4 more  
查看发件箱的username


发件结果:

Spring发送邮件_javax.mail.AuthenticationFailedException异常已解决_第2张图片

demo下载地址


发送html格式和发送附件的代码

[java]  view plain copy
  1. /** 
  2.      * 发送带有正文是html的邮件 
  3.      *  
  4.      * @throws MessagingException 
  5.      */  
  6.     public void sendHTML() throws MessagingException {  
  7.         JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender  
  8.                                                                             // bean  
  9.         MimeMessage mm = sender.createMimeMessage();  
  10.         MimeMessageHelper helper = new MimeMessageHelper(mm, true"utf-8");  
  11.         try {  
  12.             helper.setTo("[email protected]");// 接受者  
  13.             helper.setFrom("[email protected]");// 发送者  
  14.             helper.setSubject("邮件主题");// 主题  
  15.             helper.setText("

    hello!!beckham

    "
    ,true);// 第二个參数代表发送的是正文是html  
  16.             sender.send(mm);  
  17.             System.out.println("发送完成");  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.     /** 
  23.      * 发送带有附件的邮件 
  24.      *  
  25.      * @throws MessagingException 
  26.      */  
  27.     public void sendFileMail() throws MessagingException {  
  28.         JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender  
  29.                                                                             // bean  
  30.         MimeMessage mm = sender.createMimeMessage();  
  31.         MimeMessageHelper helper = new MimeMessageHelper(mm, true"utf-8");  
  32.         try {  
  33.             helper.setTo("[email protected]");// 接受者  
  34.             helper.setFrom("[email protected]");// 发送者  
  35.             helper.setSubject("邮件主题");// 主题  
  36.             helper.setText("邮件内容");// 邮件内容  
  37.               
  38.             //多个附件文件  
  39.             ClassPathResource in = new ClassPathResource("附件.txt");  
  40.             ClassPathResource in2 = new ClassPathResource("struts.xml");  
  41.               
  42.             //MimeUtility.encodeWord()解决附件的文件名称为中文问题  
  43.             helper.addAttachment(MimeUtility.encodeWord(in.getFilename()), in);  
  44.             helper.addAttachment(MimeUtility.encodeWord(in2.getFilename()), in2);  
  45.               
  46.             sender.send(mm);  
  47.             System.out.println("发送完成");  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.     }  



你可能感兴趣的:(Spring发送邮件_javax.mail.AuthenticationFailedException异常已解决)