Springmvc 发送邮件功能

Easter79
• 阅读 643

1、引入相关jar包

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

Spring的基本核心jar包没有在上面写出来

2、编辑spring配置文件

<bean id="webEmailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.126.com" />
        <property name="port" value="25" />
        <property name="username" value="XXXXX@126.com" />
        <property name="password" value="**********" />
        <property name="javaMailProperties">
            <props>
                <!--设置是否需要身份验证 -->
                <prop key="mail.smtp.auth">true</prop>
            </props>
        </property>
    </bean>
    <bean id="smg" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="你的发送邮箱" />
        <property name="to" value="你的接受邮箱" />
    </bean>

    <bean id="velocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="file.resource.loader.cache">false</prop>
                <prop key="file.resource.loader.modificationCheckInterval">3</prop>
                <prop key="resource.loader">file</prop>
                <prop key="file.resource.loader.path">.</prop>
                <prop key="file.resource.loader.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
                <prop key="velocimacro.library"></prop>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
                <prop key="default.contentType">application/x-www-form-urlencoded; charset=UTF-8</prop>
            </props>
        </property>
    </bean>

@Controller
@RequestMapping("/mail")
public class WebEmail {

    @Resource(name = "webEmailSender")
    private JavaMailSenderImpl mailSender;
    @Resource(name = "smg")
    private SimpleMailMessage MailMessage;
    @Resource
    private VelocityEngine velocityEngine;

    @RequestMapping(value="test1", method=RequestMethod.GET)
    @ResponseBody
    public void send(@RequestParam("s") String s) {
        System.out.println(s);
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setFrom(MailMessage.getFrom());
            messageHelper.setSubject(s); //主题
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("username", "xxxx");
            map.put("url", "http://www.baidu.com");  
            map.put("email", "gjni@worthtech.net");
            String result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "config/welcome.vm", "UTF-8",map); 
            messageHelper.setText(result, true);   //内容
            messageHelper.setTo(MailMessage.getTo()); //发送给
            mailSender.send(mimeMessage);    //发送邮件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

MimeMessageHelper(mimeMessage, true, "UTF-8") 要设置成UTF-8 ,如果不这样会发现,发送的邮件内容中文为乱码

点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
springboot整合redisson分布式锁
一、通过maven引入redisson的jar包<dependency<groupIdorg.redisson</groupId<artifactIdredisson</artifactId<version3.6.5</version
Wesley13 Wesley13
3年前
java配置SSM框架下的redis缓存
pom.xml引入依赖包<!jedis.jar<dependency<groupIdredis.clients</groupId<artifactIdjedis</artifactId<version2.9.0</version</depe
Easter79 Easter79
3年前
spring集成kafka
1、引入依赖jar包<dependency<groupIdorg.springframework.kafka</groupId<artifactIdspringkafka</artifactId</dependency2、配置kafka信息
Easter79 Easter79
3年前
springmvc与fastjson集成
1.导入maven依赖<dependency<groupIdcom.alibaba</groupId<artifactIdfastjson</artifactId<version1.2.20</version</dependency2.springmvc.xml配置
Wesley13 Wesley13
3年前
Java使用Sftp实现对跨服务器上传、下载、打包、写入相关操作
1、Maven引入jar<dependency<groupIdcom.jcraft</groupId<artifactIdjsch</artifactId<version0.1.54</version</dependency2、Test类pac
Stella981 Stella981
3年前
Spring boot 的腾讯企业邮箱发送 实例
1\.引入springbootstartermail依赖包<!支持发送邮件<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstartermail</artifactId
Wesley13 Wesley13
3年前
1.WebSocket编程—Hello World
1.引入jar包<dependencies<!servlet<dependency<groupIdjavax.servlet</groupId<artifactIdjavax.servletapi</artifactId<ve
Stella981 Stella981
3年前
H5 WebSocket java服务端push
1.pom<dependency<groupIdjavax</groupId<artifactIdjavaeeapi</artifactId<version7.0</version</dependency这里使用javax.websocket,没有使用springmvc.webs
Wesley13 Wesley13
3年前
MongoDB简单的增删改查
首先引入mongoDB的jar包<dependencies  <dependency    <groupIdorg.mongodb</groupId    <artifactIdmongojavadriver</artifactId    <version3.0.4</version  </dependency
Stella981 Stella981
3年前
HTTP请求客户端工具类
1.maven引入依赖<dependency<groupIdcommonshttpclient</groupId<artifactIdcommonshttpclient</artifactId<version3.1</version</dependency<!https://mvnrepos
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k