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

java郵件發送的完成

編輯:關於JAVA

java郵件發送的完成。本站提示廣大學習愛好者:(java郵件發送的完成)文章只能為提供參考,不一定能成為您想要的結果。以下是java郵件發送的完成正文


郵件是在平常生涯中,這篇文章重要引見了java完成基於SMTP發送郵件的辦法,實例剖析了java基於SMTP辦事發送郵件的相干技能,具有必定參考自創價值,須要的同伙可以參考下.

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)?"發送勝利":"發送掉敗") ;
}

願望本文所述對年夜家進修java法式設計有所贊助。

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