posts - 28,  comments - 15,  trackbacks - 0

          ???發(fā)送郵件需要通過(guò)驗(yàn)證,我們需要建立一個(gè)帶有實(shí)現(xiàn)了Authenticator的Session.我們應(yīng)該了解,在用Java編寫(xiě)JavaMail時(shí)是需要使用的驗(yàn)證的,但是,在Spring中沒(méi)有提供注入驗(yàn)證實(shí)現(xiàn)的功能(即注入Authenticator的實(shí)現(xiàn)).但是我們可以用以下方式加以實(shí)現(xiàn),下面就以gmail郵件為例說(shuō)明:
          ???在Spring中的配置信息如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xsi:schemaLocation="http://www.springframework.org/schema/beans
          							http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          
                  <!-- our Authenticator implementation -->
          	<bean id="smtpAuthenticator"
          		class="forums.mailtest.SmtpAuthenticator">
          		<constructor-arg value="yourname@gmail.com" />
          		<constructor-arg value="password" />
          	</bean>
          
                  <!-- now setup an authenticated session -->
          	<bean id="mailSession" class="javax.mail.Session"
          		factory-method="getInstance">
          		<constructor-arg>
          			<props>
          				<prop key="mail.smtp.auth">true</prop>
          				<prop key="mail.smtp.socketFactory.port">465</prop>
          				<prop key="mail.smtp.socketFactory.class">
          					javax.net.ssl.SSLSocketFactory
          				</prop>
          				<prop key="mail.smtp.socketFactory.fallback">
          					false
          				</prop>
          			</props>
          		</constructor-arg>
          		<constructor-arg ref="smtpAuthenticator" />
          	</bean>
          
                  <!-- and configure the MailSender with the authenticated session -->
          	<bean id="mailSender"
          		class="org.springframework.mail.javamail.JavaMailSenderImpl">
          		<property name="host" value="smtp.gmail.com" />
          		<property name="session" ref="mailSession" />
          	</bean>
          
          	<!-- this is a template message that we can pre-load with default state -->
          	<bean id="templateMessage"
          		class="org.springframework.mail.SimpleMailMessage">
          		<property name="from" value="yourname@gmail.com" />
          		<property name="subject" value="Testing" />
          	</bean>
          
          	<bean id="mailTest" class="forums.mailtest.MailTest">
          		<constructor-arg ref="mailSender" />
          		<constructor-arg ref="templateMessage" />
          	</bean>
          </beans>

          實(shí)現(xiàn)我們自己的驗(yàn)證實(shí)現(xiàn)
          package forums.mailtest;
          
          import javax.mail.Authenticator;
          import javax.mail.PasswordAuthentication;
          
          public class SmtpAuthenticator extends Authenticator {
          	private String username;
          	private String password;
          	
          	public SmtpAuthenticator(String username, String password) {
          		super();
          		this.username = username;
          		this.password = password;
          	}
          
          	public PasswordAuthentication getPasswordAuthentication() {
                          return new PasswordAuthentication(username, password);
          	}
          }
          ???編寫(xiě)測(cè)試類(lèi)發(fā)送郵件:
          package forums.mailtest;
          
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.mail.MailException;
          import org.springframework.mail.MailSender;
          import org.springframework.mail.SimpleMailMessage;
          
          public class MailTest {
          	private MailSender mailSender;
          	private SimpleMailMessage templateMessage;
          	public MailTest(MailSender mailSender, SimpleMailMessage templateMessage) {
          		super();
          		this.mailSender = mailSender;
          		this.templateMessage = templateMessage;
          	}
          	
          	public void sendMeMail() {
          		SimpleMailMessage msg = new SimpleMailMessage(templateMessage);
          		msg.setTo("test@example.com");
          		msg.setText("This is a test.\nGo Spring!\n");
          		try {
          			this.mailSender.send(msg);
          		} catch (MailException e) {
          			System.err.println("Didn't work.");
          			e.printStackTrace();
          		}
          	}
          	
          	public static final void main(String[] args) {
          		ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {
          				"forums/mailtest/application-config.xml"
          		});
          		
          		MailTest tester = (MailTest) appCtx.getBean("mailTest");
          		tester.sendMeMail();
          	}
          }

          ???對(duì)于gmail端口的設(shè)置可以參考:http://mail.google.com/support/bin/a...y?answer=13287,個(gè)人認(rèn)為yahoo和 gmail 都希望我們使用驗(yàn)證,并且我們可以為每個(gè)郵件服務(wù)器設(shè)置不同的端口.如果沒(méi)有采用驗(yàn)證,與gmail的連接將會(huì)拋出一個(gè)沒(méi)有驗(yàn)證信息的異常!
          posted on 2006-06-08 19:05 zhangxl 閱讀(474) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Spring
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)(17)

          隨筆檔案(28)

          文章分類(lèi)(30)

          文章檔案(30)

          相冊(cè)

          收藏夾(2)

          hibernate

          java基礎(chǔ)

          mysql

          xml

          關(guān)注

          壓力測(cè)試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96364
          • 排名 - 601

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 武城县| 师宗县| 西充县| 台东县| 宁乡县| 定西市| 手游| 崇明县| 海伦市| 和顺县| 临高县| 广昌县| 昭苏县| 东安县| 科尔| 大连市| 焉耆| 江北区| 靖江市| 新化县| 漾濞| 常熟市| 陆川县| 体育| 连平县| 禄劝| 伊金霍洛旗| 渭南市| 孙吴县| 宜丰县| 湘乡市| 连城县| 邳州市| 讷河市| 遂昌县| 旅游| 井研县| 依安县| 日土县| 松滋市| 吉林省|