设为首页
优惠IDC
收藏本站
六狼博客
六狼论坛
开启辅助访问
切换到窄版
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
只需一步,快速开始
快捷导航
门户
首页
BBS
云计算
大数据
手机
移动开发android,ios,windows phone,windows mobile
编程
编程技术java,php,python,delphi,ruby,c,c++
前端
WEB前端htmlcss,javascript,jquery,html5
数据库
数据库开发Access,mysql,oracle,sql server,MongoDB
系统
操作系统windows,linux,unix,os,RedHat,tomcat
架构
项目管理
软件设计,架构设计,面向对象,设计模式,项目管理
企业
服务
运维实战
神马
搜索
搜索
热搜:
php
java
python
ruby
hadoop
sphinx
solr
ios
android
windows
centos
本版
帖子
用户
六狼论坛
»
首页
›
编程技术
›
MicroSoft
›
ASP.NET
›
收邮件、更改邮件标志 javamai +imap
返回列表
查看:
83
|
回复:
0
收邮件、更改邮件标志 javamai +imap
[复制链接]
zhangjiaweixt
zhangjiaweixt
当前离线
积分
250
窥视卡
雷达卡
升级
16.67%
当前用户组为
举人
当前积分为
250
, 升到下一级还需要 250 点。
70
主题
70
主题
70
主题
举人
举人, 积分 250, 距离下一级还需 250 积分
举人, 积分 250, 距离下一级还需 250 积分
积分
250
发消息
楼主
|
发表于 2013-2-5 09:07:19
|
显示全部楼层
|
阅读模式
<h1 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0.5em; padding-left: 1.5em; font-size: 15px; font-family: 'Microsoft yahei', verdana, sans-serif; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #dcdcdc;" class="title_txt"><span style="font-weight: normal; font-size: 14px; line-height: 21px;">来源:http://blog.csdn.net/superstorm5/archive/2008/03/07/2157022.aspx
网上很多用pop3收邮件的例子,但是用pop3收邮件只能获取收件箱里面所有邮件,邮件是否已读等标记无法获取,使用imap协议则避免了这个尴尬,imap不仅能获得一个邮件的详细信息(比如是否已读,是否回复),它还允许用户更改邮件的标记,但是目前支持imap协议的邮件服务器并不多,我知道的只有21cn和gmail,下面的例子中使用了代理 、SSL认证多个内容,请大家参考。
代码如下:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background-color: #e6e6e6; padding-bottom: 4px; width: 937px; padding-top: 4px;">
package
com.yourcompany.mail;
import
java.io.
*
;
import
java.security.Security;
import
java.text.
*
;
import
java.util.
*
;
import
javax.mail.
*
;
import
javax.mail.internet.
*
;
public
class
gmailRead
{
private
MimeMessage mimeMessage
=
null
;
private
String saveAttachPath
=
""
;
//
附件下载后的存放目录
private
StringBuffer bodytext
=
new
StringBuffer();
//
存放邮件内容的StringBuffer对象
private
String dateformat
=
"
yy-MM-dd HH:mm
"
;
//
默认的日前显示格式
/**
* * 構造函数,初始化一个MimeMessage對象
*
*/
public
gmailRead()
{
}
public
gmailRead(MimeMessage mimeMessage)
{
this
.mimeMessage
=
mimeMessage;
System.out.println(
"
create a PraseMimeMessage object........
"
);
}
/**
* * 獲取一个MimeMessage對象
*
*/
public
void
setMimeMessage(MimeMessage mimeMessage)
{
this
.mimeMessage
=
mimeMessage;
}
/**
* * 獲得發件人的地址和姓名
*
*/
public
String getFrom()
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 getMailAddress(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
.getAddress();
if
(email
==
null
)
email
=
""
;
else
{
email
=
MimeUtility.decodeText(email);
}
String personal
=
address
.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
{
String subject
=
""
;
try
{
subject
=
MimeUtility.decodeText(mimeMessage.getSubject());
if
(subject
==
null
)
subject
=
""
;
}
catch
(Exception exce)
{
}
return
subject;
}
/**
* * 獲得郵件的發送日期
*
*/
public
String getSentDate()
throws
Exception
{
Date sentdate
=
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.isMimeType(
"
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
++
)
{
getMailContent(multipart.getBodyPart(i));
}
}
else
if
(part.isMimeType(
"
message/rfc822
"
))
{
getMailContent((Part) part.getContent());
}
else
{
}
}
/**
* * 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*
*/
public
boolean
getReplySign()
throws
MessagingException
{
boolean
replysign
=
false
;
String needreply[]
=
mimeMessage
.getHeader(
"
Disposition-Notification-To
"
);
if
(needreply
!=
null
)
{
replysign
=
true
<span style="color: #000000;">;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlo
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2008-2020
六狼论坛
(http://it.6wolf.com) 版权所有 All Rights Reserved.
Powered by
Discuz!
X3.4
京ICP备14020293号-2
本网站内容均收集于互联网,如有问题请联系
QQ:389897944
予以删除
快速回复
返回顶部
返回列表