- 浏览: 112309 次
- 性别:
- 来自: 山东
文章分类
最新评论
-
ly5188:
大虾,例子少了些东西,
javax.mail.Messagin ...
支持邮件群发,多附件发送的javaMail代码 -
DepthJava:
请问楼主,你的就能全部解析?
目前还没找到完全解析的方法,很多 ...
javamail接收邮件的bean,可以正确的解析中文(转)(一)
//接上一文章 /**
* 获取日期显示格式
*/
public String getDateFormat(String format) throws Exception {
return this.dateformat;
}
/**
* 获得发件人的地址和姓名
*
* @throws Exception
*/
public String getFrom() throws Exception {
return getFrom(this.message);
}
public String getFrom(Message mimeMessage) throws Exception {
InternetAddress address[] = (InternetAddress[]) 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 getTOAddress() throws Exception {
return getMailAddress("TO", this.message);
}
public String getCCAddress() throws Exception {
return getMailAddress("CC", this.message);
}
public String getBCCAddress() throws Exception {
return getMailAddress("BCC", this.message);
}
public String getTOAddress(Message mimeMessage) throws Exception {
return getMailAddress("TO", mimeMessage);
}
public String getCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("CC", mimeMessage);
}
public String getBCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("BCC", mimeMessage);
}
public String getMailAddress(String type) throws Exception {
return getMailAddress(type, this.message);
}
public String getMailAddress(String type, Message mimeMessage)
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 emailaddr type!");
}
return mailaddr;
}
/**
* 获得邮件主题
*/
public String getSubject() throws MessagingException {
return getSubject(this.message);
}
public String getSubject(Message mimeMessage) throws MessagingException {
String subject = "";
try {
subject = MimeUtility.decodeText(mimeMessage.getSubject());
if (subject == null)
subject = "";
} catch (Exception exce) {
}
return subject;
}
/**
* 获得邮件发送日期
*/
public String getSentDate() throws Exception {
return getSentDate(this.message);
}
public String getSentDate(Message mimeMessage) throws Exception {
Date sentdate = mimeMessage.getSentDate();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format.format(sentdate);
}
/**
* 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*/
public boolean getReplySign() throws MessagingException {
return getReplySign(this.message);
}
public boolean getReplySign(Message mimeMessage) throws MessagingException {
boolean replysign = false;
String needreply[] = mimeMessage
.getHeader("Disposition-Notification-To");
if (needreply != null) {
replysign = true;
}
return replysign;
}
/**
* 获得此邮件的Message-ID
*/
public String getMessageId() throws MessagingException {
return getMessageId(this.message);
}
public String getMessageId(Message mimeMessage) throws MessagingException {
return ((MimeMessage) mimeMessage).getMessageID();
}
/**
* 初始化出错邮件数组
*
*/
private void setRecordFailure() {
this.recordFailure = new boolean[getMessageCount()];
}
/**
* 返回出错数组
*
* @return
*/
public boolean[] getRecordFailure() {
return this.recordFailure;
}
/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew() throws MessagingException {
return isNew(this.message);
}
/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew(Message mimeMessage) throws MessagingException {
boolean isnew = false;
Flags flags = mimeMessage.getFlags();
Flags.Flag [] flag = flags.getSystemFlags();
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Flags.Flag.SEEN) {
isnew = true;
break;
}
}
return isnew;
}
/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach() throws Exception {
return isContainAttach(this.part);
}
/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception {
boolean attachflag = false;
String contentType = part.getContentType();
if (part.isMimeType("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.isMimeType("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 attachflag;
}
/**
* 连到server,创建folder对象,创建message对象
*/
public void getConn() {
try {
this.getStoreFromServer();
this.getFolderFromStore();
} catch (Exception e) {
otherError = true;
mailDownErrorCounter++;
}
}
/**
* 建立Store连接
*/
private Store getStoreFromServer() throws Exception {
//创建session
Session session = Session.getDefaultInstance(System.getProperties(),
null);
//session.setDebug(true);
//创建store,建立连接
Store store = session.getStore(protocol);
System.out.println("connecting");
store.connect(mailHost, userName, password);
System.out.println("connected successfully");
setStore(store);
return store;
}
/**
* 打开INBox文件夹
*/
private Folder getFolderFromStore() {
//打开邮件相应文件夹
Folder getFolder;
try {
getFolder = store.getFolder("INBOX");
getFolder.open(Folder.READ_ONLY);
setFolder(getFolder);
return getFolder;
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.err.println("获取Folder失败!");
e.printStackTrace();
}
return null;
}
* 获取日期显示格式
*/
public String getDateFormat(String format) throws Exception {
return this.dateformat;
}
/**
* 获得发件人的地址和姓名
*
* @throws Exception
*/
public String getFrom() throws Exception {
return getFrom(this.message);
}
public String getFrom(Message mimeMessage) throws Exception {
InternetAddress address[] = (InternetAddress[]) 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 getTOAddress() throws Exception {
return getMailAddress("TO", this.message);
}
public String getCCAddress() throws Exception {
return getMailAddress("CC", this.message);
}
public String getBCCAddress() throws Exception {
return getMailAddress("BCC", this.message);
}
public String getTOAddress(Message mimeMessage) throws Exception {
return getMailAddress("TO", mimeMessage);
}
public String getCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("CC", mimeMessage);
}
public String getBCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("BCC", mimeMessage);
}
public String getMailAddress(String type) throws Exception {
return getMailAddress(type, this.message);
}
public String getMailAddress(String type, Message mimeMessage)
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 emailaddr type!");
}
return mailaddr;
}
/**
* 获得邮件主题
*/
public String getSubject() throws MessagingException {
return getSubject(this.message);
}
public String getSubject(Message mimeMessage) throws MessagingException {
String subject = "";
try {
subject = MimeUtility.decodeText(mimeMessage.getSubject());
if (subject == null)
subject = "";
} catch (Exception exce) {
}
return subject;
}
/**
* 获得邮件发送日期
*/
public String getSentDate() throws Exception {
return getSentDate(this.message);
}
public String getSentDate(Message mimeMessage) throws Exception {
Date sentdate = mimeMessage.getSentDate();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format.format(sentdate);
}
/**
* 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*/
public boolean getReplySign() throws MessagingException {
return getReplySign(this.message);
}
public boolean getReplySign(Message mimeMessage) throws MessagingException {
boolean replysign = false;
String needreply[] = mimeMessage
.getHeader("Disposition-Notification-To");
if (needreply != null) {
replysign = true;
}
return replysign;
}
/**
* 获得此邮件的Message-ID
*/
public String getMessageId() throws MessagingException {
return getMessageId(this.message);
}
public String getMessageId(Message mimeMessage) throws MessagingException {
return ((MimeMessage) mimeMessage).getMessageID();
}
/**
* 初始化出错邮件数组
*
*/
private void setRecordFailure() {
this.recordFailure = new boolean[getMessageCount()];
}
/**
* 返回出错数组
*
* @return
*/
public boolean[] getRecordFailure() {
return this.recordFailure;
}
/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew() throws MessagingException {
return isNew(this.message);
}
/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew(Message mimeMessage) throws MessagingException {
boolean isnew = false;
Flags flags = mimeMessage.getFlags();
Flags.Flag [] flag = flags.getSystemFlags();
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Flags.Flag.SEEN) {
isnew = true;
break;
}
}
return isnew;
}
/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach() throws Exception {
return isContainAttach(this.part);
}
/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception {
boolean attachflag = false;
String contentType = part.getContentType();
if (part.isMimeType("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.isMimeType("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 attachflag;
}
/**
* 连到server,创建folder对象,创建message对象
*/
public void getConn() {
try {
this.getStoreFromServer();
this.getFolderFromStore();
} catch (Exception e) {
otherError = true;
mailDownErrorCounter++;
}
}
/**
* 建立Store连接
*/
private Store getStoreFromServer() throws Exception {
//创建session
Session session = Session.getDefaultInstance(System.getProperties(),
null);
//session.setDebug(true);
//创建store,建立连接
Store store = session.getStore(protocol);
System.out.println("connecting");
store.connect(mailHost, userName, password);
System.out.println("connected successfully");
setStore(store);
return store;
}
/**
* 打开INBox文件夹
*/
private Folder getFolderFromStore() {
//打开邮件相应文件夹
Folder getFolder;
try {
getFolder = store.getFolder("INBOX");
getFolder.open(Folder.READ_ONLY);
setFolder(getFolder);
return getFolder;
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.err.println("获取Folder失败!");
e.printStackTrace();
}
return null;
}
发表评论
-
使用javamail的一些问题
2009-09-02 21:33 978今天用javamail做了一个发送邮件的功能.遇到了一系列的问 ... -
使用javamail api开发邮件服务系统时,常碰的中文乱码问题
2009-09-02 16:50 1708乱码是因为不同语系操作系统之间文字编码不通用造成的,可以用南极 ... -
javamail接收邮件的bean,可以正确的解析中文(转)(四)
2009-09-02 16:42 1361Pop3Bean.java是主要的代码 FileProper ... -
javamail接收邮件的bean,可以正确的解析中文(转)(三)
2009-09-02 16:35 1095//接上一文章 /** * 从folder中提取 ... -
支持邮件群发,多附件发送的javaMail代码
2009-09-02 16:31 1831在网上找了半天才找到了一点比较满意的代码,但是不能直接拿来用, ... -
用JavaMail API编写可带附件的邮件发送程序
2009-09-02 16:30 1731用JavaMail API编写可带附 ... -
javamail接收邮件的bean,可以正确的解析中文(转)(一)
2009-09-02 16:23 1464这几天写的javamail项目中接收邮件和解析邮件的类,参考了 ... -
java电子邮件(四)JavaMail(JAVA邮件服务) API详解
2009-09-02 15:35 1390一、JavaMail API简介 JavaMa ... -
java电子邮件(三)
2009-09-02 15:33 1064最新的JavaMail 1.3.1增强了在国际化字符集的处理, ... -
java电子邮件(一)
2009-09-02 15:31 1387import java.io.UnsupportedEncod ... -
java电子邮件(二)使用JavaMail收发邮件,解决了中文附件名问题
2009-09-02 15:29 1773mail.jar : http://java.sun.co ...
相关推荐
实例48 使用JAVAMAIL接收邮件 实例49 获取URL文本 实例5 哈希表和枚举器 实例50 一个简单的WEB服务器 实例51 数据报通讯:聊天吧 实例52 使用ODBC连接数据库 实例53 加载JDBC驱动 实例54 使用语句预处理 实例55 可...
2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步...
第12章 在Servlet中发送和接收邮件 12. 1 电子邮件协议 12. 2 利用SMIP发送电子邮件 12. 2. 1 利用网络编程发送邮件 12. 2. 2 利用网络编程发送电子邮件的实例 12. 3 JavaMail API和电子邮件 12. 3. 1 配置...
2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA ...
17.3.2. 使用HessianServiceExporter暴露你的bean 17.3.3. 客户端连接服务 17.3.4. 使用Burlap 17.3.5. 对通过Hessian或Burlap暴露的服务使用HTTP基础认证 17.4. 使用HTTP调用器暴露服务 17.4.1. 暴露服务对象 ...
要全部下载下载一起解压,此部分为第二部分 带有书签,清华大学出版社 第1章 JAVA WEB开发简介 1.1、WEB发展历程 1.2、企业开发架构 1.3、JAVA EE架构 1.4、JAVA EE核心设计模式 1.5、Struts开发框架 ...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
5.2.2、第二种Scriptlet:!%> 5.2.3、第三种Scriptlet: 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 5.5.1、...
0200_第二部分:WEB基础开发 0205_JSP基础语法 0206_JSP内置对象 0207_JavaBean 0208_文件上传 0300_第三部分:WEB高级开发 0309_Servlet程序开发 0310_表达式语言 0311_Tomcat数据源 0312_JSP标签编程 ...