闲言少叙,现在就说说Spring邮件抽象层。这里将要用的接口和类有:
1 ) MailSender ,它是发送邮件的主要接口,代码如下:
public interface MailSender {
/**
* Send the given simple mail message.
* @param simpleMessage the message to send
* @throws org.springframework.mail.MailParseException
* in case of failure when parsing the message
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending the message
*/
void send(SimpleMailMessage simpleMessage) throws MailException;
/**
* Send the given array of simple mail messages in batch.
* @param simpleMessages the messages to send
* @throws org.springframework.mail.MailParseException
* in case of failure when parsing a message
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending a message
*/
void send(SimpleMailMessage[] simpleMessages) throws MailException;
}
2 )一个简单邮件信息的实现类 SimpleMailMessage 。
3 ) JavaMailSender 接口,提供使用 JavaMail 中 MimeMessage ,代码如下:
public interface JavaMailSender extends MailSender {
/**
* Create a new JavaMail MimeMessage for the underlying JavaMail Session
* of this sender. Needs to be called to create MimeMessage instances
* that can be prepared by the client and passed to send(MimeMessage).
* @return the new MimeMessage instance
* @see #send(MimeMessage)
* @see #send(MimeMessage[])
*/
MimeMessage createMimeMessage();
/**
* Create a new JavaMail MimeMessage for the underlying JavaMail Session
* of this sender, using the given input stream as the message source.
* @param contentStream the raw MIME input stream for the message
* @return the new MimeMessage instance
* @throws org.springframework.mail.MailParseException
* in case of message creation failure
*/
MimeMessage createMimeMessage(InputStream contentStream) throws MailException;
/**
* Send the given JavaMail MIME message.
* The message needs to have been created with {@link #createMimeMessage()} .
* @param mimeMessage message to send
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending the message
* @see #createMimeMessage
*/
void send(MimeMessage mimeMessage) throws MailException;
/**
* Send the given array of JavaMail MIME messages in batch.
* The messages need to have been created with {@link #createMimeMessage()} .
* @param mimeMessages messages to send
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending a message
* @see #createMimeMessage
*/
void send(MimeMessage[] mimeMessages) throws MailException;
/**
* Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
* <p> Alternative way to prepare MimeMessage instances, instead of
* {@link #createMimeMessage()} and {@link #send(MimeMessage)} calls.
* Takes care of proper exception conversion.
* @param mimeMessagePreparator the preparator to use
* @throws org.springframework.mail.MailPreparationException
* in case of failure when preparing the message
* @throws org.springframework.mail.MailParseException
* in case of failure when parsing the message
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending the message
*/
void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;
/**
* Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
* <p> Alternative way to prepare MimeMessage instances, instead of
* {@link #createMimeMessage()} and {@link #send(MimeMessage[])} calls.
* Takes care of proper exception conversion.
* @param mimeMessagePreparators the preparator to use
* @throws org.springframework.mail.MailPreparationException
* in case of failure when preparing a message
* @throws org.springframework.mail.MailParseException
* in case of failure when parsing a message
* @throws org.springframework.mail.MailAuthenticationException
* in case of authentication failure
* @throws org.springframework.mail.MailSendException
* in case of failure when sending a message
*/
void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;
}
4 ) MimeMessagePreparator ,支持 MimeMessage 的回调接口,代码如下:
public interface MimeMessagePreparator {
/**
* Prepare the given new MimeMessage instance.
* @param mimeMessage the message to prepare
* @throws javax.mail.MessagingException passing any exceptions thrown by MimeMessage
* methods through for automatic conversion to the MailException hierarchy
* @throws java.io.IOException passing any exceptions thrown by MimeMessage methods
* through for automatic conversion to the MailException hierarchy
* @throws Exception if mail preparation failed, for example when a
* Velocity template cannot be rendered for the mail text
*/
void prepare(MimeMessage mimeMessage) throws Exception;
}
使用 JavaMail ,需要依赖 mail.jar 和 activation.jar 两个包。利用 Spring 的 IOC 优势,可以通过配置文件来配置邮件发送信息。对于 MailSender , Spring 提供了实现类 JavaMailSenderImpl ,可以如下配置 MailSender 信息。
< bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl" >
< property name = "host" value = "smtp.126.com" ></ property >
< property name = "javaMailProperties" >
< props >
< prop key = "mail.smtp.auth" > true </ prop >
< prop key = "mail.smtp.timeout" > 20000 </ prop >
</ props >
</ property >
< property name = "username" value = "kafka0102" ></ property >
< property name = "password" value = "****" ></ property >
</ bean >
可以想到,如果是在 Service 中调用 mailSender ,可以通过 IOC 将 mailSender 注入到 Service 中。这里给出一个简单的使用演示:
BeanFactory bf = new ClassPathXmlApplicationContext( "service-applicationContext.xml" );
MailSender ms = (MailSender)bf.getBean( "mailSender" );
SimpleMailMessage smm = new SimpleMailMessage();
smm.setTo( "[email protected]" );
smm.setFrom( "[email protected]" );
smm.setSubject( "hello" );
smm.setText( "I am you" );
ms.send(smm);
其中, SimpleMailMessage 也可以通过配置文件配置使用到的属性。 SimpleMailMessage 不能发送带有附件的邮件,这时就需要 JavaMailSender (它的实现类也是 JavaMailSenderImpl ), MimeMessagePreparator 的实现类和部分 JavaMail API 。 mailSender 的配置不变,添加一个实现 MimeMessagePreparator 的类 OneMimeMessagePreparator ,代码如下。
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.mail.javamail.MimeMessagePreparator;
public class OneMimeMessagePreparator implements MimeMessagePreparator{
public void prepare(MimeMessage mm) throws Exception {
mm.setRecipient(Message.RecipientType. TO , new InternetAddress( "[email protected]" ));
mm.setFrom( new InternetAddress( "[email protected]" ));
mm.setSubject( "I am you" );
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText( "hello kafka0102" );
mp.addBodyPart(mbp);
String[] files = new String[]{ "/home/kafka0102/Document/ 常用基础算法 .txt" , "/home/kafka0102/Document/21164.html" };
for (String f : files){
MimeBodyPart mbpFile = new MimeBodyPart();
FileDataSource fds = new FileDataSource(f);
mbpFile.setDataHandler( new DataHandler(fds));
mbpFile.setFileName(fds.getName());
mp.addBodyPart(mbpFile);
}
mm.setContent(mp);
mm.setSentDate( new Date());
}
}
OneMimeMessagePreparator 的功能就是配置 MimeMessage ,其中添加了两个附件。下面就是简单的使用方法。
BeanFactory bf = new ClassPathXmlApplicationContext("service-applicationContext.xml");
JavaMailSender ms = (JavaMailSender)bf.getBean("mailSender");
ms.send(new OneMimeMessagePreparator());