Java發送郵件 – JAVA編程語言程序開發技術文章

import java.io.UnsupportedEncodingException; 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.Address; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
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 SendMail { 
 
 /**
  * 不需要驗證的郵件發送
  * String fromMail 發件人地址
  * String password 發件人密碼 
  * String toMail 收件人地址
  * String messageText 發送的消息
  * String title 發送的標題
  * String serviceName 使用的郵件服務器
  * @throws Exception
  */ 
 public static void setMessage(String fromMail, String toMail, 
   String messageText, String serviceName) throws Exception { 
  Properties props = System.getProperties(); 
  props.put("mail.smtp.host", serviceName); //設置smtp的服務器地址:該郵件服務器不需要身份驗證 
  props.put("mail.smtp.auth", "false"); //設置smtp服務器要身份驗證:缺省設置為false 
 
  Address from = new InternetAddress(fromMail); 
  Address to = new InternetAddress(toMail); 
 
  Session session = Session.getDefaultInstance(props, null); 
  Message message = new MimeMessage(session); 
  message.setFrom(from); 
  message.addRecipient(Message.RecipientType.TO, to); 
  message.setText(messageText); 
 
  Transport.send(message); 
   
   
 
 } 
  
  /**
   * 帶授權的發送郵件
   * String fromMail 發件人地址
   * String password 發件人密碼 
   * String toMail 收件人地址
   * String messageText 發送的消息
   * String title 發送的標題
   * String serviceName 使用的郵件服務器
   * @throws Exception 
   */ 
  public static void setMessageWithAuthentica(String fromMail,String password,String toMail,String messageText,String title,String serviceName) throws Exception{ 
  Properties props = new Properties(); 
  props.put("mail.smtp.host",serviceName);  //設置smtp的服務器地址是smtp.sohu.com 
  props.put("mail.transport.protocol", "smtp"); 
  props.put("mail.smtp.auth","true");         //設置smtp服務器要身份驗證。 
   
  String formMailName=fromMail.split("@")[0]; 
  String toMailName=toMail.split("@")[0]; 
    
  PopupAuthenticator auth = new PopupAuthenticator(formMailName,fromMail);  
  Session session = Session.getDefaultInstance(props,auth); 
    
  session.setDebug(true); 
 
  // 發送人地址 
  Transport transport = null; 
  try { 
   Address addressFrom = new InternetAddress(fromMail, formMailName); 
   // 收件人地址 
   Address[] addressTo =new Address[]{new InternetAddress(toMail, toMailName)}; 
     
   // 抄送地址 
   // Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush"); 
   Message message = new MimeMessage(session); 
   message.setText(messageText); 
   message.setSubject(title); 
   message.setFrom(addressFrom); 
   message.addRecipients(Message.RecipientType.TO,addressTo); 
   // message.addRecipient(Message.RecipientType.CC,addressCopy); 
   message.saveChanges(); 
   // session.setDebug(true); 
   transport = session.getTransport("smtp");  //創建連接 
   transport.connect(serviceName, fromMail, password);//連接服務器 
   transport.sendMessage(message, addressTo); //發送信息 
   transport.close();   //關閉連接   
  } catch (UnsupportedEncodingException e) { 
   e.printStackTrace(); 
   Loggers.error("發送郵件錯誤:SendMail–setMessageWithAuthentica–UnsupportedEncodingException:"+e); 
  } catch (MessagingException e) { 
   e.printStackTrace(); 
   Loggers.error("發送郵件錯誤:SendMail–setMessageWithAuthentica–MessagingException"+e); 
  } finally { 
   if(transport != null & transport.isConnected()){ 
    transport.close(); 
   } 
  } 
  } 
   
   
  public static void setMsg(String toMail, String title, String content)throws MessagingException { 
  Properties props = new Properties(); 
  props.put("mail.smtp.host", "smtp.163.com"); 
  props.put("mail.smtp.auth", "true"); 
  Session s = Session.getInstance(props); 
  s.setDebug(true); 
 
  MimeMessage message = new MimeMessage(s); 
  Transport transport = null; 
  try { 
   InternetAddress from = new InternetAddress("xxx@163.com"); 
   message.setFrom(from); 
   InternetAddress to = new InternetAddress(toMail); 
   message.setRecipient(Message.RecipientType.TO, to); 
   message.setSubject(title); 
   message.setText(content); 
   message.setSentDate(new Date()); 
   message.saveChanges(); 
   transport = s.getTransport("smtp"); 
   transport.connect("smtp.163.com", "xxx", "xxx"); 
   transport.sendMessage(message, message.getAllRecipients()); 
   transport.close(); 
  } catch (AddressException e) { 
   e.printStackTrace(); 
   Loggers.error("發送郵件錯誤:SendMail–setMsg–AddressException:" + e); 
  } catch (MessagingException e) { 
   e.printStackTrace(); 
   Loggers.error("發送郵件錯誤:SendMail–setMsg–MessagingException:" + e); 
  } finally { 
   if (transport != null & transport.isConnected()) { 
    transport.close(); 
   } 
  } 
 } 
   
  public static void main(String[] args){ 
   try { 
    SendMail.setMessageWithAuthentica("您的郵箱", "您的密碼", "對方郵箱", "郵件內容", "郵件標題", "服務器地址"); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  } 

[java]
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
 
public class PopupAuthenticator extends Authenticator { 
    private String uname;//用戶名 
 
    private String password;//密碼 
 
    //屬性方法 
    public String getUname() { 
        return uname; 
    } 
 
    public void setUname(String uname) { 
        this.uname = uname; 
    } 
 
    public String getPassword() { 
        return password; 
    } 
 
    public void setPassword(String password) { 
        this.password = password; 
    } 
 
    //構造方法 
    public PopupAuthenticator() { 
 
    } 
 
    public PopupAuthenticator(String uname, String password) { 
        this.uname = uname; 
        this.password = password; 
    } 
 
    public PasswordAuthentication getPasswordAuthentication() { 
        String username = uname; //郵箱登錄帳號 
        String pwd = password; //登錄密碼 
        return new PasswordAuthentication(username, pwd); 
    } 

摘自 那年那月那天

發佈留言