spring+velocity+javamail发送模版邮件

时间:2022-11-24 16:19:18
有时候当我们注册完的时候系统会提示有一封邮件发送到了你的邮箱,如何做呢? 
就要用到spring+velocity+javamail这3个玩意,其实只用了spring,因为其他2个都被spring集成了。 
先看代码   1spring+velocity+javamail发送模版邮件package com.worthtech.app.util;
  2spring+velocity+javamail发送模版邮件
  3spring+velocity+javamail发送模版邮件import java.io.File;
  4spring+velocity+javamail发送模版邮件import java.util.Map;
  5spring+velocity+javamail发送模版邮件
  6spring+velocity+javamail发送模版邮件import javax.mail.MessagingException;
  7spring+velocity+javamail发送模版邮件import javax.mail.internet.MimeMessage;
  8spring+velocity+javamail发送模版邮件
  9spring+velocity+javamail发送模版邮件import org.apache.velocity.app.VelocityEngine;
 10spring+velocity+javamail发送模版邮件import org.springframework.core.io.FileSystemResource;
 11spring+velocity+javamail发送模版邮件import org.springframework.mail.SimpleMailMessage;
 12spring+velocity+javamail发送模版邮件import org.springframework.mail.javamail.JavaMailSender;
 13spring+velocity+javamail发送模版邮件import org.springframework.mail.javamail.MimeMessageHelper;
 14spring+velocity+javamail发送模版邮件import org.springframework.ui.velocity.VelocityEngineUtils;
 15spring+velocity+javamail发送模版邮件
 16spring+velocity+javamail发送模版邮件public class MailSenderService {
 17spring+velocity+javamail发送模版邮件    private JavaMailSender mailSender;//spring配置中定义
 18spring+velocity+javamail发送模版邮件    private VelocityEngine velocityEngine;//spring配置中定义
 19spring+velocity+javamail发送模版邮件    private SimpleMailMessage simpleMailMessage;//spring配置中定义
 20spring+velocity+javamail发送模版邮件    private String from;
 21spring+velocity+javamail发送模版邮件    private String to;
 22spring+velocity+javamail发送模版邮件    private String subject;
 23spring+velocity+javamail发送模版邮件    private String content;
 24spring+velocity+javamail发送模版邮件    private String templateName;
 25spring+velocity+javamail发送模版邮件    // 是否需要身份验证   
 26spring+velocity+javamail发送模版邮件    private boolean validate = false
 27spring+velocity+javamail发送模版邮件    
 28spring+velocity+javamail发送模版邮件    
 29spring+velocity+javamail发送模版邮件    public JavaMailSender getMailSender() {
 30spring+velocity+javamail发送模版邮件        return mailSender;
 31spring+velocity+javamail发送模版邮件    }

 32spring+velocity+javamail发送模版邮件    public void setMailSender(JavaMailSender mailSender) {
 33spring+velocity+javamail发送模版邮件        this.mailSender = mailSender;
 34spring+velocity+javamail发送模版邮件    }

 35spring+velocity+javamail发送模版邮件    public VelocityEngine getVelocityEngine() {
 36spring+velocity+javamail发送模版邮件        return velocityEngine;
 37spring+velocity+javamail发送模版邮件    }

 38spring+velocity+javamail发送模版邮件    public void setVelocityEngine(VelocityEngine velocityEngine) {
 39spring+velocity+javamail发送模版邮件        this.velocityEngine = velocityEngine;
 40spring+velocity+javamail发送模版邮件    }

 41spring+velocity+javamail发送模版邮件    /**
 42spring+velocity+javamail发送模版邮件     * 发送模板邮件
 43spring+velocity+javamail发送模版邮件     *
 44spring+velocity+javamail发送模版邮件     */

 45spring+velocity+javamail发送模版邮件    public void sendWithTemplate(Map model){
 46spring+velocity+javamail发送模版邮件        mailSender = this.getMailSender();
 47spring+velocity+javamail发送模版邮件        simpleMailMessage.setTo(this.getTo()); //接收人  
 48spring+velocity+javamail发送模版邮件        simpleMailMessage.setFrom(simpleMailMessage.getFrom()); //发送人,从配置文件中取得
 49spring+velocity+javamail发送模版邮件        simpleMailMessage.setSubject(this.getSubject());
 50spring+velocity+javamail发送模版邮件        String result = null;
 51spring+velocity+javamail发送模版邮件        try {
 52spring+velocity+javamail发送模版邮件            result = VelocityEngineUtils.mergeTemplateIntoString(this.getVelocityEngine(), this.getTemplateName(), "UTF-8",model);
 53spring+velocity+javamail发送模版邮件        }
 catch (Exception e) {}
 54spring+velocity+javamail发送模版邮件        simpleMailMessage.setText(result);
 55spring+velocity+javamail发送模版邮件        mailSender.send(simpleMailMessage);
 56spring+velocity+javamail发送模版邮件    }

 57spring+velocity+javamail发送模版邮件    /**
 58spring+velocity+javamail发送模版邮件     * 发送普通文本邮件
 59spring+velocity+javamail发送模版邮件     *
 60spring+velocity+javamail发送模版邮件     */

 61spring+velocity+javamail发送模版邮件    public void sendText(){
 62spring+velocity+javamail发送模版邮件        mailSender = this.getMailSender();
 63spring+velocity+javamail发送模版邮件        simpleMailMessage.setTo(this.getTo()); //接收人  
 64spring+velocity+javamail发送模版邮件        simpleMailMessage.setFrom(simpleMailMessage.getFrom()); //发送人,从配置文件中取得
 65spring+velocity+javamail发送模版邮件        simpleMailMessage.setSubject(this.getSubject());
 66spring+velocity+javamail发送模版邮件        simpleMailMessage.setText(this.getContent());
 67spring+velocity+javamail发送模版邮件        mailSender.send(simpleMailMessage);
 68spring+velocity+javamail发送模版邮件    }

 69spring+velocity+javamail发送模版邮件    /**
 70spring+velocity+javamail发送模版邮件     * 发送普通Html邮件
 71spring+velocity+javamail发送模版邮件     *
 72spring+velocity+javamail发送模版邮件     */

 73spring+velocity+javamail发送模版邮件    public void sendHtml(){
 74spring+velocity+javamail发送模版邮件        mailSender = this.getMailSender();
 75spring+velocity+javamail发送模版邮件        MimeMessage mimeMessage = mailSender.createMimeMessage();
 76spring+velocity+javamail发送模版邮件        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
 77spring+velocity+javamail发送模版邮件        try {
 78spring+velocity+javamail发送模版邮件            messageHelper.setTo(this.getTo());
 79spring+velocity+javamail发送模版邮件            messageHelper.setFrom(simpleMailMessage.getFrom());
 80spring+velocity+javamail发送模版邮件            messageHelper.setSubject(this.getSubject());
 81spring+velocity+javamail发送模版邮件            messageHelper.setText(this.getContent(),true);      
 82spring+velocity+javamail发送模版邮件        }
 catch (MessagingException e) {
 83spring+velocity+javamail发送模版邮件            // TODO Auto-generated catch block
 84spring+velocity+javamail发送模版邮件            e.printStackTrace();
 85spring+velocity+javamail发送模版邮件        }

 86spring+velocity+javamail发送模版邮件        mailSender.send(mimeMessage);
 87spring+velocity+javamail发送模版邮件    }

 88spring+velocity+javamail发送模版邮件    /**
 89spring+velocity+javamail发送模版邮件     * 发送普通带一张图片的Html邮件
 90spring+velocity+javamail发送模版邮件     *
 91spring+velocity+javamail发送模版邮件     */

 92spring+velocity+javamail发送模版邮件    public void sendHtmlWithImage(String imagePath){
 93spring+velocity+javamail发送模版邮件        mailSender = this.getMailSender();
 94spring+velocity+javamail发送模版邮件        MimeMessage mimeMessage = mailSender.createMimeMessage();
 95spring+velocity+javamail发送模版邮件        try {
 96spring+velocity+javamail发送模版邮件            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
 97spring+velocity+javamail发送模版邮件            messageHelper.setTo(this.getTo());
 98spring+velocity+javamail发送模版邮件            messageHelper.setFrom(simpleMailMessage.getFrom());
 99spring+velocity+javamail发送模版邮件            messageHelper.setSubject(this.getSubject());
100spring+velocity+javamail发送模版邮件            messageHelper.setText(this.getContent(),true);
101spring+velocity+javamail发送模版邮件//            Content="<html><head></head><body><img src=\"cid:image\"/></body></html>";
102spring+velocity+javamail发送模版邮件            //图片必须这样子:<img src='cid:image'/>
103spring+velocity+javamail发送模版邮件            FileSystemResource img = new FileSystemResource(new File(imagePath));
104spring+velocity+javamail发送模版邮件            messageHelper.addInline("image",img);
105spring+velocity+javamail发送模版邮件        }
 catch (MessagingException e) {
106spring+velocity+javamail发送模版邮件            // TODO Auto-generated catch block
107spring+velocity+javamail发送模版邮件            e.printStackTrace();
108spring+velocity+javamail发送模版邮件        }

109spring+velocity+javamail发送模版邮件        mailSender.send(mimeMessage);
110spring+velocity+javamail发送模版邮件    }

111spring+velocity+javamail发送模版邮件    /**
112spring+velocity+javamail发送模版邮件     * 发送普通带附件的Html邮件
113spring+velocity+javamail发送模版邮件     *
114spring+velocity+javamail发送模版邮件     */

115spring+velocity+javamail发送模版邮件    public void sendHtmlWithAttachment(String filePath){
116spring+velocity+javamail发送模版邮件        mailSender = this.getMailSender();
117spring+velocity+javamail发送模版邮件        MimeMessage mimeMessage = mailSender.createMimeMessage();
118spring+velocity+javamail发送模版邮件        try {
119spring+velocity+javamail发送模版邮件            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
120spring+velocity+javamail发送模版邮件            messageHelper.setTo(this.getTo());
121spring+velocity+javamail发送模版邮件            messageHelper.setFrom(simpleMailMessage.getFrom());
122spring+velocity+javamail发送模版邮件            messageHelper.setSubject(this.getSubject());
123spring+velocity+javamail发送模版邮件            messageHelper.setText(this.getContent(),true);
124spring+velocity+javamail发送模版邮件            FileSystemResource file = new FileSystemResource(new File(filePath));
125spring+velocity+javamail发送模版邮件//            System.out.println("file.getFilename==="+file.getFilename());
126spring+velocity+javamail发送模版邮件            messageHelper.addAttachment(file.getFilename(),file);
127spring+velocity+javamail发送模版邮件        }
 catch (MessagingException e) {
128spring+velocity+javamail发送模版邮件            // TODO Auto-generated catch block
129spring+velocity+javamail发送模版邮件            e.printStackTrace();
130spring+velocity+javamail发送模版邮件        }

131spring+velocity+javamail发送模版邮件        mailSender.send(mimeMessage);
132spring+velocity+javamail发送模版邮件    }

133spring+velocity+javamail发送模版邮件    public String getFrom() {
134spring+velocity+javamail发送模版邮件        return from;
135spring+velocity+javamail发送模版邮件    }

136spring+velocity+javamail发送模版邮件    public void setFrom(String from) {
137spring+velocity+javamail发送模版邮件        this.from = from;
138spring+velocity+javamail发送模版邮件    }

139spring+velocity+javamail发送模版邮件    public String getSubject() {
140spring+velocity+javamail发送模版邮件        return subject;
141spring+velocity+javamail发送模版邮件    }

142spring+velocity+javamail发送模版邮件    public void setSubject(String subject) {
143spring+velocity+javamail发送模版邮件        this.subject = subject;
144spring+velocity+javamail发送模版邮件    }

145spring+velocity+javamail发送模版邮件    public String getTo() {
146spring+velocity+javamail发送模版邮件        return to;
147spring+velocity+javamail发送模版邮件    }

148spring+velocity+javamail发送模版邮件    public void setTo(String to) {
149spring+velocity+javamail发送模版邮件        this.to = to;
150spring+velocity+javamail发送模版邮件    }

151spring+velocity+javamail发送模版邮件    public String getTemplateName() {
152spring+velocity+javamail发送模版邮件        return templateName;
153spring+velocity+javamail发送模版邮件    }

154spring+velocity+javamail发送模版邮件    public void setTemplateName(String templateName) {
155spring+velocity+javamail发送模版邮件        this.templateName = templateName;
156spring+velocity+javamail发送模版邮件    }

157spring+velocity+javamail发送模版邮件    public String getContent() {
158spring+velocity+javamail发送模版邮件        return content;
159spring+velocity+javamail发送模版邮件    }

160spring+velocity+javamail发送模版邮件    public void setContent(String content) {
161spring+velocity+javamail发送模版邮件        this.content = content;
162spring+velocity+javamail发送模版邮件    }

163spring+velocity+javamail发送模版邮件    public boolean isValidate() {
164spring+velocity+javamail发送模版邮件        return validate;
165spring+velocity+javamail发送模版邮件    }

166spring+velocity+javamail发送模版邮件    public void setValidate(boolean validate) {
167spring+velocity+javamail发送模版邮件        this.validate = validate;
168spring+velocity+javamail发送模版邮件    }

169spring+velocity+javamail发送模版邮件    public SimpleMailMessage getSimpleMailMessage() {
170spring+velocity+javamail发送模版邮件        return simpleMailMessage;
171spring+velocity+javamail发送模版邮件    }

172spring+velocity+javamail发送模版邮件    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
173spring+velocity+javamail发送模版邮件        this.simpleMailMessage = simpleMailMessage;
174spring+velocity+javamail发送模版邮件    }

175spring+velocity+javamail发送模版邮件}
上面这个类实际上是个发送邮件的工具类,我们只要设置好参数后调用想要的sendXXX()就可以了。如下:

 1spring+velocity+javamail发送模版邮件//注册完马上发送邮件
 2spring+velocity+javamail发送模版邮件MailSenderService mailSender=this.getMailsenderService();
 3spring+velocity+javamail发送模版邮件mailSender.setTo(form.getEmail());
 4spring+velocity+javamail发送模版邮件mailSender.setSubject("来自worthtech的邮件");
 5spring+velocity+javamail发送模版邮件mailSender.setTemplateName("mail.vm");//设置的邮件模板
 6spring+velocity+javamail发送模版邮件Map model=new HashMap();
 7spring+velocity+javamail发送模版邮件model.put("username", form.getUserName());
 8spring+velocity+javamail发送模版邮件String url=request.getRequestURL().toString();
 9spring+velocity+javamail发送模版邮件url=url.substring(0, url.lastIndexOf("/"));
10spring+velocity+javamail发送模版邮件model.put("url", url);
11spring+velocity+javamail发送模版邮件model.put("email""gjni@worthtech.net");
12spring+velocity+javamail发送模版邮件mailSender.sendWithTemplate(model);
13spring+velocity+javamail发送模版邮件System.out.println("邮件发送成功!");

下面的是spring配置 

 1spring+velocity+javamail发送模版邮件<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
 2spring+velocity+javamail发送模版邮件       <property name="host" value="${mail.host}"/>
 3spring+velocity+javamail发送模版邮件       <property name="username" value="${mail.username}"/>
 4spring+velocity+javamail发送模版邮件       <property name="password" value="${mail.password}"/>
 5spring+velocity+javamail发送模版邮件        <property name="defaultEncoding" value="UTF-8"></property>
 6spring+velocity+javamail发送模版邮件        <property name="javaMailProperties">
 7spring+velocity+javamail发送模版邮件             <props>
 8spring+velocity+javamail发送模版邮件                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
 9spring+velocity+javamail发送模版邮件                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
10spring+velocity+javamail发送模版邮件                <!--
11spring+velocity+javamail发送模版邮件                <prop key="mail.smtp.starttls.enable">true</prop>
12spring+velocity+javamail发送模版邮件                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
13spring+velocity+javamail发送模版邮件                <prop key="mail.smtp.socketFactory.fallback">false</prop>
14spring+velocity+javamail发送模版邮件                -->
15spring+velocity+javamail发送模版邮件            </props>
16spring+velocity+javamail发送模版邮件        </property>
17spring+velocity+javamail发送模版邮件  </bean>
18spring+velocity+javamail发送模版邮件  <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage" singleton="false">
19spring+velocity+javamail发送模版邮件    <property name="from">
20spring+velocity+javamail发送模版邮件        <value>${mail.default.from}</value>
21spring+velocity+javamail发送模版邮件    </property>
22spring+velocity+javamail发送模版邮件  </bean>
23spring+velocity+javamail发送模版邮件  <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
24spring+velocity+javamail发送模版邮件        <property name="velocityProperties">   
25spring+velocity+javamail发送模版邮件            <props>   
26spring+velocity+javamail发送模版邮件                <prop key="resource.loader">class</prop>   
27spring+velocity+javamail发送模版邮件                <prop key="class.resource.loader.class">   
28spring+velocity+javamail发送模版邮件                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader   
29spring+velocity+javamail发送模版邮件                </prop>   
30spring+velocity+javamail发送模版邮件                <prop key="velocimacro.library"></prop>   
31spring+velocity+javamail发送模版邮件            </props>   
32spring+velocity+javamail发送模版邮件        </property>   
33spring+velocity+javamail发送模版邮件   </bean>
34spring+velocity+javamail发送模版邮件    <bean id="mailsenderService" class="com.worthtech.app.util.MailSenderService">  
35spring+velocity+javamail发送模版邮件          <property name="mailSender" ref="mailSender" />
36spring+velocity+javamail发送模版邮件          <property name="simpleMailMessage" ref="simpleMailMessage" />
37spring+velocity+javamail发送模版邮件          <property name="velocityEngine" ref="velocityEngine" />
38spring+velocity+javamail发送模版邮件    </bean>

mail.vm内容如下:
 1spring+velocity+javamail发送模版邮件亲爱的${username}
 2spring+velocity+javamail发送模版邮件
 3spring+velocity+javamail发送模版邮件欢迎加入worthtech! 
 4spring+velocity+javamail发送模版邮件
 5spring+velocity+javamail发送模版邮件当您收到这封信的时候,您已经可以正常登录了。
 6spring+velocity+javamail发送模版邮件请点击链接登录首页:${url}
 7spring+velocity+javamail发送模版邮件如果您的email程序不支持链接点击,请将上面的地址拷贝至您的浏览器(如IE)的地址栏进入。
 8spring+velocity+javamail发送模版邮件
 9spring+velocity+javamail发送模版邮件如果您还想申请管理员权限,可以联系管理员${email}
10spring+velocity+javamail发送模版邮件
11spring+velocity+javamail发送模版邮件我们对您产生的不便,深表歉意。
12spring+velocity+javamail发送模版邮件
13spring+velocity+javamail发送模版邮件希望您在worthtech度过快乐的时光!
14spring+velocity+javamail发送模版邮件    
15spring+velocity+javamail发送模版邮件-----------------------
16spring+velocity+javamail发送模版邮件     
17spring+velocity+javamail发送模版邮件(这是一封自动产生的email,请勿回复。)

mail.properties如下:
1spring+velocity+javamail发送模版邮件mail.host=218.000.000.234
2spring+velocity+javamail发送模版邮件mail.port=25
3spring+velocity+javamail发送模版邮件mail.username=gjni@worthtech.net
4spring+velocity+javamail发送模版邮件mail.password=xxxxxxxx
5spring+velocity+javamail发送模版邮件mail.smtp.auth=true
6spring+velocity+javamail发送模版邮件mail.smtp.timeout=25000
7spring+velocity+javamail发送模版邮件mail.default.from=gjni@worthtech.net