Spring发送邮件

JavaMail API为Java提供了邮件发送和接受服务,支持常见的邮件协议 SMTP IMAP POP3,

[b]发送纯文本邮件[/b]
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
"livebookstore@gmail.com", // From
new String[] { "livebookstore2@gmail.com" }, // 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抽象层,它通过一个[b]MailSender[/b]接口,封装了邮件发送Bean,
而[b]SimpleMailMessage[/b]封装了纯文本的简单邮件

把发送邮件的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("livebookstore@gmail.com");
mail.setTo("livebookstore2@gmail.com");
mail.setSubject("Another test mail");
mail.setText("测试使用Spring MailSender发送邮件");
// 发送:
mailSender.send(mail);
}



[b]发送Mime邮件[/b]
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("livebookstore@gmail.com");
helper.setTo("livebookstore2@gmail.com");
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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值