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";
    }
}

 

自己動手,豐衣足食!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM