您的位置:首页 > 编程语言 > Java开发

【JavaMail开发总结】配置文件形式--发送邮件程序

2015-08-09 17:10 204 查看
在上一篇中简单的实现了一个发送邮件功能的程序,今天用配置文件的方式来实现,大致思路一致,示例代码如下:

package com.javamail.test.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailUtil {
 /**
  * 发送邮件
  * @param to
  *   收信人
  * @param subject
  *   邮件主题
  * @param content
  *   邮件内容
  * @throws IOException
  * @throws AddressException
  * @throws MessagingException
  */
 public static void send_email(String to, String subject, String content)
               throws IOException, AddressException, MessagingException {
     Properties properties = new Properties();
     InputStream resourceAsStream = null;
     try {
         // 获取配置文件
         resourceAsStream = EmailUtil.class.getClassLoader().getResourceAsStream("email.properties");
         properties.load(resourceAsStream);
     } finally {
          if (resourceAsStream != null) {
              resourceAsStream.close();
          }
     }
     // System.err.println("properties:"+properties);
     properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
     properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
     properties.put("mail.smtp.auth", "true");
     Authenticator authenticator = new Email_Authenticator(properties.get(
             "username").toString(), properties.get("password").toString());
     javax.mail.Session sendMailSession = javax.mail.Session
              .getInstance(properties, authenticator);
     MimeMessage mailMessage = new MimeMessage(sendMailSession);
     //设置发信人
     mailMessage.setFrom(new InternetAddress(properties.get("username")
              .toString(),"***"));
     // 设置收信人,Message.RecipientType.TO 收信人,Message.RecipientType.CC抄送人
     mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
               to,properties.get("username").toString()));
     //主题
     mailMessage.setSubject(subject, "UTF-8");
     //设置邮件发送日期
     mailMessage.setSentDate(new Date());
     // MiniMultipart类是一个容器类
     Multipart mainPart = new MimeMultipart();
     // 创建一个邮件体对象
     BodyPart html = new MimeBodyPart();
     html.setContent(content.trim(), "text/html; charset=utf-8");
     mainPart.addBodyPart(html);
     mailMessage.setContent(mainPart);
     Transport.send(mailMessage);
  }
}
//通过构造函数传入身份验证信息
class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}


配置文件如下(一般放在src目录下):

#email Configuration
#need account and password
#author cash
#host
mail.smtp.host=smtp.qq.com
#port
mail.smtp.port=25
#your email account
username=***@qq.com
#your email password
password=***


"mail.smtp.host=smtp.qq.com"指的是用qq.com的smtp服务器,除此之外还有163.com等等

"mail.smtp.port=25"指的是SMTP服务器端口是25
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: