前面一直用javamail来发送,对比spring会显得臃肿复杂,利用spring只需简单的配置就可以实现发邮件的功能。
首先必须导入所必须的包activation.jar,commons-logging.jar,mail.jar,spring.jar
新建一个applicationContext .xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host">
<value>127.0.0.1</value><!-- 本地服务器 如果是其他,请填如:smtp.sohu.com-->
</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>[email protected]</value> <!-- 我这里用本地的邮箱名-->
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
</beans>
然后建立一个java文件
package org.fantlam.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class SpringMail1 {
public static void main(String args[]){
ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
SimpleMailMessage mail = new SimpleMailMessage();
//这里SimpleMailMessage只能用来发送text格式的邮件
try {
mail.setTo("[email protected]");//接收者
mail.setFrom("[email protected]");//按前面讲的,可以随便起
mail.setSubject("spring mail test!");//主题
mail.setText("springMail的简单发送测试");//邮件内容
sender.send(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这代码本身是没错的,一开始用了网易的老邮箱发送,它老给我报错
org.springframework.mail.MailSendException; nested exception details (1) are:
Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 553 You are not authorized to send mail, authentication is required
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:382)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)
at org.fantlam.spring.SpringMail1.main(SpringMail1.java:20)
然后我试了一个新邮箱,报错又不一样。
org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:395)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)
at org.fantlam.spring.SpringMail1.main(SpringMail1.java:20)
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)
... 3 more
不过这并不是代码本身的错误,是邮箱验证那些东西,后来我用了本地的服务器就可以成功发送了,这就是用spring发送的简单邮件,不用解释什么,一看就明白了。