




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、广西民族大学实 验 报 告学院: 班级 11信计 姓名 学号 分数 实验日期 2014.5.11 指导老师 秦董洪 实验名称 用java实现邮件的发送和接收 实验内容:编写一个发送和接收邮件信息的邮件程序,包括邮件中附件的发送与接收。实验所用软件:Intellij idea 13.0实验过程:(1).在D盘下新建两个文本文件,分别是sendEmail.txt和receiveEmail.txt,在这两个文件里加上两行数据,第一行是邮箱地址,第二行是密码.前者是记录着发送邮件的邮箱地址与邮箱密码,后者记录着查看接收邮件的邮箱地址与密码(2).打开Intellij idea,按FileNew mod
2、ule,新建一个java模块,命名为Email.如下图所示:(3).在Intellij idea的project面板窗口里,双击展开Email,右键单击它的子文件夹“src”,新建一个”java”文件,命名为“Email.java”(4).编写出程序的实现代码/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.主要写一些一般的信件内容。 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); /设置文件名,可以不是原来的文件名。 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(内容: + 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(请选择你要执行的操作:1.发送带附件邮件 2.发送没有携带附件的邮件 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(请输入邮件的内容: ); String fileName = iMailMan.getUserInputFromCMD(请输入附件的路径,路径中的请加上转义字符: ); /从文件里获取邮箱的账户和密码 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(邮件发送成功!); iMailMan.chooseOperation(iMailMan); break; case 2 : String emailTo =
20、iMailMan.getUserInputFromCMD(请输入收件人的邮箱: ); String emailSubject = iMailMan.getUserInputFromCMD(请输入邮件的标题: ); String emailContent = iMailMan.getUserInputFromCMD(请输入邮件的内容: ); /从文件里获取邮箱的账户和密码 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025届四川省眉山市仁寿县铧强中学高三一诊考试英语试卷含答案
- 湖南平江二中2025届高考英语三模试卷含解析
- 云南省玉溪市元江民族中学2025届高考仿真卷英语试题含答案
- 2025届江苏省南京市玄武区高三压轴卷英语试卷含答案
- 2025届新疆昌吉回族自治州木垒县中高考冲刺英语模拟试题含解析
- 北京丰台区北京第十二中学2025年高三3月份第一次模拟考试英语试卷含解析
- 2025届河南正阳第二高级中学高考临考冲刺英语试卷含解析
- 广西柳铁一中2025年高三第一次模拟考试英语试卷含解析
- 福福建省泉州市2025年高考英语二模试卷含解析
- 2025届广东省广州市荔湾区高三下学期一模考试英语试题含解析
- 幼儿园成语故事《朝三暮四》课件
- 病案信息技术基础知识考试重点梳理(最新最全)
- 安全施工作业票(模版)
- 项目部施工管理实施计划编制任务分工表
- 【2021部编版语文】-三年级下册第七单元教材解读--PPT课件
- 橙色黑板风小学生知识产权科普PPT模板
- 中国供销合作社标识使用手册课件
- Q∕CR 9218-2015 铁路隧道监控量测技术规程
- 甲状腺解剖及正常超声切面ppt课件
- 上海市城市地下空间建设用地审批及房地产登记试行规定
- 蠕墨铸铁项目可行性研究报告写作范文
评论
0/150
提交评论