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);
}
}
=============================================================================
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("发送成功");
}