多媒体网络通信技术实验报告

上传人:hao****an 文档编号:163921843 上传时间:2022-10-23 格式:DOC 页数:32 大小:424.01KB
收藏 版权申诉 举报 下载
多媒体网络通信技术实验报告_第1页
第1页 / 共32页
多媒体网络通信技术实验报告_第2页
第2页 / 共32页
多媒体网络通信技术实验报告_第3页
第3页 / 共32页
资源描述:

《多媒体网络通信技术实验报告》由会员分享,可在线阅读,更多相关《多媒体网络通信技术实验报告(32页珍藏版)》请在装配图网上搜索。

1、多媒体网络通信邮件接收与发送实 验 报 告 学 院:计算机与电子信息学院班级名称:学生姓名:空白小龙学 号:指导教师:时 间:2011年1月一、实验题目:编写一个邮件客户端和邮件接收端,能够接收和发送邮件。二、实验过程:本次实验通过使用JavaMail以及Java的图形化界面来完成邮件接收与发送的功能。JavaMail是提供给开发者处理电子邮件相关的编程接口,它是Sun发布的用来处理Email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook或者Foxmail的应用程序。虽然JavaMail是Sun的API之一,但它目前还没

2、有被加在标准的java开发工具包中(Java Development Kit),这就意味着你在使用前必须另外下载JavaMail文件。所以在此次实验中,我在项目中添加了activation.jar和mail.jar这两个jar包。JavaMail包中用于处理电子邮件的核心类是:Session,Message,Address,Authenticator,Transport,Store,Folder等。Session定义了一个基本的邮件会话,它需要从Properties中读取类似于邮件服务器,用户名和密码等信息。下面简要介绍下我的设计思路与实现过程并简单地注释下代码。我的项目构造如下图:主要设计思

3、想是登录验证发送邮件或者接收邮件,通过校验用户输入的邮箱账号和密码来进行验证,输入不正确的话就弹出提示框给出错误提示并可继续重新输入,如果正确的话就进入发送邮件的界面,此时默认的发件人就是用户输入的账号,与此同时把用户在登录模块输入的密码也存起来,作为以后发送邮件和接收邮件的密码。成功登录之后,即进入了发送邮件的界面中,发件人已经默认设置为用户登录时输入的账号了,其他的比如说:收件人、邮件主题、内容、附件均可由用户输入与选择,可支持多附件传输。发送邮件成功后会弹出对话框提示邮件已经成功发送。在发送邮件的界面上还有一个按钮是“收信”按钮,可切换到接收邮件界面。进入接收邮件界面后,就默认接收第一封

4、邮件了,在界面上分别显示了邮箱中一共有几封邮件;邮件的主题、发件人、发送邮件的时间、邮件内容、邮件附件(默认保存在C盘根目录中);并可通过“上一封”和“下一封”来查看其他邮件;在界面上还有一个“写信”按钮,用于切换到发送邮件界面。大概的功能设计就是这样了。为了更好的体现面向对象的设计思想,我把邮件抽象成了一个Mail类(mail包),在接收和发送邮件的工程中很好的使用到了这个类,Mail类(自定义的邮件类)主要包含了发送者邮箱用户名userName、发送者邮箱密码password、发送邮件服务器hostName、发送邮件服务器端口port、邮件接收者toAddress、邮件主题subject、

5、邮件内容content、邮件发送者fromAddress、附件路径列表fileList、附件保存路径attachPath、邮件发送时间time、邮件数量number这些成员变量,并添加了相应的set和get方法。另外Check类(check包)用于检测用户输入的邮箱账号和密码是否正确,正确则返回true,否则返回false。recive包中的ReciveMyMail类为接收邮件类;send包中的SendMyMail类为发送邮件类,而MyAuthenticator类继承了Authenticator类用于记录用户名和密码。gui包中包含三个图形化界面的类,分别为:Login类(邮件登录界面)、Se

6、adMail类(邮件发送界面)、ReciveMail类(邮件接收界面)。三、实验代码:mail包中的Mail.javapackage mail;(import略)/* * 自定义的邮件类 * * author 空白小龙 * version ,2010-10-28 */public class Mail / 发送者邮箱用户名public static String userName;/ 发送者邮箱密码public static String password;/ 发送邮件服务器private String hostName;/ 发送邮件服务器端口private int port;/ 邮件接收者p

7、rivate String toAddress;/ 邮件主题private String subject;/ 邮件内容private String content;/ 邮件发送者private String fromAddress;/ 附件路径列表private List fileList = new ArrayList();/ 附件保存路径private String attachPath;/ 邮件发送时间private String time;/ 邮件数量private int number;/ get方法和set方法略check包中的Check.javapackage check;(im

8、port略)/* * 检测邮箱账号和密码是否正确 * * author空白小龙 * version ,2010-10-28 */public class Check /* * 验证登录是否成功,成功返回true,否则返回false * * param mail * return true or false */public boolean MyCheck(Mail mail) try / 设置JavaMail属性Properties props = new Properties();/ 设置邮件服务器端口props.put(mail.smtp.port, mail.getPort();/ SM

9、TP邮件服务器IP地址或主机名props.put(mail.smtp.host, mail.getHostName();props.put(mail.smtp.auth, true);/ 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)MyAuthenticator auth = new MyAuthenticator(mail.getUserName(), mail.getPassword();/ 根据已配置的JavaMail属性创建Session实例Session mailSession = Session.getInstance(props,(Authentic

10、ator) auth);/ 创建Transport对象Transport tran = mailSession.getTransport(smtp);/ 连接邮件服务器try tran.connect(mail.getHostName(), mail.getUserName(), mail.getPassword(); catch (javax.mail.AuthenticationFailedException e) return false;tran.close(); catch (AddressException e) catch (MessagingException e) retur

11、n true;recive包中的ReciveMyMail.javapackage recive;(import略)/* * 接收邮件类 * * author空白小龙 * version ,2010-10-28 */public class ReciveMyMail / Mail实例化对象static Mail mail;/ 附件路径列表static List fileList = new ArrayList();/ 构造方法public ReciveMyMail() / 重写构造方法SuppressWarnings(static-access)public ReciveMyMail(Mail

12、mail) this.mail = mail;/ 接收邮件的方法public Mail reveiveMail(int num) try Properties props = new Properties();Session session = Session.getDefaultInstance(props, null);Store store = session.getStore(pop3);store.connect(mail.getHostName(), mail.getUserName(), mail.getPassword();Folder folder = store.getFo

13、lder(INBOX);folder.open(Folder.READ_ONLY);Message message = folder.getMessages();mail.setNumber(message.length);if (message.length 0) fileList.clear();handleMultipart(messagenum); else return null;if (folder != null) folder.close(true);if (store != null) store.close(); catch (NoSuchProviderException

14、 e) e.printStackTrace(); catch (MessagingException e) e.printStackTrace(); catch (Exception e) e.printStackTrace();return mail;private void handleMultipart(Message msg) throws Exception handle(msg);String disposition;Multipart mp = (Multipart) msg.getContent();int mpCount = mp.getCount();for (int m

15、= 0; m mpCount; m+) BodyPart part = mp.getBodyPart(m);disposition = part.getDisposition();if (disposition != null & disposition.equals(Part.ATTACHMENT) saveAttach(part, mail.getAttachPath();/ 邮件附件mail.setFileList(fileList);/ 填充邮件SuppressWarnings(deprecation)private static void handle(Message msg) th

16、rows Exception / 邮件主题mail.setSubject(msg.getSubject();/ 邮件发件人mail.setToAddress(msg.getFrom()0.toString();/ 邮件发送时间mail.setTime(msg.getSentDate().toLocaleString();String contentStr = ;/ 获取信息对象Part messagePart = msg;Object content = messagePart.getContent();/ 附件if (content instanceof Multipart) message

17、Part = (Multipart) content).getBodyPart(0);/ 获取content类型String contentType = messagePart.getContentType();/ 如果邮件内容是纯文本或者是HTML,那么打印出信息if (contentType.startsWith(text/plain)| contentType.startsWith(text/html) InputStream is = messagePart.getInputStream();BufferedReader reader = new BufferedReader(new

18、InputStreamReader(is);String thisLine = reader.readLine();while (thisLine != null) contentStr = contentStr + thisLine;thisLine = reader.readLine();/ 邮件内容mail.setContent(contentStr);/ 保存附件private static void saveAttach(BodyPart part, String filePath)throws Exception String fileName = part.getFileName

19、();fileList.add(fileName);InputStream in = part.getInputStream();FileOutputStream writer = new FileOutputStream(new File(filePath + + fileName);byte content = new byte255;while (in.read(content) != -1) writer.write(content);writer.close();in.close();send包中的MyAuthenticator.javapackage send;(import略)/

20、* * Authenticator类的实现:记录用户名和密码 * * author 空白小龙 * version ,2010-10-28 */public class MyAuthenticator extends Authenticator / 发送邮箱的账号String userName;/ 发送邮箱的密码String userPass;/ 构造方法public MyAuthenticator() / 重写构造方法public MyAuthenticator(String userName, String userPass) this.userName = userName;this.us

21、erPass = userPass;/ 设置邮箱账号和密码protected PasswordAuthentication getPasswordAuthentication() return new PasswordAuthentication(userName, userPass);send包中的SendMyMail.javapackage send;(import略)/* * 发送邮件类 * * author 空白小龙 * version ,2010-10-28 */public class SendMyMail SuppressWarnings(static-access)/ 发送邮件

22、方法public void setMail(Mail mail) try / 设置JavaMail属性Properties props = new Properties();/ 设置邮件服务器端口props.put(mail.smtp.port, mail.getPort();/ SMTP邮件服务器IP地址或主机名props.put(mail.smtp.host, mail.getHostName();props.put(mail.smtp.auth, true);/ 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)MyAuthenticator auth = new

23、 MyAuthenticator(mail.getUserName(), mail.getPassword();/ 根据已配置的JavaMail属性创建Session实例Session mailSession = Session.getInstance(props,(Authenticator) auth);/ 你可以在控制台(console)上看到发送邮件的过程mailSession.setDebug(false);MimeMessage msg = new MimeMessage(mailSession);InternetAddress address = InternetAddress.

24、parse(mail.getToAddress(), false);/ 设置邮件接收者msg.setRecipients(Message.RecipientType.TO, address);/ 设置邮件主题msg.setSubject(mail.getSubject();/ 设置邮件时间msg.setSentDate(new Date();/ 设置邮件发送者msg.setFrom(new InternetAddress(mail.getFromAddress();Multipart multipart = new MimeMultipart();/ 加入文本内容MimeBodyPart mi

25、meBodyPart = new MimeBodyPart();mimeBodyPart.setText(mail.getContent();multipart.addBodyPart(mimeBodyPart);List fileList = mail.getFileList();/ 加入附件for (String filePath : fileList) MimeBodyPart bodyPart = new MimeBodyPart();/ 得到数据源FileDataSource fileDataSource = new FileDataSource(filePath);bodyPart

26、.setDataHandler(new DataHandler(fileDataSource);bodyPart.setDisposition(Part.ATTACHMENT);/ 设置文件名bodyPart.setFileName(fileDataSource.getName();multipart.addBodyPart(bodyPart);msg.setContent(multipart);/ 创建Transport对象Transport tran = mailSession.getTransport(smtp);/ 连接邮件服务器tran.connect(mail.getHostNam

27、e(), mail.getUserName(), mail.getPassword();/ 发送邮件tran.send(msg);/ 关闭Transport对象tran.close(); catch (AddressException e) e.printStackTrace(); catch (MessagingException e) e.printStackTrace();gui包中的Login.javapackage gui;(import略)/* * 邮件登录界面 * * author 空白小龙 * version ,2010-10-28 */public class Login e

28、xtends Frame private static final long serialVersionUID = 1L;/ 密码输入框private JPasswordField jPasswordField = null;/ 账号输入框private TextField textField = null;/ 标签private Label label1 = null;/ 标签private Label label2 = null;/ 登录按钮private Button button = null;/ 退出按钮private Button quit = null;/* * 初始化密码输入框

29、 * */private JPasswordField getJPasswordField() if (jPasswordField = null) jPasswordField = new JPasswordField();jPasswordField.setBounds(new Rectangle(155, 91, 207, 36);jPasswordField.setFont(new Font(Dialog, Font.PLAIN, 18);jPasswordField.setText(070707);return jPasswordField;/* * 初始化账号输入框 * */pri

30、vate TextField getTextField() if (textField = null) textField = new TextField();textField.setBounds(new Rectangle(155, 45, 207, 36);textField.setFont(new Font(Dialog, Font.PLAIN, 23);textField.setText(dongshil);return textField;/* * 初始化登录按钮,并添加按钮动作 * */private Button getButton() if (button = null) b

31、utton = new Button();button.setBounds(new Rectangle(233, 141, 71, 36);button.setFont(new Font(Dialog, Font.PLAIN, 18);button.setLabel(登录);/ 添加登录按钮的事件处理button.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) / 获得用户输入的账号String usename = te

32、xtField.getText();/ 获得用户输入的密码String password = String.valueOf(jPasswordField.getPassword();/ 判断账号和密码是否正确boolean flag = false;Check c = new Check();Mail mail = new Mail();mail.setHostName();mail.setPort(25);mail.setUserName(usename);mail.setPassword(password);flag = c.MyCheck(mail);/ 账号和密码如果正确则跳转到邮件接

33、收界面,否则弹出一个对话框if (flag) Mail.userName = usename;Mail.password = password;Login.this.setVisible(false);SeadMail send = new SeadMail();send.setVisible(true); else JOptionPane.showMessageDialog(null, 邮箱或密码输入不正确,请重新输入。););return button;/* * 初始化退出按钮 * */private Button getQuit() if (quit = null) quit = new

34、 Button();quit.setBounds(new Rectangle(100, 141, 71, 36);quit.setFont(new Font(Dialog, Font.PLAIN, 18);quit.setLabel(退出);quit.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) System.exit(0););return quit;/* * main方法 * * param args */publ

35、ic static void main(String args) Login login = new Login();login.setVisible(true);/* * 构造方法 */public Login() super();initialize();/* * 初始化方法 * * return void */private void initialize() Dimension d;label2 = new Label();label2.setBounds(new Rectangle(24, 91, 120, 36);label2.setText(密码:);label2.setAlig

36、nment(Label.CENTER);label2.setFont(new Font(Dialog, Font.PLAIN, 18);label1 = new Label();label1.setBounds(new Rectangle(24, 45, 120, 36);label1.setText(邮箱:);label1.setAlignment(Label.CENTER);label1.setFont(new Font(Dialog, Font.PLAIN, 18);d = Toolkit.getDefaultToolkit().getScreenSize();this.setLocat

37、ion(d.width / 3, d.height / 3);this.setLayout(null);this.setSize(407, 209);this.setTitle(登录-空白小龙(0707100101)邮件接收与发送程序);this.add(getJPasswordField(), null);this.add(getTextField(), null);this.add(label1, null);this.add(label2, null);this.add(getButton(), null);this.add(getQuit(), null);this.addWindow

38、Listener(new java.awt.event.WindowAdapter() public void windowClosing(java.awt.event.WindowEvent e) System.exit(0););gui包中的ReciveMail.javapackage gui;(import略)/* * 邮件接收界面 * * author 空白小龙 * version ,2010-10-28 */public class ReciveMail extends Frame private static final long serialVersionUID = 1L;/ 标

39、签private Label label = null;/ 标签private Label subject = null;/ 标签private Label fromto = null;/ 标签private Label time = null;/ 标签private Label content = null;/ 标签private Label file = null;/ 标签private Label subjectContent = null;/ 标签private Label fromtoContent = null;/ 标签private Label timeContent = nul

40、l;/ 标签private Label fileContent = null;/ 标签private Label notice = null;/ 标签private Label count = null;/ 邮件内容显示框private TextArea textAreaContent = null;/ 写信按钮private Button button = null;/ 上一封邮件按钮private Button before = null;/ 下一封邮件按钮private Button after = null;static int length = 0;static int coun =

41、 0;/* * 构造方法 */public ReciveMail() super();initialize();recive();/ 自定义的接收邮件方法recive()public void recive() Mail mail = new Mail();mail.setHostName();mail.setUserName(Mail.userName);mail.setPassword(Mail.password);mail.setAttachPath(C:);ReciveMyMail receiver = new ReciveMyMail(mail);try mail = receive

42、r.reveiveMail(coun);length = mail.getNumber();count.setText(一共 + mail.getNumber() + 封邮件);subjectContent.setText(mail.getSubject();fromtoContent.setText(mail.getToAddress();timeContent.setText(mail.getTime();textAreaContent.setText(mail.getContent();String str = ;for (int i = 0; i (mail.getFileList()

43、.size(); i+) str = str + mail.getFileList().get(i) + ;fileContent.setText(str); catch (Exception e) e.printStackTrace();if (coun = 0) before.setEnabled(false); else before.setEnabled(true);if (coun = (length - 1) after.setEnabled(false); else after.setEnabled(true);/* * 初始化方法 * * return void */priva

44、te void initialize() Dimension d;count = new Label();count.setBounds(new Rectangle(357, 67, 97, 26);count.setText();count.setFont(new Font(Dialog, Font.PLAIN, 16);notice = new Label();notice.setBounds(new Rectangle(329, 300, 128, 24);notice.setText(附件已存入C盘根目录);fileContent = new Label();fileContent.s

45、etBounds(new Rectangle(110, 297, 212, 26);fileContent.setText();fileContent.setFont(new Font(Dialog, Font.PLAIN, 18);timeContent = new Label();timeContent.setBounds(new Rectangle(110, 155, 212, 26);timeContent.setText();timeContent.setFont(new Font(Dialog, Font.PLAIN, 18);fromtoContent = new Label()

46、;fromtoContent.setBounds(new Rectangle(110, 121, 212, 26);fromtoContent.setText();fromtoContent.setFont(new Font(Dialog, Font.PLAIN, 18);subjectContent = new Label();subjectContent.setBounds(new Rectangle(110, 90, 212, 26);subjectContent.setText();subjectContent.setFont(new Font(Dialog, Font.PLAIN,

47、18);file = new Label();file.setBounds(new Rectangle(28, 297, 66, 23);file.setText(附件:);file.setFont(new Font(Dialog, Font.PLAIN, 18);content = new Label();content.setBounds(new Rectangle(28, 190, 66, 23);content.setText(内容:);content.setFont(new Font(Dialog, Font.PLAIN, 18);time = new Label();time.se

48、tBounds(new Rectangle(29, 155, 66, 23);time.setText(时间:);time.setFont(new Font(Dialog, Font.PLAIN, 18);fromto = new Label();fromto.setBounds(new Rectangle(29, 121, 66, 23);fromto.setText(发件人:);fromto.setFont(new Font(Dialog, Font.PLAIN, 18);subject = new Label();subject.setBounds(new Rectangle(29, 9

49、0, 66, 23);subject.setText(主题:);subject.setFont(new Font(Dialog, Font.PLAIN, 18);label = new Label();label.setBounds(new Rectangle(87, 41, 256, 41);label.setAlignment(Label.CENTER);label.setFont(new Font(Dialog, Font.PLAIN, 18);label.setText(您好, + Mail.userName + ,欢迎您);d = Toolkit.getDefaultToolkit(

50、).getScreenSize();this.setLocation(d.width / 3, d.height / 5);this.setLayout(null);this.setSize(472, 339);this.setTitle(空白小龙(0707100101)邮件接收);this.add(label, null);this.add(subject, null);this.add(fromto, null);this.add(time, null);this.add(content, null);this.add(file, null);this.add(subjectContent

51、, null);this.add(fromtoContent, null);this.add(timeContent, null);this.add(getTextAreaContent(), null);this.add(fileContent, null);this.add(getButton(), null);this.add(notice, null);this.add(count, null);this.add(getBefore(), null);this.add(getAfter(), null);this.addWindowListener(new java.awt.event

52、.WindowAdapter() public void windowClosing(java.awt.event.WindowEvent e) System.exit(0););/* * 初始化邮件内容显示框 * */private TextArea getTextAreaContent() if (textAreaContent = null) textAreaContent = new TextArea();textAreaContent.setBounds(new Rectangle(110, 190, 328, 101);textAreaContent.setFont(new Fon

53、t(Dialog, Font.PLAIN, 18);textAreaContent.setEditable(false);return textAreaContent;/* * 初始化写信按钮 * */private Button getButton() if (button = null) button = new Button();button.setBounds(new Rectangle(370, 155, 68, 28);button.setLabel(写信);button.setFont(new Font(Dialog, Font.PLAIN, 18);/ 添加写信按钮的事件处理b

54、utton.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) ReciveMail.this.setVisible(false);SeadMail send = new SeadMail();send.setVisible(true););return button;/* * 初始化上一封邮件按钮 * */private Button getBefore() if (before = null) before = new Button();before.setBounds(new Rectangle(338, 106, 52, 30);before.setLabel(上一封);before.setFont(new Font(Dialog, Font.PLAIN, 16);/ 添加上一封按钮的事件处理before.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEven

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!