發送郵件依賴一個jar包 javax.mail.jar 包 用之前先把包導入http://files.VEVb.com/files/wenjie123/javax.mail.jar.rarpackage com.svse;import java.io.File;/* *建立 附件類,只有文件,即附件才文件名 */public class AttachBean { PRivate String cid; private File file; private String fileName; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public AttachBean() { } public AttachBean(File file, String fileName) { super(); this.file = file; this.fileName = fileName; }}
package com.svse;import java.util.ArrayList;import java.util.List;/** * 表示郵件類,你需要設置:賬戶名和密碼、收件人、抄送(可選)、暗送(可選)、主題、內容,以及附件(可選) * * 在創建了Mail對象之后 * 可以調用它的setSubject()、setContent(),設置主題和正文 * 也可以調用setFrom()和 addToAddress(),設置發件人,和添加收件人。 * 也可以調用addAttch()添加附件 * 創建AttachBean:new AttachBean(new File("..."), "fileName"); */public class Mail { private String from;//發件人 private StringBuilder toAddress = new StringBuilder();//收件人 private StringBuilder ccAddress = new StringBuilder();//抄送 private StringBuilder bccAddress = new StringBuilder();//暗送 private String subject;//主題 private String content;//正文 // 附件列表 private List<AttachBean> attachList = new ArrayList<AttachBean>(); public Mail() {} public Mail(String from, String to) { this(from, to, null, null); } public Mail(String from, String to, String subject, String content) { this.from = from; this.toAddress.append(to); this.subject = subject; this.content = content; } /** * 返回發件人 * @return */ public void setFrom(String from) { this.from = from; } /** * 返回發件人 * @return */ public String getFrom() { return from; } /** * 返回主題 */ public String getSubject() { return subject; } /** * 設置主題 */ public void setSubject(String subject) { this.subject = subject; } /** * 獲取主題內容 */ public String getContent() { return content; } /** * 設置主題內容 * @param content */ public void setContent(String content) { this.content = content; } /** * 獲取收件人 * @return */ public String getToAddress() { return toAddress.toString(); } /** * 獲取抄送 * @return */ public String getCcAddress() { return ccAddress.toString(); } /** * 獲取暗送 * @return */ public String getBccAddress() { return bccAddress.toString(); } /** * 添加收件人,可以是多個收件人 * @param to */ public void addToAddress(String to) { if(this.toAddress.length() > 0) { this.toAddress.append(","); } this.toAddress.append(to); } /** * 添加抄送人,可以是多個抄送人 * @param cc */ public void addCcAddress(String cc) { if(this.ccAddress.length() > 0) { this.ccAddress.append(","); } this.ccAddress.append(cc); } /** * 添加暗送人,可以是多個暗送人 * @param bcc */ public void addBccAddress(String bcc) { if(this.bccAddress.length() > 0) { this.bccAddress.append(","); } this.bccAddress.append(bcc); } /** * 添加附件,可以添加多個附件 * @param attachBean */ public void addAttach(AttachBean attachBean) { this.attachList.add(attachBean); } /** * 獲取所有附件 * @return */ public List<AttachBean> getAttachs() { return this.attachList; }}
package com.svse;import java.io.IOException;import java.util.List;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.MessagingException;import javax.mail.PassWordAuthentication;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;import javax.mail.internet.MimeUtility;import javax.mail.internet.MimeMessage.RecipientType;/** * 建立郵件工具類 發送郵件 * @author 00 * */public class MailUtils { public static Session createSession(String host, final String username, final String password) { Properties prop = new Properties(); prop.setProperty("mail.host", host);// 指定主機 prop.setProperty("mail.smtp.auth", "true");// 指定驗證為true // 創建驗證器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }; // 獲取session對象 return Session.getInstance(prop, auth); } /** * 發送指定的郵件 * * @param mail */ public static void send(Session session, final Mail mail) throws MessagingException, IOException { MimeMessage msg = new MimeMessage(session);// 創建郵件對象 msg.setFrom(new InternetAddress(mail.getFrom()));// 設置發件人 msg.addRecipients(RecipientType.TO, mail.getToAddress());// 設置收件人 // 設置抄送 String cc = mail.getCcAddress(); if (!cc.isEmpty()) { msg.addRecipients(RecipientType.CC, cc); } // 設置暗送 String bcc = mail.getBccAddress(); if (!bcc.isEmpty()) { msg.addRecipients(RecipientType.BCC, bcc); } msg.setSubject(mail.getSubject());// 設置主題 MimeMultipart parts = new MimeMultipart();// 創建部件集對象 MimeBodyPart part = new MimeBodyPart();// 創建一個部件 part.setContent(mail.getContent(), "text/html;charset=utf-8");// 設置郵件文本內容 parts.addBodyPart(part);// 把部件添加到部件集中 /////////////////////////////////////////// // 添加附件 List<AttachBean> attachBeanList = mail.getAttachs();// 獲取所有附件 if (attachBeanList != null) { for (AttachBean attach : attachBeanList) { MimeBodyPart attachPart = new MimeBodyPart();// 創建一個部件 attachPart.attachFile(attach.getFile());// 設置附件文件 attachPart.setFileName(MimeUtility.encodeText(attach .getFileName()));// 設置附件文件名 String cid = attach.getCid(); if(cid != null) { attachPart.setContentID(cid); } parts.addBodyPart(attachPart); } } msg.setContent(parts);// 給郵件設置內容 Transport.send(msg);// 發郵件 }}
package com.wenjie;import java.io.IOException;import javax.mail.MessagingException;import javax.mail.Session;import cn.itcast.mail.Mail;import cn.itcast.mail.MailUtils;public class MailTest { public static void main(String args[]){ String host = "smtp.163.com";//網易163郵件傳輸協議 騰訊 QQ的是smtp.qq.com /** * 這里需要注意一下 如果你想用qq郵箱作為發件人郵箱的話 記得把郵箱傳輸協議host值改為smtp.qq.com * 另外 username登陸名還是 一樣 直接寫QQ號,不用加后綴 */ String username = "用戶名>不加后綴";//發件人郵箱的用戶名 這里不要加后綴@163.com /** 注意事項 * 如果是用的QQ郵箱的話 這里的password不能寫QQ郵箱的登陸密碼 你要去登錄到QQ郵箱 點 設置>賬戶 下面會有一個"POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務" * 選項,把"POP3/SMTP服務" 開啟來 這時候 如果你之前沒開啟過 那么會提示你 設置獨立密碼 設置完成后 password的值就寫你剛才設置的獨立密碼即可 ,否則會驗證失敗 * 如果你用的是163或者126的話 就直接寫登陸密碼即可 */ String password = "密碼"; //發件人郵箱的登陸密碼 /** * 這里發件人 要寫全名 */ String from = "郵箱全名";//發件人的郵箱 全名 加后綴 /** * 收件人 同樣要寫全名 */ String to = "收件人的郵箱";//收件人的郵箱 /** * 主題自定義 */ String subject = "郵件測試";//郵件主題 /** * 自定義 */ String content = "http://http://www.49028c.com/wenjie123";//郵件的內容 /** * 調用寫好的郵件幫助類 MailUtils 直接調用createSession 根據以上(host, username, password三個參數)創建出session */ Session session = MailUtils.createSession(host, username, password); /** * 創建郵件對象from, to,subject,content 這三個參數 */ Mail mail = new Mail(from, to,subject,content); try { /** * 最后一步 調用MailUtils的send方法 將session和創建好的郵件對象傳進去 發送就ok了 */ MailUtils.send(session, mail); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
新聞熱點
疑難解答