<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 28,  comments - 15,  trackbacks - 0

    ???發送郵件需要通過驗證,我們需要建立一個帶有實現了Authenticator的Session.我們應該了解,在用Java編寫JavaMail時是需要使用的驗證的,但是,在Spring中沒有提供注入驗證實現的功能(即注入Authenticator的實現).但是我們可以用以下方式加以實現,下面就以gmail郵件為例說明:
    ???在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>

    實現我們自己的驗證實現
    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);
    	}
    }
    ???編寫測試類發送郵件:
    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();
    	}
    }

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

    常用鏈接

    留言簿(1)

    隨筆分類(17)

    隨筆檔案(28)

    文章分類(30)

    文章檔案(30)

    相冊

    收藏夾(2)

    hibernate

    java基礎

    mysql

    xml

    關注

    壓力測試

    算法

    最新隨筆

    搜索

    •  

    積分與排名

    • 積分 - 96278
    • 排名 - 601

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 在人线av无码免费高潮喷水| 久久久亚洲精品视频| 免费在线视频一区| 国产亚洲精品激情都市| 亚洲激情在线视频| 黄色毛片免费网站| 亚洲图片校园春色| 亚洲日韩中文字幕一区| 久久精品亚洲一区二区三区浴池| 亚洲一区在线观看视频| 污视频网站免费在线观看| 久久国产乱子伦精品免费看| h片在线播放免费高清| 国产亚洲精品仙踪林在线播放| 国产午夜亚洲精品国产| 亚洲一卡二卡三卡| 又硬又粗又长又爽免费看| 久久免费看黄a级毛片| 亚洲毛片网址在线观看中文字幕| 亚洲精品国产精品乱码不卡| 亚洲一区二区三区夜色| 亚洲AV日韩AV永久无码色欲| 国产成人久久AV免费| 四虎在线播放免费永久视频| 久久久久亚洲av无码专区导航| 男男gvh肉在线观看免费| 最近中文字幕电影大全免费版| 亚洲高清免费视频| 亚洲精品成人无限看| 久久亚洲高清观看| 亚洲欧美自偷自拍另类视| 免费日本一区二区| **aaaaa毛片免费| 久久亚洲国产精品五月天婷| 亚洲综合校园春色| 久久精品亚洲日本波多野结衣| 69国产精品视频免费| 亚洲中文字幕在线第六区| 国产成人亚洲精品电影| 一个人在线观看视频免费| 亚洲黄色片在线观看|