程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring整合javamail 用gmail發送郵件

Spring整合javamail 用gmail發送郵件

編輯:關於JAVA

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

}

發送郵件的實現類.

然後在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 -->

............................

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("我現在修改了郵件的內容 呵呵呵");

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved