首页 javaMail实现收发邮件

javaMail实现收发邮件

举报
开通vip

javaMail实现收发邮件1、JavaM ail 发送邮件示例 package c om.zlx.mail; import j ava.util.Properties; import j avax.mail.M essage; import j avax.mail.M essagingException; import j avax.mail.Session; import javax.mail.Transport; import j avax.mail.internet.InternetAddress; import j...

javaMail实现收发邮件
1、JavaM ail 发送邮件示例 package c om.zlx.mail; import j ava.util.Properties; import j avax.mail.M essage; import j avax.mail.M essagingException; import j avax.mail.Session; import javax.mail.Transport; import j avax.mail.internet.InternetAddress; import j avax.mail.internet.M imeMessage; /** JavaM ail 发送邮件示例*/ public class EmailSender { private String host = "smtp.163.com"; // 邮件主机服务器 private String from = "163test@163.com"; // 发件人 private String to = "163test2@163.com"; // 收件人 private String subject = "JavaM ail收发邮件测试标题"; // 邮件标题 private String content = "JavaMail收发邮件测试正文"; // 邮件内容 private String userName = "163test"; // 用户名 private String password = "*******"; // 密码 public static void main(String[] args) throws Exception { new EmailSender().sendM ail(); } /** * 设置内容 * * @param content * 邮件内容 */ public void setContent(String content) { this.content = content; } /** * 设置收件人 * * @param to * 收件人地址 */ public void setT o(String to) { this.to = to; } /** * 发送email * * @throws MessagingException * @throws Exception */ public void sendM ail() throws M essagingException, Exception { Properties props = new Properties(); props.put("mail.smtp.host", host); // 指定SMT P服务器 props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证 Session mailSession = Session.getDefaultInstance(props); Message message = new MimeM essage(mailSession); message.setFrom(new InternetAddress(from)); // 发件人 message.addRecipient(Message.Recipienttype.TO, new InternetAddress(to)); // 收件人 message.setSubject(subject); // 邮件主题 message.setText(content); // 邮件内容 message.saveChanges(); Transport transport = null; transport = mailSession.getTransport("smtp"); transport.connect(host, userName, password); transport.sendMessage(message, message.getA llRecipients()); transport.close(); } } 2、JavaM ail 收取邮件示例 package c om.zlx.mail; import j ava.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ava.io.file; import j ava.io.FileOutputStream; import j ava.io.InputStream; import j ava.text.SimpleDateFormat; import j ava.util.Date; import j ava.util.Properties; import j avax.mail.BodyPart; import j avax.mail.Flags; import j avax.mail.Folder; import j avax.mail.M essage; import j avax.mail.M essagingException; import j avax.mail.Multipart; import j avax.mail.Part; import j avax.mail.Session; import j avax.mail.Store; import j avax.mail.URLName; import j avax.mail.internet.InternetAddress; import j avax.mail.internet.M imeMessage; import j avax.mail.internet.M imeUtility; /** * 有一封邮件就需要建立一个ReciveM ail对象 */ public class ReciveOneM ail { private MimeMessage mimeM essage = null; private String saveAttachPath = ""; // 附件下载后的存放目录 private StringBuffer bodytext = new StringBuffer();// 存放邮件内容 private String dateformat = "yy-MM-dd HH:mm"; // 默认的日前显示格式 public ReciveOneM ail(MimeM essage mimeM essage) { this.mimeMessage = mimeMessage; } public void setM imeM essage(MimeM essage mimeMessage) { this.mimeMessage = mimeMessage; } /** * 获得发件人的地址和姓名 */ public String getFrom() throws Exception { InternetAddress address[] = (InternetA ddress[]) mimeMessage.getFrom(); String from = address[0].getAddress(); if (from == null) from = ""; String personal = address[0].getPersonal(); if (personal == null) personal = ""; String fromaddr = personal + "<" + from + ">"; return fromaddr; } /** * 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同"to"----收件人"cc"---抄送人地址"bcc"---密送人地址 */ public String getM ailAddress(String type) throws Exception { String mailaddr = ""; String addtype = type.toUpperCase(); InternetAddress[] address = null; if (addtype.equals("TO") || addtype.equals("CC") || addtype.equals("BCC")) { if (addtype.equals("TO")) { address = (InternetAddress[]) mimeMessage .getRecipients(Message.RecipientType.TO); } else if (addtype.equals("CC")) { address = (InternetAddress[]) mimeMessage .getRecipients(Message.RecipientType.CC); } else { address = (InternetAddress[]) mimeMessage .getRecipients(Message.RecipientType.BCC); } if (address != null) { for (int i = 0; i < address.length; i++) { String email = address[i].getAddress(); if (email == null) email = ""; else { email = MimeUtility.decodeText(email); } String personal = address[i].getPersonal(); if (personal == null) personal = ""; else { personal = MimeUtility.decodeText(personal); } String compositeto = personal + "<" + email + ">"; mailaddr += "," + compositeto; } mailaddr = mailaddr.substring(1); } } else { throw new Exception("Error e mailaddr type!"); } return m ailaddr; } /** * 获得邮件主题 */ public String getSubject() throws M essagingException { String subject = ""; try { subject = MimeUtility.decodeT ext(mimeMessage.getSubject()); if (subject == null) subject = ""; } catch (Exception exce) { } return s ubject; /** * 获得邮件发送日期 */ public String getSentDate() throws Exception { Date s entdate = mimeMessage.getSentDate(); SimpleDateFormat format = new SimpleDateFormat(dateformat); return format.format(sentdate); } /** * 获得邮件正文内容 */ public String getBodyText() { return bodytext.toString(); } /** * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件主要是根据Mimetype类型的不同执行不同的操作,一步一步的解析 */ public void getMailContent(Part part) throws Exception { String contenttype = part.getContentType(); int nameindex = contenttype.indexOf("name"); boolean conname = false; if (nameindex != -1) conname = true; System.out.println("CONTENTTYPE: " + contenttype); if (part.isM imeType("text/plain") && !conname) { bodytext.append((String) part.getContent()); } else if (part.isMimeType("text/html") && !conname) { bodytext.append((String) part.getContent()); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int counts = multipart.getCount(); for (int i = 0; i < counts; i++) { getM ailContent(multipart.getBodyPart(i)); } } else if (part.isMimeType("message/rfc822")) { getM ailContent((Part) part.getContent()); } else { } } /** * 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"f alse" */ public boolean getReplySign() throws MessagingException { boolean replysign = false; String needreply[] = mimeM essage .getHeader("Disposition-Notification-To"); if (needreply != null) { replysign = true; } return replysign; } /** * 获得此邮件的M essage-ID */ public String getMessageId() throws MessagingException { return m imeM essage.getM essageID(); } * 【判断此邮件是否已读,如果未读返回返回false,反之返回true】 */ public boolean isNew() throws MessagingException { boolean isnew = false; Flags flags = ((Message) mimeMessage).getFlags(); Flags.Flag[] flag = flags.getSystemFlags(); System.out.println("flags's l ength: " + flag.length); for (int i = 0; i < flag.length; i++) { if (flag[i] == Flags.Flag.SEEN) { isnew = true; System.out.println("seen Message......."); break; } } return i snew; } /** * 判断此邮件是否包含附件 */ public boolean isContainA ttach(Part part) throws Exception { boolean attachflag = false; // String contenttype = part.getContentType(); if (part.isM imeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { BodyPart mpart = mp.getBodyPart(i); String disposition = mpart.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition .equals(Part.INLINE)))) attachflag = true; else if (mpart.isM imeType("multipart/*")) { attachflag = isContainAttach((Part) mpart); } else { String contype = mpart.getContentType(); if (contype.toLowerCase().indexOf("application") != -1) attachflag = true; if (contype.toLowerCase().indexOf("name") != -1) attachflag = true; } } } else if (part.isMimeType("message/rfc822")) { attachflag = isContainAttach((Part) part.getContent()); } return a ttachflag; } /** * 【保存附件】 */ public void saveAttachM ent(Part part) throws Exception { String fileName = ""; if (part.isM imeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { BodyPart mpart = mp.getBodyPart(i); String disposition = mpart.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition .equals(Part.INLINE)))) { FileName = mpart.getFileName(); if (fileName.toLowerCase().indexOf("gb2312") != -1) { fileName = MimeUtility.decodeT ext(fileName); } 继续阅读
本文档为【javaMail实现收发邮件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_841159
暂无简介~
格式:doc
大小:40KB
软件:Word
页数:0
分类:互联网
上传时间:2019-09-10
浏览量:12