又一个新的项目启动了,虽说目前还处于系统设计阶段,但从当前的系统需求来看,其中有一项功能就是通过Web方式发送系统邮件。还是提前做好准备吧,还说不错,花了一下午的时间,把封装了一个MailService类,直接调用即可。此篇仅介绍用javamail实现发送邮件功能,其中涉及smtp认证,邮件附件发送,及HTML内容邮件等。
系统环境:Windows XP SP3
开发环境:Eclipse 3.5/JDK 1.6/Struts2/Tiles
测试工具:Firefox 3.6/IE 8
测试邮箱:网易邮箱、IBM Lotus Notes邮箱
所需JAR包:mail.jar(包含MailService.java类文件) 下载地址一 下载地址二
MailService.java类文件(经过封装的发送邮件类):
- package com.olympus.Util;
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- public class MailService extends Authenticator {
- private String m_username = null;
- private String m_userpass = null;
- public void setUsername(String username) {
- m_username = username;
- }
- public void setUserpass(String userpass) {
- m_userpass = userpass;
- }
- public MailService(String username, String userpass) {
- super();
- setUsername(username);
- setUserpass(userpass);
- }
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(m_username,m_userpass);
- }
- public static void sendmail(
- String subject, String from,
- String[] to, String text,
- String[] filenames,String mimeType) throws Exception
- {
- Properties props = new Properties();
- String smtp = "smtp.163.com"; //设置发送邮件所用到的SMTP服务器地址
- String smtpname = "username"; //设置登录SMTP服务器的用户名
- String smtppassword = "password"; //设置登录SMTP服务器的密码
- Session mailSession; //邮件会话对象
- MimeMessage mimeMsg; //MIME邮件对象
- props = java.lang.System.getProperties(); //获得系统属性对象
- props.put("mail.smtp.host", smtp); //设置SMTP主机
- props.put("mail.smtp.auth", "true"); //链接到SMTP服务器是否需要用户名和密码验证
- //到服务器验证发送的用户名和密码是否正确
- MailService myEmailAuther = new MailService(smtpname, smtppassword);
- //设置邮件会话
- mailSession = Session.getInstance(props, (Authenticator) myEmailAuther);
- Transport transport = mailSession.getTransport("smtp"); //设置传输协议
- mimeMsg = new MimeMessage(mailSession); //设置from、to等信息
- InternetAddress sentFrom = new InternetAddress(from);
- mimeMsg.setFrom(sentFrom); //设置发送人地址
- InternetAddress[] sendTo = new InternetAddress[to.length];
- for (int i = 0; i < to.length; i++) {
- //System.out.println("发送到:" + to[i]);
- sendTo[i] = new InternetAddress(to[i]);
- }
- mimeMsg.setRecipients(MimeMessage.RecipientType.TO, sendTo);
- mimeMsg.setSubject(subject, "utf-8");
- MimeBodyPart messageBodyPart1 = new MimeBodyPart();
- //messageBodyPart.setText(UnicodeToChinese(text));
- messageBodyPart1.setContent(text, mimeType);
- Multipart multipart = new MimeMultipart();//附件传输格式
- multipart.addBodyPart(messageBodyPart1);
- for (int i = 0; i < filenames.length; i++) {
- MimeBodyPart messageBodyPart2 = new MimeBodyPart();
- String filename = filenames[i].split(",")[0]; //选择出每一个附件名
- String displayname = filenames[i].split(",")[1];
- FileDataSource fds = new FileDataSource(filename); //得到数据源
- //得到附件本身并至入BodyPart
- messageBodyPart2.setDataHandler(new DataHandler(fds));
- //得到文件名同样至入BodyPart
- //messageBodyPart2.setFileName(displayname);
- //messageBodyPart2.setFileName(fds.getName());
- messageBodyPart2.setFileName(MimeUtility.encodeText(displayname));
- multipart.addBodyPart(messageBodyPart2);
- }
- mimeMsg.setContent(multipart);
- mimeMsg.setSentDate(new Date()); //设置信件头的发送日期
- mimeMsg.saveChanges();
- Transport.send(mimeMsg); //发送邮件
- transport.close();
- }
- }
在Action中调用
- @Action(value="//",
- results={@Result(name="success", location="index",type="tiles")})
- public String Index(){
- String sendTo[] = {"liang.xu@ohc.olympus.com.hk"};
- String fileNames[] = {};
- try {
- MailService.sendmail(
- "JavaWeb发送邮件测试", "Alex Xu<xht555@163.com>",
- sendTo, "JavaWeb发送邮件测试",
- fileNames, "text/html;charset=utf-8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return SUCCESS;
- }
- 测试结果
- 我的网易邮箱显示邮件发送成功:
② 我的工作邮箱(IBM Lotus Notes)成功接收到来自网易邮箱发出的邮件:
-
: