java mail發送郵件(源代碼)

--------------------發送郵件時需要的基本信息類-------------------------

package com.lun.mail;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 發送郵件需要的基本信息
 * @author zhanglun
 * blog: blog.csdn.net/lun379292733
 */
public class MailSenderInfo {
 
 private String mailServerHost;      //發送郵件的服務器
 private String mailServerPort="25";    //發送郵件的服務器的端口
 private String fromAddress;            //發送者地址
 private String toAddress;              //接收者地址
 private String username;         //發送者用戶名
 private String password;         //發送者密碼
 private boolean validate=true;      //是否需要身份驗證
 private String subject;           //郵件主題
 private String content;          //郵件內容
 private Map<String, String> mailType;        //郵箱類型
 
 /**
  * 得到郵件會話屬性
  *
  */
 public Properties getProperties(){
  Properties p = new Properties();
  p.put("mail.smtp.host", this.mailServerHost);
  p.put("mail.smtp.port", this.mailServerPort);
  p.put("mail.smtp.auth", validate?"true":"false");
  return p;
 }
 
 public String getMailServerHost() {
  return mailServerHost;
 }
 public void setMailServerHost(String mailServerHost) {
  this.mailServerHost = mailServerHost;
 }
 public String getMailServerPort() {
  return mailServerPort;
 }
 public void setMailServerPort(String mailServerPort) {
  this.mailServerPort = mailServerPort;
 }
 public String getFromAddress() {
  return fromAddress;
 }
 public void setFromAddress(String fromAddress) {
  this.fromAddress = fromAddress;
 }
 public String getToAddress() {
  return toAddress;
 }
 public void setToAddress(String toAddress) {
  this.toAddress = toAddress;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
  this.fromAddress=username;
  if(username!=null&&username.length()>0){
   String fromEmailExt=username.substring(username.lastIndexOf("@")+1,username.lastIndexOf("."));
   this.mailServerHost=getMailType().get(fromEmailExt);
  }
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public boolean isValidate() {
  return validate;
 }
 public void setValidate(boolean validate) {
  this.validate = validate;
 }
 
 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }

 public Map<String, String> getMailType() {
  mailType=new HashMap<String, String>();
  mailType.put("163","smtp.163.com");
  mailType.put("139","smtp.139.com");
  mailType.put("126","smtp.126.com");
  mailType.put("qq", "smtp.qq.com");
  mailType.put("sohu", "smtp.sohu.com");
  mailType.put("live","smtp.live.cn");
  mailType.put("msn","smtp.msn.com");
  mailType.put("kum", "mail.kum.net.cn");
  mailType.put("hotmail","smtp.hotmail.cn");
  return mailType;
 }

 public void setMailType(Map<String, String> mailType) {
  this.mailType = mailType;
 }
 
}

 

------------------------------發郵件時的身份驗證器-------------------------------

package com.lun.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * 發郵件時的身份驗證器
 * @author zhanglun
 * blog: blog.csdn.net/lun379292733
 */
public class MyAuthenticator extends Authenticator{
 
 String userName = null;
 String password = null;
 
 public MyAuthenticator(String userName, String password) {
  this.userName = userName;
  this.password = password;
 }
 @Override
 protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(userName, password);
 }
 
}

 

-----------------------郵件發送器類,也是主體部分---------------------------

package com.lun.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;


/**
 * 郵件發送器
 * @author zhanglun
 * blog: blog.csdn.net/lun379292733
 */
public class MailSender{
 
 /**
  * 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session   
  * @return
  */
 protected  static Session getSession(MailSenderInfo mailSenderInfo){
  if(mailSenderInfo == null){
   return null;
  }
  MyAuthenticator authenticator = null;
  Properties pro = mailSenderInfo.getProperties();
  if(mailSenderInfo.isValidate()){
   authenticator = new MyAuthenticator(mailSenderInfo.getUsername(),mailSenderInfo.getPassword());
  }
  return Session.getDefaultInstance(pro, authenticator);
 }
 
 /**
  * 發送文本格式郵件
  * @return
  */
 public static boolean sendTextMail(MailSenderInfo mailSenderInfo){
  try {
       
         //得到session
         Session mailSession = getSession(mailSenderInfo); 

   //根據session創建一個郵件消息
   Message mailMessage=new MimeMessage(mailSession);
   
   //創建郵件發送者的地址
   Address fromAddress=new InternetAddress(mailSenderInfo.getFromAddress());
   
   //創建郵件接收者的地址
   Address toAddress=new InternetAddress(mailSenderInfo.getToAddress());
   
   //設置郵件發送者的地址
   mailMessage.setFrom(fromAddress);
   
   //設置郵件接收者地址
   mailMessage.setRecipient(Message.RecipientType.TO, toAddress);
   
   //設置郵件的主題
   mailMessage.setSubject(mailSenderInfo.getSubject());
   
   //設置郵件的發送內容
   mailMessage.setText(mailSenderInfo.getContent());
   
   //設置發送郵件的時間
   mailMessage.setSentDate(new Date());
   
   //發送郵件
   Transport.send(mailMessage);
   
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 發送html格式郵件
  * @return
  */
 public static boolean sendHtmlMail(MailSenderInfo mailSenderInfo) {
  try {
   //得到session
   Session mailSession = getSession(mailSenderInfo);

   //根據session創建一個郵件消息
   Message mailMessage = new MimeMessage(mailSession);

   //創建郵件發送者的地址
   Address fromAddress=new InternetAddress(mailSenderInfo.getFromAddress());
   
   //創建郵件接收者的地址
   Address toAddress=new InternetAddress(mailSenderInfo.getToAddress());
   
   //設置郵件發送者的地址
   mailMessage.setFrom(fromAddress);
   
   //設置郵件接收者地址
   mailMessage.setRecipient(Message.RecipientType.TO, toAddress);

   //設置郵件消息的主題
   mailMessage.setSubject(mailSenderInfo.getSubject());

   //設置郵件消息發送的時間
   mailMessage.setSentDate(new Date());

   //MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
   Multipart mainPart = new MimeMultipart();

   //創建一個包含HTML內容的MimeBodyPart
   BodyPart html = new MimeBodyPart();

   //設置HTML內容
   html.setContent(mailSenderInfo.getContent(), "text/html; charset=UTF-8");
   
   //將內容添加到容器對象中
   mainPart.addBodyPart(html);

   //將MiniMultipart對象設置爲郵件內容
   mailMessage.setContent(mainPart);

   //發送郵件
   Transport.send(mailMessage);
   
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
}

 

---------------------------------------測試-----------------------------------------

package com.lun.mail;

/**
 * 測試類
 *@author zhanglun
 * blog: blog.csdn.net/lun379292733
 */
public class Test{
 public static void main(String[] args) {
  MailSenderInfo senderInfo=new MailSenderInfo();
  senderInfo.setUsername("[email protected]"); //用戶名
  senderInfo.setPassword("123456");      //密碼
  senderInfo.setToAddress("[email protected]");//接收者
  senderInfo.setSubject("這是主題"); //主題
  senderInfo.setContent("<a href='http://baidu.com'>百度一下</a>"); //內容
  
  //發送
  MailSender.sendHtmlMail(senderInfo);
 }
}

發佈了24 篇原創文章 · 獲贊 14 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章