瑕佸寘鍚殑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鏈嶅姟
鐐瑰嚮寮€鍚嵆鍙紝寮€鍚悗浼氱敓鎴愪竴涓巿鏉冪爜锛屼綘濡傛灉蹇樹簡涔熸病浜嬨€?
浣嗘槸163閭濂藉儚灏變竴涓巿鏉冪爜锛岃鐗㈣