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 阅读(467) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(1)

随笔分类(17)

随笔档案(28)

文章分类(30)

文章档案(30)

相册

收藏夹(2)

hibernate

java基础

mysql

xml

关注

压力测试

算法

最新随笔

搜索

  •  

积分与排名

  • 积分 - 95546
  • 排名 - 602

最新评论

阅读排行榜

评论排行榜