首先是发邮件的程序
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.SimpleMailMessage;
public class Email {
private JavaMailSender sender;
//private String smtpHost;
private String from;
public void sendPasswdMail(String email,
String username, String password)
{
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设定收件人、寄件人、主题与内文
mailMessage.setTo(email);
mailMessage.setFrom(getFrom());
mailMessage.setSubject("您的密码是");
mailMessage.setText("你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好"+username+"您好,您的密码是"+password);
// 传送邮件
sender.send(mailMessage);
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
然后在application.xml中写入
<bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
//注意下面三行.表明邮箱需要SMTP验证
<property name="host" value="smtp.163.com"></property>
<property name="username" value="abc"></property>
<property name="password" value="abc"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!-- 要设定您的 Smtp Server 与寄件人 -->
<bean id="simpleMail"
class="liao.util.Email">
<property name="sender" ref="sender"/>
<property name="from">
<value>abc@tom.com</value>
</property>
</bean>