Spring中发送邮件

时间:2022-12-30 17:57:16

 

在使用spring的邮件反送的时候,个人理解是先要有一个邮件发送器可以使用任意一个smtp进行邮件发送,使用此功能必须使用的jar文件为

spring-context.jar
  mail.jar
  spring-core.jar
  activation.jar
  commons-logging.jar

resinweblogic下启动均无异常发生但在tomcat下发生如下异常

java.lang.NoClassDefFoundError:javax/mail/MessagingException

这个问题的解决方法为将mail.jaractivation.jar放入tomcat下的lib

Spring  bean的编写:

<bean id="mailSender"

        class="org.springframework.mail.javamail.JavaMailSenderImpl"

        abstract="false" singleton="true" lazy-init="default"

        autowire="default" dependency-check="default" >

        <property name="host">

            <value>smtp.126.com</value>

        </property>

        <property name="javaMailProperties">

            <props>

                <prop key="mail.smtp.auth">true</prop>

                <prop key="mail.smtp.timeout">25000</prop>

            </props>

        </property>

        <property name="username">

            <value>username</value>

        </property>

        <property name="password">

            <value>********</value>

        </property>

    </bean>

但在后台进行注入后总是发生如下异常

2008-03-23 15:19:43,640 WARN [org.apache.struts.action.RequestProcessor] [org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)] - Unhandled Exception thrown: class org.springframework.mail.MailAuthenticationException

[15:19:43.656] javax.servlet.ServletException: org.springframework.mail.MailAuthenticationException: Authentication failed: null; nested exception is javax.mail.AuthenticationFailedException

[15:19:43.656] at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)

[15:19:43.656] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)

[15:19:43.656] at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)

[15:19:43.656] at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)

[15:19:43.656] at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)

[15:19:43.656] at javax.servlet.http.HttpServlet.service(HttpServlet.java:115)

[15:19:43.656] at javax.servlet.http.HttpServlet.service(HttpServlet.java:92)

[15:19:43.656] at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:106)

[15:19:43.656] at cn.xy.filter.Chstr.doFilter(Chstr.java:23)

[15:19:43.656] at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)

[15:19:43.656] at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:173)

[15:19:43.656] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)

[15:19:43.656] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274)

[15:19:43.656] at com.caucho.server.port.TcpConnection.run(TcpConnection.java:514)

[15:19:43.656] at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:520)

[15:19:43.656] at com.caucho.util.ThreadPool.run(ThreadPool.java:442)

[15:19:43.656] at java.lang.Thread.run(Thread.java:595)

[15:19:43.656] Caused by: org.springframework.mail.MailAuthenticationException: Authentication failed: null; nested exception is javax.mail.AuthenticationFailedException

[15:19:43.656] at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:395)

[15:19:43.656] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)

[15:19:43.656] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)

[15:19:43.656] at cn.xy.struts.action.MailtestAction.execute(MailtestAction.java:47)

[15:19:43.656] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)

[15:19:43.656] ... 15 more

[15:19:43.656] Caused by: javax.mail.AuthenticationFailedException

[15:19:43.656] at javax.mail.Service.connect(Service.java:306)

[15:19:43.656] at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)

[15:19:43.656] ... 19 more

错误原因查明为使用发送邮件的信箱为刚刚注册的126信箱,不提供使用此功能所以更换其他邮箱以后问题解决

必须注意的是你发送邮件的时候使用的from邮箱必须是XML文件中配置的邮箱否则会发生异常
简单文本邮件发送

SimpleMailMessage mail = new SimpleMailMessage();

        mail.setTo("qinjiangli0003@163.com");

        mail.setFrom("qinjiangli0003@163.com");

        mail.setSubject("这里是标题");

        mail.setText("这是里内容");

        //JavaMailSender sender = (JavaMailSender)Mailsender;

        mailSender.send(mail);

发送超文本格式的邮件(带附件)

MimeMessage mailmessage  = mailSender.createMimeMessage();

               try {

                     MimeMessageHelper messagehelper = new MimeMessageHelper(mailmessage,true);

                     messagehelper.setTo("qinjiangli0003@163.com");

                     messagehelper.setFrom("qinjiangli0003@163.com");

                     messagehelper.setSubject("图片附件邮件标题");

                     String text="<html><body><a href=/"http://www.maishebei.com/" target=/"_blank/"><img src=/"cid:logo/"/></a>";

                     text+="<br><a href=/"http://www.maishebei.com/" target=/"_blank/"><font size=/"3/">中国机床设备团够网</font></a>";

                     text+="<br>尊敬的*****用户您在机床设备团购网的新密码为:<br>********请及时更换你的密码";

                     text+="</body></html>";

                     //messagehelper.sett

                      

                     messagehelper.setText(text,true);

                     String basepath=this.getServlet().getServletContext().getRealPath("/");

                    

                     FileSystemResource img = new FileSystemResource(new File(basepath+"mainimg//logo1.gif"));

                  messagehelper.addInline("logo", img);

                  FileSystemResource file = new FileSystemResource(new File(basepath+"mainimg//gg.jpg"));

                  messagehelper.addAttachment("gg.jpg", file);

                  mailSender.send(mailmessage);

               } catch (MessagingException e) {

                     // TODO 自动生成 catch

                     e.printStackTrace();

              }

但是发送出现以下问题 即使在不转码的时候也一样出现这个乱码问题

 

MimeMessageHelper(mailmessage,true);

该为MimeMessageHelper(mailmessage,true,”gb2312”);后正常显示