• Spring Mail


    想必大家在今天这个特殊的日子里,一定热血沸腾了一把。为上午的阅兵点70个赞!

    进入Spring Mail的主题:

    前后大概花了8个小时的时间来研究Spring封装的javaMail。本来觉得挺简单的应用,但是真正动手实现起来,出现各种各样不知所措的问题。

    推荐几篇blog:

    1. http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mail.html

    2. http://obullxl.iteye.com/blog/703248

    3. http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc

    论资料,当然首推Spring的官方文档(参考1)。清晰,简介。只是想动手实现时,发现文档里没有类Order的实现,在几经查看文档并试着拼凑出Order类的实现时,感觉太过浪费时间,想着自己大体实现mail的功能就行,没必要注重细节。可这是,碰到了参考3。 不得不感慨几句,不劳而获总是要付出代价的!

    参考3中对Spring Mail的结构,实现等等,做了很清晰的概括+解释。而且也一步步build了一个Spring Mail实现。最后,还附上了源码!在Spring MVC框架下,实现邮件功能。

    本人在这个的基础上做了小的改动(主要因为gmail在国内不能用。。。),而就是这个小的改动,给我整出来一个个不知所云的exception!

    先把问题和原因总结一下:

    1. Spring的版本问题

      提供的源码中Spring的版本是3.2..,自己用的jdk版本是1.8的。这两个版本不匹配,会造成加载xml文件时的错误。

    2. 对JavaMailSenderImpl的配置问题

      由于源码中配置的是gmail,所以改成了163的。但是各个邮箱服务器的配置是有差异的。所以根据参考2里的说明,重新配置邮件服务器。但仍然出现让自己纠结了3个多小时的认证失败问题。修改javaMailProperties中的属性多次都无果。有人还说username只保留邮箱@符号的前部分,但尝试后仍然无果。最终认为自己163邮箱的smtp认证有问题吧。

    3. 根据2的假想,想重新注册一个163邮箱试试。这个时候163又TMD恶心了我一把:需要下载客户端才能注册! 果断换注册sina邮箱了! 紧接着重新配置JavaMailSenderImpl。最后完成了邮件发送。

    上代码 (以备后用):

    Spring用4.0版本以上的。最好用Maven管理。

    实验用的界面就是用的参考3的。

    web.xml未改动

    applicationContext.xml 如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:annotation-config />
        <mvc:annotation-driven />
        <context:component-scan base-package="net.codejava.spring" />
    
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="smtp.email.com" />
            <property name="username" value="your_email_address" />
            <property name="password" value="******" />
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtp.timeout">30000</prop>
                    <prop key="mail.debug">true</prop>
                    <!-- <prop key="mail.smtp.starttls.enable">true</prop> -->
                    
                </props>
            </property>
        </bean>
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <bean
            class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <prop key="java.lang.Exception">Error</prop>
                </props>
            </property>
        </bean>
    </beans>    

    class SendEmailController:

    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/sendEmail.do")
    public class SendEmailController {
    
        @Autowired
        private JavaMailSender mailSender;
        
        @RequestMapping(method = RequestMethod.POST)
        public String doSendEmail(HttpServletRequest request) {
            // takes input from e-mail form
            String sentFrom = "your_email_address";
            String recipientAddress = request.getParameter("recipient");
            String subject = request.getParameter("subject");
            String message = request.getParameter("message");
            
            // prints debug info
            System.out.println("To: " + recipientAddress);
            System.out.println("Subject: " + subject);
            System.out.println("Message: " + message);
            
            // creates a simple e-mail object
            SimpleMailMessage email = new SimpleMailMessage();
            email.setFrom(sentFrom);
            email.setTo(recipientAddress);
            email.setSubject(subject);
            email.setText(message);
            
            // sends the e-mail
            mailSender.send(email);
            
            // forwards to the view named "Result"
            return "Result";
        }
    }

    自己动手,丰衣足食!

    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    Python小工具:统计代码行数
    计算机图形学复习(一)
    牛客多校训练第一场 J.Different Integers
    数据校验码概述
    数据库复习第二章
    数据库复习第一章
    自动化AC器(带界面版)
    ZOJ 3747 Attack on Titans
    Codeforces Round #245 (Div. 1) B. Working out
    HDU 6266 Hakase and Nano 【博弈论】
  • 原文地址:https://www.cnblogs.com/hello-yz/p/4780573.html
Copyright © 2020-2023  润新知