JavaMail API为Java提供了邮件发送和接受服务,支持常见的邮件协议 SMTP IMAP POP3,
发送纯文本邮件
public class Main {
public static void main(String[] args) throws AddressException,
MessagingException {
// 直接使用JavaMail API:
Properties props = new Properties();
// 设置SMTP服务器:
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
// 使用SSL安全连接:
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
send(props, "livebookstore", // Username
"LiVe-BoOkStOrE", // Password
"[email protected]", // From
new String[] { "[email protected]" }, // To
"A test mail", // Subject
"测试使用JavaMail API发送邮件" // Text
);
}
public static void send(Properties props, final String username,
final String password, String from, String[] to, String subject,
String text) throws AddressException, MessagingException {
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(text);
message.setSentDate(new Date());
Address[] addressTo = new Address[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
message.saveChanges();
Transport.send(message, addressTo);
}
}
Spring提供了非常方便的Mail抽象层,它通过一个
MailSender接口,封装了邮件发送Bean,
而
SimpleMailMessage封装了纯文本的简单邮件
把发送邮件的STMP配置注入Bean中
<!-- 配置MailSender -->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="livebookstore" />
<property name="password" value="LiVe-BoOkStOrE" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">
javax.net.ssl.SSLSocketFactory
</prop>
</props>
</property>
</bean>
// 使用Spring提供的MailSender:
ApplicationContext context = new ClassPathXmlApplicationContext(
"config.xml");
JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");
sentBySpring(mailSender);
private static void sentBySpring(JavaMailSender mailSender) {
// 创建一个纯文本邮件:
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("[email protected]");
mail.setTo("[email protected]");
mail.setSubject("Another test mail");
mail.setText("测试使用Spring MailSender发送邮件");
// 发送:
mailSender.send(mail);
}
发送Mime邮件
Spring提供助手类MimeMessageHepler,能方便的操作MimeMessage
private static void sentMime(JavaMailSender mailSender)
throws MessagingException {
// 发送Mime邮件:
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setSubject("Test mime mail");
// 设定为HTML格式:
helper.setText("<html><body>访问Live在线书店:<br>"
+ "<a href='http://www.livebookstore.net' target='_blank'>"
+ "<img src='cid:logo'></a></body></html>", true);
// 添加附件嵌入HTML中显示,在img标签中用"cid:xxx",
// 对应附件通过addInline()添加
helper.addInline("logo", new ClassPathResource("logo.gif"));
helper.addAttachment("freebsd.gif", new ClassPathResource("freebsd.gif"));
// 发送:
mailSender.send(mime);
}