本文實例講解了java發郵件的詳細過程,供大家參考,具體內容如下
1、郵件協議
發郵件的:SMTP (Simple Mail Transport Protocal)
收郵件的協議:pop3 (Post Office Protocal 3)
IMAP 新協議發郵件也可以收郵件。
(一步步的與服務器交互)
SMTP :
2、收發郵件的過程:
一般情況下,smtp和pop3是兩個服務器(主機)。
Smtp郵件的端口為25。
POP3 端口為110。
發郵件示例
1)、安裝foxmail:
2)、發郵件時,要對用戶名和密碼進行base64編碼
//對用戶名和密碼進行base64編碼@Test public void base64(){ String name = "wj_leaf12345"; String pwd = "1qaz2wsx"; BASE64Encoder en = new BASE64Encoder(); name = en.encode(name.getBytes()); pwd = en.encode(pwd.getBytes()); System.err.println(name); System.err.println(pwd); }3)、通過java代碼發郵件
用java發郵件,必須要導入新的包
mail.jar 發郵件的核心包
activation.jar 對用戶和密碼加密.
在mail.jar中有三個核心類:
Javax.mail.Session 是指與郵件服務器會話。整個項目中只要一個就可以了.
Javax.mail.Message(接口)―準備發送數據信息。
MimeMessage - 可以設置類型的數據信息。
Transport 它擁有一個方法可以發送Message。
第一步:導入兩個jar包
第二步:發簡單的郵件
public void sendMail() throws Exception{ //第一步:聲明properties對象放信息Properties prop = new Properties(); //設置連接哪一台服務器prop.setProperty("mail.host","smtp.126.com"); //設置是否驗證prop.setProperty("mail.smtp.auth", "true"); //第二步:聲明用戶名和密碼Authenticator auth = new Authenticator() { //此訪求返回用戶和密碼的對象public PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication pa = new PasswordAuthentication("aaa", "sss"); return pa; } }; ////第二步:獲取Session對象Session session = Session.getDefaultInstance(prop,auth); //設置session的調試模式session.setDebug(true); //第三步:聲明信息MimeMessage mm1 = new MimeMessage(session); //第四步:設置發件人email Address from = new InternetAddress("[email protected]"); mm1.setFrom(from); //第五步:設置收件人mm1.setRecipient(RecipientType.TO,new InternetAddress("[email protected]")); mm1.setRecipient(RecipientType.CC, new InternetAddress("[email protected]")); mm1.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]")); //第六步:設置主題mm1.setSubject("這是用Java發的郵件3"); mm1.setContent("你好,這是用java發的郵件,3333再試一下", "text/plain;charset=UTF-8"); //第七步: Transport.send(mm1); }第三步:v髮帶有超連接的郵件
mm1.setSubject("這是用Java發的郵件sfasdf3"); mm1.setContent("你好,這是用java發的郵件,<a href='http://www.baidu.com'>百度</a>", "text/html;charset=UTF-8" ); //第七步: Transport.send(mm1);第四步:符件的郵件
public void sendFile() throws Exception{ Properties p = new Properties(); p.setProperty("mail.host","smtp.163.com"); p.setProperty("mail.smtp.auth","true"); Session s = Session.getDefaultInstance(p,new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("ww", "123"); } }); s.setDebug(true); //聲明MimeMessage MimeMessage msg = new MimeMessage(s); msg.setFrom(new InternetAddress("[email protected]")); msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); msg.setSubject("圖片的"); //第一步:聲明多處理的Part MimeMultipart mm = new MimeMultipart(); //第二步:聲明MimeBodyPart body1 = new MimeBodyPart(); //第三步:設置符件DataSource ds = new FileDataSource(new File("./img/a.jpg")); DataHandler dh = new DataHandler(ds); body1.setDataHandler(dh); //必須要設置名稱body1.setFileName(MimeUtility.encodeText("美女.jpg")); MimeBodyPart body2 = new MimeBodyPart(); //第三步:設置符件DataSource ds2 = new FileDataSource(new File("./img/b.jpg")); DataHandler dh2 = new DataHandler(ds2); body2.setDataHandler(dh2); //必須要設置名稱body2.setFileName(MimeUtility.encodeText("美女2.jpg")); MimeBodyPart body3 = new MimeBodyPart(); //第三步:設置符件DataSource ds3 = new FileDataSource(new File("./img/m.mp3")); DataHandler dh3 = new DataHandler(ds3); body3.setDataHandler(dh3); //必須要設置名稱body3.setFileName(MimeUtility.encodeText("世紀末.mp3")); //將body1添加到mm mm.addBodyPart(body1); mm.addBodyPart(body2); mm.addBodyPart(body3); msg.setContent(mm); //發送Transport.send(msg); }以上就是本文的全部內容,希望對大家的學習有所幫助。