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

Java應用JavaMail發送郵件的辦法

編輯:關於JAVA

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


本文實例講述了Java應用JavaMail發送郵件的辦法。分享給年夜家供年夜家參考,詳細以下:

代碼1、Email_Autherticator.java 辦事器驗證代碼

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
  String username = "你郵箱的用戶名";
  String password = "你郵箱的暗碼";
  public Email_Autherticator() {
 super();
  }
  public Email_Autherticator(String user,String pwd){
 super();
 username = user;
 password = pwd;
  }
  public PasswordAuthentication getPasswordAuthentication(){
 return new PasswordAuthentication(username,password);
  }
}

代碼2、Mail.java 發送郵件的代碼

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
  private String host = "smtp.126.com";
  private String mail_head_name = "this is head of this mail";
  private String mail_head_value = "this is head of this mail";
  private String mail_to = "[email protected]";
  private String mail_from = "[email protected]";
  private String mail_subject = "this is the subject of this test mail";
  private String mail_body = "this is mail_body of this test mail";
  private String personalName = "我的郵件";
  public void sendMail() throws SendFailedException{
 try {
   Properties props = new Properties();//獲得體系情況
   Authenticator auth = new Email_Autherticator();//停止郵件辦事用戶認證
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.auth", "true");
   System.out.println(props);
   Session session = Session.getDefaultInstance(props,auth);
   //設置session,和郵件辦事器停止通信
   MimeMessage message = new MimeMessage(session);
   message.setContent("Hello","text/plain");//設置郵件格局
   message.setSubject(mail_subject);//設置郵件主題
   message.setText(mail_body);//設置郵件內容
   message.setHeader(mail_head_name, mail_head_value);//設置郵件題目
   message.setSentDate(new Date());//設置郵件發送時代
   Address address = new InternetAddress(mail_from,personalName);
   message.setFrom(address);//設置郵件發送者的地址
   Address toaddress = new InternetAddress(mail_to);//設置郵件吸收者的地址
   message.addRecipient(Message.RecipientType.TO,toaddress);
   System.out.println(message);
   Transport.send(message);
   System.out.println("Send Mail Ok!");
 } catch (Exception e) {
   e.printStackTrace();
 }
 //return flag;
  }
}

代碼3、Test.java 測試發送郵件的代碼

public class Test {
  public static void main(String[] args) {
 Mail m = new Mail();
 try {
   m.sendMail();
 } catch (Exception e) {
 }
  }
}

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

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