天天看點

JAVA代碼 發送各種電子郵件(包括QQ郵件,163郵件,sohu郵件等)含詳細注解

瑕佸寘鍚殑jar鍖呭寘鎷琺ailapi.jar,smpt.jar,activation.jar

package cn.itcast.javamail2;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Demo2 {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		Properties props=new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.163.com");//鎸囧畾閭鏈嶅姟鍣ㄥ悕绉闆拰绔彛,绔彛鍙互鏀逛負stmp.qq.com绛夌瓑锛屽彧瑕佽窡浣犵櫥褰曠殑閭涓€鏍峰氨琛?		Session session=Session.getInstance(props,      //session鍦ㄩ渶瑕佺敤鎴峰悕鍜屽瘑鐮佹槸浼氳皟鐢ㄨ繖涓狝uthenticator绫?				new Authenticator() {                    //涓€涓被,瑕嗙洊浜嗙埗绫繪柟娉?					protected PasswordAuthentication getPasswordAuthentication(){//鍙璋冪敤杩欎釜绫葷殑鏂規硶锛屽氨浼氳繑鍥濸asswordAuthentication瀵矽薄锛屽寘鍚簡鐢ㄦ埛鍚嶅拰瀵嗙爜,杩欓噷瑕佹敞鎰忥紝浣犻鍏堣鍘誨紑鍚閭鐨刾op3鍜宻mtp鏈嶅姟锛屽熀鏈笂鍦ㄨ缃腑閮借兘鎵懼埌锛岀劧鍚庝細缁欎綘涓€涓巿鏉冪爜锛屼綘鍦ㄨ繖閲岃緭鍏ョ殑鐨勫瘑鐮佹槸閭d釜鎺堟潈鐮?						return new PasswordAuthentication("[email聽protected]", "test1mima");
					}
				}
		);//鍒涘緩session
		Message msg=new MimeMessage(session);
		msg.setFrom(new InternetAddress("[email聽protected]"));//鍙戜歡浜洪偖绠憋紝鍐欎笂闈㈢殑璐﹀彿
		msg.setSubject("涓枃涓婚");//涓婚
		msg.setRecipients(RecipientType.TO,InternetAddress.parse("[email聽protected],[email聽protected]") );//鎸囧畾澶氫釜鏀朵歡浜? RecipientType.TO鍙戦€侊紝RecipientType.CC鎶勯€侊紝RecipientType.BCC鏆楅€?		msg.setContent("<span style='color:red'>SJR甯呯殑涓€绗?lt;/span>","text/html;charset=gbk");//鍙戦€佺殑鍐呭锛岋紙浠ヤ粈涔堢被鍨嬫樉绀猴紝瀛楃闆嗙瓑绛夛級
		Transport.send(msg);		
	}

}
           

浠Q閭涓轟緥寮€鍚疨OP3鏈嶅姟鍜孲TMP鏈嶅姟

JAVA代碼 發送各種電子郵件(包括QQ郵件,163郵件,sohu郵件等)含詳細注解

鐐瑰嚮寮€鍚嵆鍙紝寮€鍚悗浼氱敓鎴愪竴涓巿鏉冪爜锛屼綘濡傛灉蹇樹簡涔熸病浜嬨€?

JAVA代碼 發送各種電子郵件(包括QQ郵件,163郵件,sohu郵件等)含詳細注解

浣嗘槸163閭濂藉儚灏變竴涓巿鏉冪爜锛岃鐗㈣