package com.wgh;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WebMailBean implements HttpSessionBindingListener {
Session session = null;
String protocol = null;
String host = null;
int port = -1;
String user = null;
String defaultFrom = null;
Store store = null;
Folder[] folders = null;
String[] folderNames = null;
Folder currentFolder = null;
int currentMessageNumber = 0;
Message currentMessage = null;
// 使用你选择的smtp服务器
public WebMailBean() {
Properties properties = System.getProperties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "mail.yangnan.com");
session = Session.getInstance(properties, null);
// session.setDebug(true);
}
// 返回一个已安装协议的列表
public String[] getProtocols() {
Provider[] providers = session.getProviders();
List storeProtocols = new ArrayList();
for (int i = 0; i < providers.length; ++i) {
if (providers[i].getType() == Provider.Type.STORE) {
storeProtocols.add(providers[i].getProtocol());
}
}
return (String[]) storeProtocols.toArray(new String[0]);
}
// 属性设置方法
// 包括主机,协议,端口等
public void setProtocol(String protocol) {
if (this.protocol == null) {
this.protocol = protocol;
}
}
public String getProtocol() {
return this.protocol;
}
public void setHost(String host) {
if (this.host == null) {
this.host = host;
}
}
public String getHost() {
return this.host;
}
public void setPort(int port) {
if (this.port == -1) {
this.port = port;
}
}
public int getPort() {
return this.port;
}
// 用户
public void setUser(String user) {
if (this.user == null) {
this.user = user;
}
}
public String getUser() {
return this.user;
}
// 使用密码来初始化一个连接
public void setPassword(String password) throws MessagingException {
connect(password);
}
// 使用用户的名称和主机来初始化一个发送人地址
public String getFrom() {
if (defaultFrom == null) {
defaultFrom = user + "@" + host;
}
return defaultFrom;
}
void connect(String password) throws MessagingException {
if (store == null) {
store = session.getStore(protocol);
store.connect(host, port, user, password);
cacheFolders();
setFolder("INBOX");
}
}
// 在用户登录时判断是否仍然在连接中
public boolean isConnected() {
if (store != null) {
return store.isConnected();
}
return false;
}
// 关闭连接
void close() {
if (isConnected()) {
try {
// 将所有打开的文件夹关闭
if (folders != null) {
for (int i = 0; i < folders.length; ++i) {
if (folders[i].isOpen()) {
folders[i].close(true);
}
}
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
// 异常的处理
}
}
}
// 对文件夹的操作进行缓存
void cacheFolders() throws MessagingException {
Folder defaultFolder = store.getDefaultFolder();
Folder[] childFolders = defaultFolder.list();
List availableFolders = new ArrayList();
for (int i = 0; i < childFolders.length; ++i) {
String folderName = childFolders[i].getName();
if (!folderName.startsWith(".")
&& !folderName.equalsIgnoreCase("INBOX")) {
availableFolders.add(childFolders[i]);
}
}
// 按照文件夹的名称进行排序
Collections.sort(availableFolders, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Folder) o1).getName().compareToIgnoreCase(
((Folder) o2).getName());
}
});
// 将INBOX加到列表的第一的位置
Folder inbox = defaultFolder.getFolder("INBOX");
if (inbox.exists()) {
availableFolders.add(0, inbox);
}
folders = (Folder[]) availableFolders.toArray(new Folder[0]);
}
// 从缓存的数组中寻找文件夹
Folder findFolder(String folderName) {
for (int i = 0; i < folders.length; ++i) {
if (folders[i].getName().equals(folderName)) {
return folders[i];
}
}
return null;
}
public void setFolder(String folderName) throws MessagingException {
currentFolder = findFolder(folderName);
if ((!currentFolder.isOpen()) && (currentFolder.exists())
&& ((currentFolder.getType() & Folder.HOLDS_MESSAGES) != 0)) {
currentFolder.open(Folder.READ_WRITE);
}
currentMessageNumber = 0;
currentMessage = null;
}
// 得到当前文件夹名称
public String getFolderName() {
return currentFolder.getName();
}
// 得到一个缓存文件夹名称数组
public String[] getFolderNames() {
String[] folderNames = new String[folders.length];
for (int i = 0; i < folders.length; ++i) {
folderNames[i] = folders[i].getName();
}
return folderNames;
}
// 得到除当前文件夹以外的所有文件夹的名称
// 以便用于信息在文件夹之间的转移
public String[] getOtherFolderNames() {
String[] folderNames = new String[folders.length - 1];
for (int i = 0, j = 0; i < folders.length; ++i) {
if (folders[i] != currentFolder) {
folderNames[j++] = folders[i].getName();
}
}
return folderNames;
}
// 返回当前文件夹中的信件数目
public int getMessageCount() throws MessagingException {
return currentFolder.getMessageCount();
}
// 循环得到当前文件夹中未删除的信件
public boolean getNextMessage() throws MessagingException {
int messageCount = currentFolder.getMessageCount();
for (int i = currentMessageNumber + 1; i <= messageCount; ++i) {
Message nextMessage = currentFolder.getMessage(i);
if (!nextMessage.isSet(Flags.Flag.DELETED)) {
currentMessageNumber = i;
currentMessage = nextMessage;
return true;
}
}
return false;
}
// 设置信件编号,rest一下信件的指针。
public void setMessage(int messageNumber) throws MessagingException {
if (messageNumber == 0) {
currentMessageNumber = messageNumber;
currentMessage = null;
} else {
currentMessageNumber = messageNumber - 1;
getNextMessage();
}
}
// 得到当前信件的编号
public int getMessageNumber() {
return currentMessage.getMessageNumber();
}
// 得到当前信件发送时间,转为字符串格式
public String getMessageSentDate() throws MessagingException {
Date sentDate = currentMessage.getSentDate();
if (sentDate != null) {
return sentDate.toString();
} else {
return "Date unknown";
}
}
// 得到信件的发送地址和回复地址。
public String getMessageFrom() throws MessagingException {
Address[] addresses = currentMessage.getFrom();
if (addresses == null) {
addresses = currentMessage.getReplyTo();
}
if (addresses.length > 0) {
return addresses[0].toString();
}
return "";
}
// 得到信件的回复地址。在地址数组中的第一个地址。
public String getMessageReplyTo() throws MessagingException {
Address[] addresses = currentMessage.getReplyTo();
if (addresses.length > 0) {
return addresses[0].toString();
}
return "";
}
public String getMessageTo() throws MessagingException {
Address[] addresses = currentMessage
.getRecipients(Message.RecipientType.TO);
if (addresses != null && addresses.length > 0) {
return addresses[0].toString();
}
return "";
}
// 得到信件主题
public String getMessageSubject() throws MessagingException {
String subject = currentMessage.getSubject();
if (subject == null) {
subject = "";
}
return subject;
}
// 得到信件的大小
public String g
没有合适的资源?快使用搜索试试~ 我知道了~
javamail-demo
共161个文件
jar:40个
jpg:23个
jsp:19个
5星 · 超过95%的资源 需积分: 3 115 下载量 54 浏览量
2011-12-31
14:25:57
上传
评论
收藏 5.98MB RAR 举报
温馨提示
包括javamail.jar包和javamail2-TXT,javamail3-HTML,javamail4-带附件,javamail5-群发,javamail6-收邮件 例子
资源推荐
资源详情
资源评论
收起资源包目录
javamail-demo (161个子文件)
WebMailBean.class 11KB
DisplyEmail.class 6KB
ReceiveEmail.class 4KB
CopyOfSendMailServlet.class 4KB
SendMailServlet.class 3KB
DealAttach.class 3KB
ConnDB.class 2KB
ChStr.class 2KB
EncodingFilter.class 1KB
WebMailBean$1.class 826B
.classpath 1KB
.classpath 962B
.classpath 836B
.classpath 758B
.classpath 758B
.classpath 758B
style.css 3KB
style.css 2KB
style.css 2KB
style.css 2KB
style1.css 2KB
style.css 2KB
index_04.gif 62KB
tiring-room_01.gif 45KB
index_tools_dlS.gif 7KB
index_mend_dlS.gif 7KB
index_friendlink.gif 7KB
index_bbs.gif 7KB
index_01.gif 7KB
upFile_bg.gif 6KB
tiring-room_03.gif 5KB
h_user.gif 4KB
tiring-room_02.gif 4KB
tiring-room_05.gif 4KB
main_bg.gif 2KB
index_02.gif 246B
U.gif 130B
B.gif 127B
I.gif 124B
copyright.htm 601B
mail.jar 425KB
mail.jar 425KB
mail.jar 425KB
mail.jar 425KB
mail.jar 425KB
mail.jar 425KB
mailapi.jar 227KB
mailapi.jar 227KB
imap.jar 167KB
commons-email-1.1-javadoc.jar 86KB
commons-email-1.1-javadoc.jar 86KB
commons-email-1.1-javadoc.jar 86KB
commons-email-1.1-javadoc.jar 86KB
commons-email-1.1-javadoc.jar 86KB
commons-email-1.1-javadoc.jar 86KB
activation.jar 54KB
activation.jar 54KB
activation.jar 54KB
activation.jar 54KB
activation.jar 54KB
activation.jar 54KB
commons-fileupload-1.2.jar 52KB
smtp.jar 42KB
smtp.jar 42KB
commons-email-1.1-sources.jar 35KB
commons-email-1.1-sources.jar 35KB
commons-email-1.1-sources.jar 35KB
commons-email-1.1-sources.jar 35KB
commons-email-1.1-sources.jar 35KB
commons-email-1.1-sources.jar 35KB
pop3.jar 34KB
pop3.jar 34KB
commons-email-1.1.jar 30KB
commons-email-1.1.jar 30KB
commons-email-1.1.jar 30KB
commons-email-1.1.jar 30KB
commons-email-1.1.jar 30KB
commons-email-1.1.jar 30KB
dsn.jar 18KB
jsmartcom_zh_CN.jar 13KB
WebMailBean.java 12KB
DisplyEmail.java 4KB
ReceiveEmail.java 3KB
CopyOfSendMailServlet.java 3KB
SendMailServlet.java 3KB
ConnDB.java 2KB
DealAttach.java 1KB
ChStr.java 1KB
EncodingFilter.java 938B
7_2_3_01副本.jpg 189KB
bgnew.jpg 160KB
top.jpg 151KB
main.jpg 136KB
loginBg.jpg 112KB
bottom.JPG 100KB
复件 bg.jpg 84KB
bg.jpg 84KB
bg.jpg 81KB
top.jpg 74KB
left.jpg 53KB
共 161 条
- 1
- 2
rock0304
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页