程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> JAVA 郵件發送,java郵件發送

JAVA 郵件發送,java郵件發送

編輯:JAVA綜合教程

JAVA 郵件發送,java郵件發送


                      郵件發送

 

1.寫一個 MailSendProper 類封裝需要的屬性

import java.util.Properties;

public class MailSendProper {
    private String Host ;          //發送郵件服務器的ip
    private String Port ;          //發送郵件服務器的端口號
    private String SendAddress ;      //郵件發送者的地址
    private String ReceiveAddress ;     //郵件接受者的地址
    private String username ;        //登錄發送郵箱的用戶名
    private String password ;        //登錄發送郵箱的密碼
    private boolean isvalidate = true ;   //是否需要身份驗證
    private String subject ;          //郵件標題
    private String content ;        //郵件內容

    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 String getHost() {
      return Host;
    }
    public void setHost(String host) {
      Host = host;
    }
    public String getPort() {
      return Port;
    }
    public void setPort(String port) {
      Port = port;
    }
    public String getSendAddress() {
      return SendAddress;
    }
    public void setSendAddress(String sendAddress) {
      SendAddress = sendAddress;
    }
    public String getReceiveAddress() {
      return ReceiveAddress;
    }
    public void setReceiveAddress(String receiveAddress) {
      ReceiveAddress = receiveAddress;
    }
    public String getUsername() {
      return username;
    }
    public void setUsername(String username) {
      this.username = username;
    }
    public String getPassword() {
      return password;
    }
    public void setPassword(String password) {
      this.password = password;  
    }
    public boolean isIsvalidate() {
      return isvalidate;
    }
    public void setIsvalidate(boolean isvalidate) {
      this.isvalidate = isvalidate;
    }
    public Properties getProperties(){
      Properties properties = new Properties() ;
      properties.put("mail.smtp.host", this.Host) ;
      properties.put("mail.smtp.port", this.Port) ;
      properties.put("mail.smtp.auth", isvalidate?"true":"false") ;
      return properties ;
    }
}


 

 

2、寫一個  EmailMessage 封裝 發送信息

public class EmailMessage {
    private String title ;
    private String context ;
    private String toEmail ;

    public EmailMessage() {
      super();
    }
    public EmailMessage(String title, String context, String toEmail) {
      super();
      this.title = title;
      this.context = context;
      this.toEmail = toEmail;
    }
    public String getTitle() {
      return title;
    }
    public void setTitle(String title) {
      this.title = title;
    }
    public String getContext() {
      return context;
    }
    public void setContext(String context) {
      this.context = context;
    }
    public String getToEmail() {
      return toEmail;
    }
    public void setToEmail(String toEmail) {
      this.toEmail = toEmail;
    }
}


 

3、寫一個 MailAttorney  郵箱密碼驗證器 類

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

public class MailAttorney extends Authenticator {
  private String username ;
  private String password ;

  public MailAttorney(String username,String password) {
    this.username = username ;
    this.password = password ;
  }
  //覆蓋父類方法,獲取密碼認證器
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username,password) ;
  }
}


 

4 、 寫一個 MailSend  郵件工具類

import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.VTBBS.entity.EmailMessage;

public class MailSend {
    public static boolean mailTest(MailSendProper mailsender){
        MailAttorney attorney = null ;
        if(mailsender.isIsvalidate()){//判斷是否需要身份認證
          attorney = new MailAttorney(mailsender.getUsername(),mailsender.getPassword()) ;
        }
        //根據郵箱會話屬性和密碼驗證器構造一個發送郵箱的seesion
        Session session = Session.getInstance(mailsender.getProperties(),attorney) ;
        //根據session創建一個郵件消息
        Message mailMessage = new MimeMessage(session) ;
        try {
          //創建郵件發送者的地址
          Address from = new InternetAddress(mailsender.getSendAddress()) ;
          //設置郵箱消息的發送者
          mailMessage.setFrom(from);
          //創建郵件接收這的地址,並設置到郵件消息中
          Address to = new InternetAddress(mailsender.getReceiveAddress()) ;
          mailMessage.setRecipient(Message.RecipientType.TO, to);
          mailMessage.setSubject(mailsender.getSubject()); //設置郵件標題
          mailMessage.setSentDate(new Date()); //設置發送郵件時間
          mailMessage.setText(mailsender.getContent()); //設置郵件內容
          Transport.send(mailMessage);
          return true ;
        } catch (Exception e) {
          e.printStackTrace();
          return false ;
        }
    }

    public static boolean sendEmail(EmailMessage message){
        MailSendProper mail = new MailSendProper() ;
        mail.setHost("smtp.126.com");          //smtp簡單的郵件傳輸協議,默認端口號是25,
        mail.setPort("25");
        mail.setIsvalidate(true);              //需呀身份驗證
        mail.setUsername("");               //設置登錄用戶名 
        mail.setPassword("");               //設置發送者的密碼
        mail.setSendAddress("");             //設置發送這的地址  發送者地址和登錄用戶名是同一個
        mail.setReceiveAddress(message.getToEmail());  //設置接收者的地址
        mail.setSubject(message.getTitle());        //設置郵箱標題
        mail.setContent(message.getContext());      //設置郵箱的內容
        return mailTest(mail) ;
    }

}

注意:使用的郵箱必須開啟POP3/SMTP服務 才能發送成功,

   不同的郵箱,郵箱的傳輸協議不一樣如

        QQ郵箱:SMTP傳輸協議是 smtp.qq.com 端口 25

              POP3傳輸協議是 pop3.qq.com  端口 110


5、使用方法測試

public static void main(String[] args) {
  EmailMessage message = new EmailMessage() ;
  String code = String.valueOf(Math.random()).substring(3, 9) ; //生成驗證碼
  message.setTitle("郵箱驗證");                  //郵件標題
  message.setContext("尊敬的用戶你好,你的驗證碼為"+code+"。"); //郵件內容
  message.setToEmail("[email protected]"); //要發送給誰
  System.out.println(MailSend.sendEmail(message)?"發送成功":"發送失敗") ;
}

若要轉載,請標明此處

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved