1. 程式人生 > >java連線163郵箱傳送郵件

java連線163郵箱傳送郵件

一:jar包:
image
下載連結:連結: http://pan.baidu.com/s/1dDhIDLv 密碼: ibg5
二:程式碼

1----------------------------------------------------------------------------------
package com.No008.shop.framework.common.dic.mail.mail163;
import java.util.Properties;
/**
 * 主題:簡單郵件(不帶附件的郵件)傳送
 * @author 劉軍/shell_liu
 * 2015-4-12
 */
public class
MailSenderInfo { //傳送郵件的伺服器的IP和埠 private String mailServerHost; private String mailServerPort="25"; //郵件傳送者的地址 private String fromAddress; //郵件接收者的地址 private String toAddress; //登陸郵件傳送伺服器的使用者名稱和密碼 private String userName; private String password; //是否需要身份驗證 private
boolean validate=false; //郵件傳送主題 private String subject; //郵件的文字內容 private String content; //郵件附件的檔名 private String[] attachFileNames; /** * 獲得郵件會話屬性 */ public Properties getProperties(){ Properties p=new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port
", this.mailServerPort); p.put("mail.smtp.auth", validate?"true":"false"); return p;} public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } 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 isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } 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[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] attachFileNames) { this.attachFileNames = attachFileNames; } } 2--------------------------------------------------------------------------------------------------------------------------------------- package com.No008.shop.framework.common.dic.mail.mail163; import javax.mail.*; /** * 主題:簡單郵件(不帶附件的郵件)傳送 * @author 劉軍/shell_liu * 2015-4-12 */ public class MyAuthenticator extends Authenticator{ String userName=null; String password=null; public MyAuthenticator( ) { } public MyAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } } 3--------------------------------------------------------------------------------------------------------------------------- package com.No008.shop.framework.common.dic.mail.mail163; /** * 主題:簡單郵件(不帶附件的郵件)傳送 * @author 劉軍/shell_liu * 2015-4-12 */ public class SendMail { public static void main(String[] args) { SendMail.send_163(); } //163郵箱 public static void send_163() { //這個類主要是設定郵件 MailSenderInfo mailInfo=new MailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("[email protected]");//實際傳送者 mailInfo.setPassword("密碼");//您的郵箱密碼 mailInfo.setFromAddress("[email protected]");//設定傳送人郵箱地址 mailInfo.setToAddress("[email protected]"); mailInfo.setSubject("test");//設定郵箱標題 mailInfo.setContent("<b>test</b>");//設定郵箱內容 //這個類主要是用來發送郵件 SimpleMailSender sms=new SimpleMailSender(); sms.sendTextMail(mailInfo);//傳送文字格式 sms.sendHtmlMail(mailInfo);//傳送html格式:如果需要以html格式傳送則需要處理好附件上傳地址問題 } } 4---------------------------------------------------------------------------------------------------------------------- package com.No008.shop.framework.common.dic.mail.mail163; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /** * 主題:簡單郵件(不帶附件的郵件)傳送 * @author 劉軍/shell_liu * 2015-4-12 */ public class SimpleMailSender { /** * 以文字格式傳送郵件 * @param mailInfo * 代傳送的郵件的資訊 */ public boolean sendTextMail(MailSenderInfo mailInfo){ //判斷是否需要身份認證 MyAuthenticator authenticator=null; Properties pro=mailInfo.getProperties(); if(mailInfo.isValidate()){ //如果需要身份認證;則建立一個密碼驗證器 authenticator=new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } //根據郵件會話屬性和密碼驗證器構造一個郵件傳送的session Session sendMailSession=Session.getDefaultInstance(pro, authenticator); try { // 根據session 建立一個郵件訊息 Message mailMessage=new MimeMessage(sendMailSession); //建立郵件傳送者地址 Address from =new InternetAddress(mailInfo.getFromAddress()); //設定郵件訊息的傳送者 mailMessage.setFrom(from); //建立郵件的接受者地址;並設定到郵件訊息中 Address to=new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); //設定郵件訊息的主題 mailMessage.setSubject(mailInfo.getSubject()); //設定郵件訊息傳送的時間 mailMessage.setSentDate(new Date()); //設定郵件訊息的主要內容 String mailContent =mailInfo.getContent(); mailMessage.setText(mailContent); //傳送郵件 Transport.send(mailMessage); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /*** * 以HTML格式傳送郵件 * @param mailInfo * 待發送的郵件資訊 */ public boolean sendHtmlMail(MailSenderInfo mailInfo){ //判斷是否需要身份認證 MyAuthenticator authenticator=null; Properties pro=mailInfo.getProperties(); //如果需要身份認證;則建立一個密碼驗證器 if(mailInfo.isValidate()){ authenticator=new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } //根據郵件會話屬性和密碼驗證器構造一個郵件傳送的session Session sendMailSession=Session.getDefaultInstance(pro, authenticator); try { // 根據session 建立一個郵件訊息 Message mailMessage=new MimeMessage(sendMailSession); //建立郵件傳送者地址 Address from =new InternetAddress(mailInfo.getFromAddress()); //設定郵件訊息的傳送者 mailMessage.setFrom(from); //建立郵件的接受者地址;並設定到郵件訊息中 Address to=new InternetAddress(mailInfo.getToAddress()); //Message.RecipientType.TO表示接收者的型別為TO mailMessage.setRecipient(Message.RecipientType.TO, to); //設定郵件訊息的主題 mailMessage.setSubject(mailInfo.getSubject()); //設定郵件訊息傳送的時間 mailMessage.setSentDate(new Date()); //設定郵件訊息的主要內容 //MiniMultipart 類是一個容器 包含MimeBodyPart型別的物件 Multipart mailPart =new MimeMultipart(); //建立一個包含HTNL內容的MimeBodyPart BodyPart html=new MimeBodyPart(); //設定HTML內容 html.setContent(mailInfo.getContent(),"text/html;charset=utf-8"); mailPart.addBodyPart(html); //s設定信件的附件(用本地上的檔案作為附件) /** html=new MimeBodyPart(); FileDataSource fds=new FileDataSource(""); DataHandler dh=new DataHandler(fds); html.setFileName(""); html.setDataHandler(dh); mailPart.addBodyPart(html); **/ //將MiniMultipart物件設定為郵件內容 mailMessage.setContent(mailPart); mailMessage.saveChanges(); //傳送郵件 Transport.send(mailMessage); return true; } catch (Exception e) { e.printStackTrace(); } return false; } }