配置邮件服务mail

ApplicationContext.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.xsd"
>
<!-- 配置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>

</beans>

[code]
package example.chapter9;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Main {

public static void main(String[] args) throws Exception {
// 直接使用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
);
//-----------------------------------------------------------------Spring发送文本文件----------------------------------------
// 使用Spring提供的MailSender:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
JavaMailSender mailSender = (JavaMailSender)context.getBean("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);

//-----------------------------------------------------------------Spring发送带两个附件的邮件----------------------------------------
// 发送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);
helper.addInline("logo", new ClassPathResource("logo.gif"));
helper.addAttachment("freebsd.gif", new ClassPathResource("freebsd.gif"));
// 发送:
mailSender.send(mime);
}
//-------------------------------------------------------------普通邮件调用的方法-----------------------------------------------------
public static void send(Properties props, final String username, final String password, String from, String[] to, String subject, String text) throws MessagingException {
Session session = Session.getInstance(props, new Authenticator() {
public 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);
}

}

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值