参考 http://www.blogjava.net/fancydeepin/category/51935.html
1、如果是maven项目,需要引入依赖包
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
2、在项目加载的basic.xml中,配置两个bean:JavaMailSenderImpl和SimpleMailMessage,在后续发送邮件中通过这两个类进行发送。
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" >
<value>----</value>
</property>
<property name="port" >
<value type="java.lang.Integer">----</value>
</property>
<property name="protocol">
<value>smtps</value>
</property>
<property name="username">
<value>----</value>
</property>
<property name="password">
<value>----</value>
</property>
<!-- SMTP服务器验证 -->
<property name="javaMailProperties">
<props>
<!-- 验证身份 -->
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.transport.protocol">smtps</prop>
</props>
</property>
</bean>
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>${notify_service.smtp_mail.username}</value>
</property>
</bean>
3、在实际发送的class里面调用
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage;
//初始化mailSender 和SimpleMailMessage 两个对象
simpleMailMessage.setTo(sendTo);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);
另外如果是发送HTML格式的邮件 参考:http://www.blogjava.net/fancydeepin/archive/2012/06/12/spring-smtp-mail-with-attachment_maven3.html
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
private JavaMailSender javaMailSender;
private MimeMessage mimeMessage;
private MimeMessageHelper mimeMessageHelper;
private SimpleMailMessage simpleMailMessage;
try {
mimeMessage = javaMailSender.createMimeMessage();
mimeMessageHelper = new MimeMessageHelper(mimeMessage,true,"GBK");
mimeMessageHelper.setFrom(simpleMailMessage.getFrom());
mimeMessageHelper.setTo(sendTo);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
4、如果不用Spring直接用javamail发送
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.Transport;
public class SendHtmlMail {
public void sendmail(){
//创建邮件发送类 JavaMailSender
//用于发送基本的文本邮件信息(不能包括附件,及图片)
JavaMailSender sender = new JavaMailSenderImpl();
//设置邮件服务主机
((JavaMailSenderImpl)sender).setHost("smtp.sohu.com");
//发送者邮箱的用户名
((JavaMailSenderImpl)sender).setUsername("userName");
//发送者邮箱的密码
((JavaMailSenderImpl)sender).setPassword("password");
//配置文件,用于实例化java.mail.session
Properties pro = System.getProperties();
//登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。
//测试 sohu 的邮箱可以获得授权
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.socketFactory.port", "25");
pro.put("mail.smtp.socketFactory.fallback", "false");
//通过文件获取信息
((JavaMailSenderImpl)sender).setJavaMailProperties(pro);
//创建基本邮件信息
MailMessage mailMessage = new SimpleMailMessage();
//发送者地址,必须填写正确的邮件格式,否者会发送失败
mailMessage.setFrom("******@sohu.com");
//邮件主题
mailMessage.setSubject("测试邮件");
//邮件内容,简单的邮件信息只能添加文本信息
mailMessage.setText("测试");
//邮件接收者的邮箱地址
mailMessage.setTo("******@qq.com");
//发送邮件,参数可以是数组
//sender.send(SimpleMailMessage[])
sender.send((SimpleMailMessage)mailMessage);
}
}
5、javaMail发送HTML个是的邮件
public void sendMimeMail() throws MessagingException{
//创建邮件发送类 JavaMailSender
//用于发送多元化邮件,包括附件,图片,html 等
JavaMailSender mimeMailSender = new JavaMailSenderImpl();
//设置邮件服务主机
((JavaMailSenderImpl)mimeMailSender).setHost("smtp.sohu.com");
//发送者邮箱的用户名
((JavaMailSenderImpl)mimeMailSender).setUsername("userName");
//发送者邮箱的密码
((JavaMailSenderImpl)mimeMailSender).setPassword("password");
//配置文件,用于实例化java.mail.session
Properties pro = System.getProperties();
//登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。
//测试 sohu 的邮箱可以获得授权
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.socketFactory.port", "25");
pro.put("mail.smtp.socketFactory.fallback", "false");
((JavaMailSenderImpl)mimeMailSender).setJavaMailProperties(pro);
//创建多元化邮件
MimeMessage mimeMessage = ((JavaMailSenderImpl)mimeMailSender).createMimeMessage();
//创建 mimeMessage 帮助类,用于封装信息至 mimeMessage
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GBK");
//基本信息
//发送者地址,必须填写正确的邮件格式,否者会发送失败
helper.setFrom("*******@sohu.com");
//邮件主题
helper.setSubject("测试邮件");
//邮件内容,简单的邮件信息只能添加文本信息
helper.setText("测试");
//邮件接收者的邮箱地址
helper.setTo("******@qq.com");
//多元化信息
//添加HTML 信息
File myPicture = new File("C:\\Documents and Settings\\Administrator\\桌面\\kz2.jpg");
//如果是html代码,boolean 的参数用该为true
helper.setText("<html><head><meta http-equiv="+"Content-Type"+" content="+"text/html; charset=gb2312"+"></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间"
+ "</a>中查看<br><img src='cid:myPicture'></body></html>", true);
//添加图片至HTML 代码中
helper.addInline("myPicture", myPicture);
//添加附件
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\validate.jar");
helper.addAttachment("picture", myPicture);
helper.addAttachment("picture", file);
((JavaMailSender)mimeMailSender).send(mimeMessage);
System.out.println("发送成功");
}