JAVA在不基於XML配置檔案的情況下實現郵件傳送功能(郵箱轟炸)
阿新 • • 發佈:2018-11-14
今天要講的是如何用Java程式碼實現簡單郵件傳送和複雜郵件傳送的功能,這裡我使用的是QQ郵箱,你們也可以嘗試使用其他的郵箱哦~
想實現郵件傳送功能首先郵箱賬號必須要開啟 SMTP 服務,在網頁登入郵箱後點擊設定→賬戶然後下拉,如圖
點選右邊的 "開啟",然後會彈出這樣的提示
記下這一串號碼,待會就用這個號碼進行傳送郵件
郵件傳送的工具類在這~
package com.sixmai.mail; /** * Created by MaNongXF on 2018/10/30 . */ import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class MailUtil { private MimeMessage mimeMsg; //MIME郵件物件 private Session session; //郵件會話物件 private Properties props; //系統屬性 private boolean needAuth = false; //smtp是否需要認證 //smtp認證使用者名稱和密碼 private String username; private String password; private Multipart mp; //Multipart物件,郵件內容,標題,附件等內容均新增到其中後再生成MimeMessage物件 /** * Constructor * @param smtp 郵件傳送伺服器 */ public MailUtil(String smtp){ setSmtpHost(smtp); createMimeMessage(); } /** * 設定郵件傳送伺服器 * @param hostName String */ public void setSmtpHost(String hostName) { System.out.println("設定系統屬性:mail.smtp.host = "+hostName); if(props == null) props = System.getProperties(); //獲得系統屬性物件 props.put("mail.smtp.host",hostName); //設定SMTP主機 } /** * 建立MIME郵件物件 * @return */ public boolean createMimeMessage() { try { System.out.println("準備獲取郵件會話物件!"); session = Session.getDefaultInstance(props,null); //獲得郵件會話物件 } catch(Exception e){ System.err.println("獲取郵件會話物件時發生錯誤!"+e); return false; } System.out.println("準備建立MIME郵件物件!"); try { mimeMsg = new MimeMessage(session); //建立MIME郵件物件 mp = new MimeMultipart(); return true; } catch(Exception e){ System.err.println("建立MIME郵件物件失敗!"+e); return false; } } /** * 設定SMTP是否需要驗證 * @param need */ public void setNeedAuth(boolean need) { System.out.println("設定smtp身份認證:mail.smtp.auth = "+need); if(props == null) props = System.getProperties(); if(need){ props.put("mail.smtp.auth","true"); }else{ props.put("mail.smtp.auth","false"); } } /** * 設定使用者名稱和密碼 * @param name * @param pass */ public void setNamePass(String name,String pass) { username = name; password = pass; } /** * 設定郵件主題 * @param mailSubject * @return */ public boolean setSubject(String mailSubject) { System.out.println("設定郵件主題!"); try{ mimeMsg.setSubject(mailSubject); return true; } catch(Exception e) { System.err.println("設定郵件主題發生錯誤!"); return false; } } /** * 設定郵件正文 * @param mailBody String */ public boolean setBody(String mailBody) { try{ BodyPart bp = new MimeBodyPart(); bp.setContent(""+mailBody,"text/html;charset=GBK"); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("設定郵件正文時發生錯誤!"+e); return false; } } /** * 新增附件 * @param filename String */ public boolean addFileAffix(String filename) { System.out.println("增加郵件附件:"+filename); try{ BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(MimeUtility.encodeText(fileds.getName())); mp.addBodyPart(bp); return true; } catch(Exception e){ System.err.println("增加郵件附件:"+filename+"發生錯誤!"+e); return false; } } /** * 設定發信人 * @param from String */ public boolean setFrom(String from) { System.out.println("設定發信人!"); try{ mimeMsg.setFrom(new InternetAddress(from)); //設定發信人 return true; } catch(Exception e) { return false; } } /** * 設定收信人 * @param to String */ public boolean setTo(String to){ if(to == null)return false; try{ mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); return true; } catch(Exception e) { return false; } } /** * 設定抄送人 * @param copyto String */ public boolean setCopyTo(String copyto) { if(copyto == null)return false; try{ mimeMsg.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto)); return true; } catch(Exception e) { return false; } } /** * 傳送郵件 */ public boolean sendOut() { try{ mimeMsg.setContent(mp); mimeMsg.saveChanges(); System.out.println("正在傳送郵件...."); Session mailSession = Session.getInstance(props,null); Transport transport = mailSession.getTransport("smtp"); transport.connect((String)props.get("mail.smtp.host"),username,password); transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO)); //transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC)); //transport.send(mimeMsg); System.out.println("傳送郵件成功!"); transport.close(); return true; } catch(Exception e) { e.printStackTrace(); System.err.println("郵件傳送失敗!"+e); return false; } } /** * 呼叫sendOut方法完成郵件傳送 * @param smtp * @param from * @param to * @param subject * @param content * @param username * @param password * @return boolean */ public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password) { MailUtil theMailUtil = new MailUtil(smtp); theMailUtil.setNeedAuth(true); //需要驗證 if(!theMailUtil.setSubject(subject)) return false; if(!theMailUtil.setBody(content)) return false; if(!theMailUtil.setTo(to)) return false; if(!theMailUtil.setFrom(from)) return false; theMailUtil.setNamePass(username,password); if(!theMailUtil.sendOut()) return false; return true; } /** * 呼叫sendOut方法完成郵件傳送,帶抄送 * @param smtp * @param from * @param to * @param copyto * @param subject * @param content * @param username * @param password * @return boolean */ public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) { MailUtil theMailUtil = new MailUtil(smtp); theMailUtil.setNeedAuth(true); //需要驗證 if(!theMailUtil.setSubject(subject)) return false; if(!theMailUtil.setBody(content)) return false; if(!theMailUtil.setTo(to)) return false; if(!theMailUtil.setCopyTo(copyto)) return false; if(!theMailUtil.setFrom(from)) return false; theMailUtil.setNamePass(username,password); if(!theMailUtil.sendOut()) return false; return true; } /** * 呼叫sendOut方法完成郵件傳送,帶附件 * @param smtp * @param from * @param to * @param subject * @param content * @param username * @param password * @param filename 附件路徑 * @return */ public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password,String filename) { MailUtil theMailUtil = new MailUtil(smtp); theMailUtil.setNeedAuth(true); //需要驗證 if(!theMailUtil.setSubject(subject)) return false; if(!theMailUtil.setBody(content)) return false; if(!theMailUtil.addFileAffix(filename)) return false; if(!theMailUtil.setTo(to)) return false; if(!theMailUtil.setFrom(from)) return false; theMailUtil.setNamePass(username,password); if(!theMailUtil.sendOut()) return false; return true; } /** * 呼叫sendOut方法完成郵件傳送,帶附件和抄送 * @param smtp * @param from * @param to * @param copyto * @param subject * @param content * @param username * @param password * @param filename * @return */ public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password,String filename) { MailUtil theMailUtil = new MailUtil(smtp); theMailUtil.setNeedAuth(true); //需要驗證 if(!theMailUtil.setSubject(subject)) return false; if(!theMailUtil.setBody(content)) return false; if(!theMailUtil.addFileAffix(filename)) return false; if(!theMailUtil.setTo(to)) return false; if(!theMailUtil.setCopyTo(copyto)) return false; if(!theMailUtil.setFrom(from)) return false; theMailUtil.setNamePass(username,password); if(!theMailUtil.sendOut()) return false; return true; } /** * 傳送郵件,含附件 * @param to * @param subject * @param content * @param filename * @return */ public static boolean send(String to,String subject,String content,String filename) { // MailUtil theMailUtil = new MailUtil(SkxxConfig.get("MAIL_SMTP")); // theMailUtil.setNeedAuth(true); //需要驗證 // // if(!theMailUtil.setSubject(subject)) return false; // if(!theMailUtil.setBody(content)) return false; // if(filename!=null) { // if (!theMailUtil.addFileAffix(filename)) return false; // } // if(!theMailUtil.setTo(to)) return false; // if(!theMailUtil.setFrom(SkxxConfig.get("MAIL_FROM"))) return false; // theMailUtil.setNamePass(SkxxConfig.get("MAIL_USERNAME"),SkxxConfig.get("MAIL_PASSWORD")); // // if(!theMailUtil.sendOut()) return false; return true; } public static void main(String[] args){ String smtp = "smtp.qq.com";//qq郵箱使用這個,類似,163郵箱使用 smtp.163.com String from = "
[email protected]";//發件人 String to = "[email protected]";//收件人 String copyto = "";//抄送人 String subject = "面試邀請";//主題 String content = "hello :";//內容 String username="[email protected]";//你的郵箱 String password="剛剛記下的那串密碼";//密碼 // String filename = "d:\\about.html"; while (true){ MailUtil.send(smtp, from, to, subject, content, username, password); } } }
為了便於大家更快的上手,直接可以在最後面的main方法裡測試(複雜郵件功能大家自己看看裡面的其他方法即可)~
在send方法後面還可以增加一個檔案引數,由於我沒傳,所以註釋起來了
傳送成功的結果如下
這就是傳送成功啦~
於是我添加了這麼一段程式碼。。。
至於結果如何,大家自由發揮想象~
The end!!!