Spring整合javamail 用gmail发送邮件
副标题#e#
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;
}
}
#p#副标题#e#
发送邮件的实现类.
然后在WEB-INF下面建设一个文件messages.properties,今后修改设置信息 直接在这里修改就可以了。
host=smtp.gmail.com
[email protected]
password=xxxxxx
port=465
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
[email protected]
subject=\u6807\u9898
text=\u5185\u5BB9\u554A
applicationContext.xml
.......................
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderCon figurer">
<property name="locations">
<list>
<value>WEB-INF/mail.properties</value>
</list>
</property>
</bean>
<!-- gmail 发送邮箱的设置文件 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${host}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<property name="port">
<value>${port}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout} </prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable} </prop>
<prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class }</prop>
<prop key="mail.smtp.socketFactory.fallback">${mail.smtp.socketFactory.fa llback}</prop>
</props>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>${from}</value>
</property>
<property name="subject">
<value>${subject}</value>
</property>
<property name="text">
<value>${text}</value>
</property>
</bean>
<bean id="sendmail" class="com.cgt.itspiderman.service.email.SendManagerServiceImp">
<property name="mailSender" ref="mailSender"></property>
<property name="message" ref="mailMessage"></property>
</bean>
<!-- end -->
……………………….
#p#分页标题#e#
public static void main(String[] args) {
ApplicationContext context= null;
context = new ClassPathXmlApplicationContext ("applicationContext.xml");
SendManagerService sms= (SendManagerService) context.getBean("sendmail");
sms.sendMail("[email protected]");
}
假如是在struts2里利用的话需要在Action里加上private SendManagerService sendemail;(加上get和set),然后在spring里设置这个 Action的时候加上<property name="sendemail" ref="sendmail"></property>
然后在Action里挪用下面这一行代码就可以发送了。
sendemail.sendMail("[email protected]");
写到这里各人会问,假如需要动态的修改这个邮件的内容呢?要领很简朴
WebApplicationContext context=WebApplicationContextUtils.getRequiredWebApplicationContext (ServletActionContext.getServletContext());
((SimpleMailMessage) context.getBean ("mailMessage")).setText("我此刻修改了邮件的内容 呵呵呵");