Spring整合javamail 用gmail发送邮件[Java编程]
本文“Spring整合javamail 用gmail发送邮件[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
Spring整合javamail成功之后,把大量的代码配置到spring的配置文件里了 ,从而降低了Javamail对邮箱、端口、协议、主题、内容…… 的耦合.在客户端上只需求调用一行代码便可以发送邮件了.
首先不需求往项目的lib里导入jar,但是需求在tomcat的lib里导入3个jar文 件activation.jar ,jspsmartupload.jar,mail.jar 导入这3个jar文件,这样 tomcat就不会报错了.
SendManagerService .java
package com.cgt.itspiderman.service.email;
public interface SendManagerService {
public void sendMail(String email);
}
定义一个发送邮件的接口.
SendManagerServiceImp.java
package com.cgt.itspiderman.service.email;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
public class SendManagerServiceImp implements SendManagerService {
private JavaMailSender mailSender;
private SimpleMailMessage message;
public void sendMail(final String email) {
// TODO Auto-generated method stub
MimeMessagePreparator preparator = new MimeMessagePreparator()
{
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient (Message.RecipientType.TO, new InternetAddress(email));
mimeMessage.setFrom(new InternetAddress(message.getFrom()));
mimeMessage.setSubject (message.getSubject(),"gbk");
mimeMessage.setText(message.getText (),"gbk");
}
};
mailSender.send(preparator);
}
public JavaMailSender getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public SimpleMailMessage getMessage() {
return message;
}
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
}
以上是“Spring整合javamail 用gmail发送邮件[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |