java实现email发送

package cn.blog.utils;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtils {
 public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException {
  /*
   * 用SMTP协议发送Email时通常要设置mail.smtp.host(mail.protocol.host协议特定邮件服务器名)属性。
   * prop.put("mail.smtp.host","smtp.mailServer.com"); Session
   * mailSession=Session.getInstance(prop);
   */
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  Properties props = new Properties();
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.smtp.host", "smtp.gmail.com");
  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  props.setProperty("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.port", "465");
  props.setProperty("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.auth", "true");
  /*
   * props.setProperty("mail.transport.protocol", "smtp");
   * props.setProperty("mail.smtp.host", "smtp.sohu.com");
   * props.setProperty("mail.smtp.socketFactory.class", SSL_Factory);
   * //props.setProperty("mail.smtp.socketFactory.fallback", "false");
   * props.setProperty("mail.smtp.port", "25");
   * //props.setProperty("mail.smtp.socketFactory.port", "587");
   * props.setProperty("mail.smtp.starttls.enable", "true");
   * props.put("mail.smtp.auth", "true");
   */
  /*
   * 在真正使用创建的过程中,往往会让我们验证密码,这是我们要写一个密码验证类。
   * javax.mail.Authenticator是一个抽象类,我们要写MyAuthenticator的密码验证类,
   * 该类继承Authenticator实现
   */
  Authenticator auth = new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("put your email address before@ example:123456", "put your email password here");
   }
  };
  /* 这时我们创建Session对象 */
  Session session = Session.getInstance(props, auth);
  /*
   * MimeMessage扩展抽象的Message类,构造MimeMessage对象: MimeMessage message=new
   * MimeMessage(session);
   */
  /* MimeMessage */Message message = new MimeMessage(session);
  /*
   * 消息发送者、日期、主题 message.setFrom(Address theSender);
   * message.setSentDate(java.util.Date theDate);
   * message.setSubject(String theSubject);
   */
  message.setFrom(new InternetAddress("blueskyswiner@gmail.com"));
  // message.setSentDate(Calendar.getInstance().getTime());
  message.setSentDate(new Date());
  /*
   * setRecipient(Message.RecipientType type , Address
   * theAddress)、setRecipients(Message.RecipientType type , Address[]
   * theAddress)、addRecipient(Message.RecipientType type , Address
   * theAddress)、addRecipients(Message.RecipientType type,Address[]
   * theAddress)方法都可以指定接受者类型,但是一般用后两个,这样可以避免意外的替换或者覆盖接受者名单。定义接受者类型
   *
   * Message.RecipientType.TO://消息接受者 Message.RecipientType.CC://消息抄送者
   * Message.RecipientType.BCC://匿名抄送接收者(其他接受者看不到这个接受者的姓名和地址)
   */
  /*
   * message.addRecipient(Message.RecipientType.TO, new
   * InternetAddress(email));
   */
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
  message.setSubject("用户账号激活认证");
  message.setContent(emailMsg, "text/html;charset=utf-8");
  Transport.send(message);
 }
 public static void main(String[] args) {
  try {
   sendMail("put the email address that you send to", "put your message here!!! ");
   System.out.println("success");
  } catch (AddressException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

评论

热门博文