使用JavaMailSender 发送邮件
package com.juvenxu.mvnbook.account.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class AccountEmailServiceImpl
implements AccountEmailService
{
private JavaMailSender javaMailSender;
private String systemEmail;
public void sendMail( String to, String subject, String htmlText )
throws AccountEmailException
{
try
{
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper( msg );
msgHelper.setFrom( systemEmail );// from从哪里发,需要发送邮件的公司
msgHelper.setTo( to ); //to 目的地 ,比如客户
msgHelper.setSubject( subject );
msgHelper.setText( htmlText, true );
javaMailSender.send( msg );
}
catch ( MessagingException e )
{
throw new AccountEmailException( "Faild to send mail.", e );
}
}
public JavaMailSender getJavaMailSender()
{
return javaMailSender;
}
public void setJavaMailSender( JavaMailSender javaMailSender )
{
this.javaMailSender = javaMailSender;
}
public String getSystemEmail()
{
return systemEmail;
}
public void setSystemEmail( String systemEmail )
{
this.systemEmail = systemEmail;
}
}
如何配置javaMailSender
email.protocol=smtps //协议
email.host=smtp.163.com
email.port=465
email.username=atzhang3si@163.com //from
email.password=TKOBSJJCSHTKHRCQ //在邮箱中设置smtp协议会给你一个16位密码******
email.auth=true
email.systemEmail=675742869@qq.com //to 目的地,客户地址
手机扫一扫
移动阅读更方便
你可能感兴趣的文章