ITeye博客

Spring-mail(1)

Technoboy · 2011-03-31 16:01 · Spring · 企业架构 · Markdown

  1. Java Mail介绍 JavaEE框架为应用提供了JavaMail接口,通过JavaMail相关的接口可以读取邮件服务器的邮件,并且可以完成邮件的发送过程。JavaMail是JavaEE中的标准API,是对邮件服务器访问过程的封装。使用JavaMail API则不需要编写与邮件服务器交互的详细过程,只需要调用相关接口即可。 Java API 主要包括四个部分: Session, Message, Transport 和 InternetAddress。 1.1 Session Session定义了全局的和每个用户的与邮件相关的属性。这些属性说明了客户机和服务器如何交流信息。 mail.store.protocol:检索邮件使用的协议 mail.transport.protocol:发送邮件使用的协议 mail.host:服务器主机名 mail.user:检索邮件或者发送邮件的用户名 mail.protocol.host:发送邮件服务器或者接受邮件服务器 mail.protocol.user:登陆特定邮件服务器使用的用户名 mail.from:指定默认的回复地址

1.2 Message 一个抽象类,表示单个邮件信息,其属性包括消息类型。地址消息和锁定义的目录结构。一般我们使用其MimeMessage子类。 Message类的主要方法有两部分,一是在发送邮件时候使用,用于设置邮件的相关信息,包括发送者,接受者,主题等信息。 如:


  setFrom(Address address); //注意这里的参数为javax.mail.Address类型
  setRecipients();
  setSentDate();
  

二是用于获取邮件的相关信息。 如:


 Flags getFlags();   //获取与邮件相关的标记属性
 Folder getFolder(); //获取邮件所在的文件夹

1.3 Transport 一个抽象类,用于发送邮件。我们经常用这两个方法:


public static void send(Message msg); //发送邮件
public static void send(Message msg, Address[] addresses); //第一参数是要发送邮件的本身,第二个参数是邮件发送的目的地,它会忽略邮件中设置的接受者。

1.4 InternetAddress InternetAddress就是把用户的Email映射为Internet地址。注意,在Message中确定邮件的接受者和发送者,以及在发送邮件的时候使用的都是Address对象。InternetAddress是Address的子类。经常使用的构造函数:


  public InternetAddress(String address);//把字符串构造成InternetAddress

1.5 发送邮件的步骤! 1)获取Session 2)将邮件地址解析为InternetAddress,构造Message消息 3)通过Transport的send方法发送消息。

我们先来通过apache的commons email来简单实现个邮件发送,然后使用spring的mail,并详解spring的mail封装。(本文的commons email为commons-email-1.2) 需要mail.jar和commons-email-1.2.jar。


public class CommonsEmail {
	public static void main(String[] args) throws Exception {
		Email email = new SimpleEmail();
		email.setHostName("smtp.qq.com");
		email.setSmtpPort(25);
		email.setAuthenticator(new DefaultAuthenticator("465553115#qq.com", "your password"));
		email.setFrom("465553115@qq.com"); //注意,这里的发送地址一定要和上面的验证地址一致
		email.setSubject("commons email");
		email.setMsg("a mail sent used by commons email");
		email.addTo("technoboy#yeah.net");
		email.send();
		System.out.println("over");
	}
}

好了,利用commons email我们简单发了一封邮件。(分析过spring的mail后,大家也就明白了commons email) 我们看一下Spring里的mail。Spring提供了一个发送电子邮件的高级抽象层,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理。发送邮件的主要接口为MailSender以及值对象SimpleMailMessage。 看一下MailSender接口中的方法:


void send(SimpleMailMessage simpleMessage);
void send(SimpleMailMessage[] simpleMessages);

这里方法的参数就是我们提到的值对象。 而SimpleMailMessage的部分定义如下:


public class SimpleMailMessage implements MailMessage, Serializable {

	private String from;

	private String replyTo;

	private String[] to;

	private String[] cc;

	private String[] bcc;

	private Date sentDate;

	private String subject;

	private String text;

那么,我们就明白了SimpleMailMessage的用处了,是用来设置发送者,接受者,主题的一些信息的类。 回过头,我们继续看一下MailSender的层次结构图:

我们可以看到,真正实现mailSender类是JavaMailSenderImpl。这个也就是spring中发送邮件的核心类。看一下它部分的代码:


public class JavaMailSenderImpl implements JavaMailSender {

	/** The default protocol: 'smtp' */
	public static final String DEFAULT_PROTOCOL = "smtp";

	/** The default port: -1 */
	public static final int DEFAULT_PORT = -1;

	private static final String HEADER_MESSAGE_ID = "Message-ID";


	private Properties javaMailProperties = new Properties();

	private Session session;

	private String protocol = DEFAULT_PROTOCOL;

	private String host;

	private int port = DEFAULT_PORT;

	private String username;

	private String password;

	private String defaultEncoding;

	private FileTypeMap defaultFileTypeMap;

从这里,我们可以看出,JavaMailSenderImpl使用的默认协议为smtp,如果需要安全验证,我们需要制定用户名,提供密码。那么,我们使用spring来实现mail的发送: (需要导入mail.jar 和activation.jar。这里使用的是spring3.0)

  1. spring_mail.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	
<bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl">
	<property name="host" value = "smtp.qq.com"></property>
	<property name="protocol" value = "smtp"></property>
	<property name="username" value = "465553115#qq.com"></property>
	<property name="password" value = "your password"></property>
	<property name="port" value = "25"></property>
	<!-- 这里设置验证 -->
	<property name="javaMailProperties">
		<props>
			<prop key="mail.smtp.auth">true</prop>
		</props>
	</property>
	
</bean>	

<bean id ="mailMessage" class = "org.springframework.mail.SimpleMailMessage">
    <!-- from的地址需要和上面username地址一致 -->
	<property name="from" value = "465553115#qq.com"></property>
	<property name="subject" value = "spring mail test"></property>
	<property name="to" value = "technoboy#yeah.net"></property>
	<property name="text" value = "a mail sent used by spring mail"></property>
</bean>

<bean id ="DummySpringMail" class = "org.com.mail.DummySpringMail">
	<property name="mailSender" ref = "mailSender"/>
	<property name="mailMessage" ref = "mailMessage"/>
</bean>

</beans>
  1. DummySpringMail类

public class DummySpringMail {
	
	//
	private MailSender mailSender;
	private SimpleMailMessage mailMessage;
	
	public void setMailMessage(SimpleMailMessage mailMessage) {
		this.mailMessage = mailMessage;
	}

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	//
	public void sendMsg(){
		SimpleMailMessage  smm = new SimpleMailMessage(this.mailMessage);
		this.mailSender.send(smm);
	}
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_mail.xml");
		DummySpringMail som = (DummySpringMail)context.getBean("DummySpringMail");
		som.sendMsg();
		System.out.println("over");
	}
	
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。