用intellijidea編寫java的發(fā)送郵件,接收郵件,發(fā)送帶附件的郵件_第1頁
用intellijidea編寫java的發(fā)送郵件,接收郵件,發(fā)送帶附件的郵件_第2頁
用intellijidea編寫java的發(fā)送郵件,接收郵件,發(fā)送帶附件的郵件_第3頁
用intellijidea編寫java的發(fā)送郵件,接收郵件,發(fā)送帶附件的郵件_第4頁
用intellijidea編寫java的發(fā)送郵件,接收郵件,發(fā)送帶附件的郵件_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、廣西民族大學(xué)實 驗 報 告學(xué)院: 班級 11信計 姓名 學(xué)號 分數(shù) 實驗日期 2014.5.11 指導(dǎo)老師 秦董洪 實驗名稱 用java實現(xiàn)郵件的發(fā)送和接收 實驗內(nèi)容:編寫一個發(fā)送和接收郵件信息的郵件程序,包括郵件中附件的發(fā)送與接收。實驗所用軟件:Intellij idea 13.0實驗過程:(1).在D盤下新建兩個文本文件,分別是sendEmail.txt和receiveEmail.txt,在這兩個文件里加上兩行數(shù)據(jù),第一行是郵箱地址,第二行是密碼.前者是記錄著發(fā)送郵件的郵箱地址與郵箱密碼,后者記錄著查看接收郵件的郵箱地址與密碼(2).打開Intellij idea,按FileNew mod

2、ule,新建一個java模塊,命名為Email.如下圖所示:(3).在Intellij idea的project面板窗口里,雙擊展開Email,右鍵單擊它的子文件夾“src”,新建一個”java”文件,命名為“Email.java”(4).編寫出程序的實現(xiàn)代碼/EmailHandler.javaimport javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.*;import ernet.InternetAddress;import javax.m

3、ernet.MimeBodyPart;import ernet.MimeMessage;import ernet.MimeMultipart;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.InputStreamReader;import java.util.Date;import java.util.Properties;class EmailHandler private static S

4、tring server = ; private static int port = 25; private String pop3 = ; public void sendEmail(String emailFrom, String emailFromPassword, String emailTo, String emailSubject, String body, String fileName) try File f = new File(fileName); String fName = f.getName(); Properties props = new Properties()

5、; props.put(mail.smtp.host, server); props.put(mail.smtp.port, String.valueOf(port); props.put(mail.smtp.auth, true); Transport transport = null; Session session = Session.getDefaultInstance(props, null); transport = session.getTransport(smtp); transport.connect(server, emailFrom, emailFromPassword)

6、; MimeMessage msg = new MimeMessage(session); msg.setSentDate(new Date(); InternetAddress fromAddress = new InternetAddress(emailFrom); msg.setFrom(fromAddress); InternetAddress toAddress = new InternetAddress1; toAddress0 = new InternetAddress(emailTo); msg.setRecipients(Message.RecipientType.TO, t

7、oAddress); msg.setSubject(emailSubject, UTF-8); MimeMultipart multi = new MimeMultipart(); BodyPart textBodyPart = new MimeBodyPart(); /第一個BodyPart.主要寫一些一般的信件內(nèi)容。 textBodyPart.setText(body);/ 壓入第一個BodyPart到MimeMultipart對象中。 multi.addBodyPart(textBodyPart); FileDataSource fds = new FileDataSource(file

8、Name); /必須存在的文檔,否則throw異常。 BodyPart fileBodyPart = new MimeBodyPart(); /第二個BodyPart fileBodyPart.setDataHandler(new DataHandler(fds); /字符流形式裝入文件 fileBodyPart.setFileName(fName); /設(shè)置文件名,可以不是原來的文件名。 multi.addBodyPart(fileBodyPart);/ MimeMultPart作為Content加入message msg.setContent(multi); msg.setFileName

9、(fileName); msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients(); catch (NoSuchProviderException e) e.printStackTrace(); catch (MessagingException e) e.printStackTrace(); public void sendEmail(String emailFrom, String emailFromPassword, String emailTo, String emailSubject, String bod

10、y) try Properties props = new Properties(); props.put(mail.smtp.host, server); props.put(mail.smtp.port, String.valueOf(port); props.put(mail.smtp.auth, true); Transport transport = null; Session session = Session.getDefaultInstance(props, null); transport = session.getTransport(smtp); transport.con

11、nect(server, emailFrom, emailFromPassword); MimeMessage msg = new MimeMessage(session); msg.setSentDate(new Date(); InternetAddress fromAddress = new InternetAddress(emailFrom); msg.setFrom(fromAddress); InternetAddress toAddress = new InternetAddress1; toAddress0 = new InternetAddress(emailTo); msg

12、.setRecipients(Message.RecipientType.TO, toAddress); msg.setSubject(emailSubject, UTF-8); msg.setText(body); transport.sendMessage(msg, msg.getAllRecipients(); catch(Exception e) e.printStackTrace(); public void receiveEmail(String pop3, String user, String password) throws Exception Properties prop

13、s = System.getProperties(); Session session = Session.getInstance(props, null); Store store = session.getStore(pop3); store.connect(pop3, user, password); Folder folder = store.getFolder(INBOX); folder.open(Folder.READ_WRITE); Message msg = folder.getMessages(); for (int i = 0; i msg.length; i+) Sys

14、tem.out.println(來自: + msgi.getFrom()0); System.out.println(標題: + msgi.getSubject(); System.out.println(內(nèi)容: + msgi.getContent() + n); if(msgi.getFrom()0).toString().equalsIgnoreCase String s; BufferedReader in = new BufferedReader(new InputStreamReader(msgi.getInputStream(); System.out.p

15、rintln(-Mail Text-); while(!(s=in.readLine().equals() System.out.println(s); in.close(); folder.close(false); store.close(); public String getUserInputFromCMD(String s) throws Exception System.out.println(s); BufferedReader br = new BufferedReader(new InputStreamReader(System.in); String b; b = br.r

16、eadLine(); String userInput = b.trim(); return userInput; public String getUserInputFromTxt(File file) throws Exception FileReader fr =new FileReader(file); BufferedReader br = new BufferedReader(fr); String data; String userInfo = new String2; int i = 0; while(data = br.readLine() != null) userInfo

17、i = data; i+; return userInfo; public void chooseOperation(EmailHandler iMailMan) throws Exception System.out.println(請選擇你要執(zhí)行的操作:1.發(fā)送帶附件郵件 2.發(fā)送沒有攜帶附件的郵件 3.接收郵件 4.退出); BufferedReader br = new BufferedReader(new InputStreamReader(System.in); char a = (char) br.read(); switch (a) case 1 : String emailT

18、o = iMailMan.getUserInputFromCMD(請輸入收件人的郵箱: ); String emailSubject = iMailMan.getUserInputFromCMD(請輸入郵件的標題: ); String emailContent = iMailMan.getUserInputFromCMD(請輸入郵件的內(nèi)容: ); String fileName = iMailMan.getUserInputFromCMD(請輸入附件的路徑,路徑中的請加上轉(zhuǎn)義字符: ); /從文件里獲取郵箱的賬戶和密碼 String userInfo = iMailMan.getUserInp

19、utFromTxt(new File(D:sendEmail.txt); String emailFrom = userInfo0; String emailFromPassword = userInfo1; iMailMan.sendEmail(emailFrom, emailFromPassword, emailTo, emailSubject, emailContent, fileName); System.out.println(郵件發(fā)送成功!); iMailMan.chooseOperation(iMailMan); break; case 2 : String emailTo =

20、iMailMan.getUserInputFromCMD(請輸入收件人的郵箱: ); String emailSubject = iMailMan.getUserInputFromCMD(請輸入郵件的標題: ); String emailContent = iMailMan.getUserInputFromCMD(請輸入郵件的內(nèi)容: ); /從文件里獲取郵箱的賬戶和密碼 String userInfo = iMailMan.getUserInputFromTxt(new File(D:sendEmail.txt); String emailFrom = userInfo0; String emailFromPassword = userInfo1; iMailMan.sendEmail(emailFrom, emailFromPassword, emailTo, emailSubject

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論