📄 emailserver.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.request;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.smtp.*;
import org.compiere.util.*;
/**
* Mail Server
*
* @author Jorg Janke
* @version $Id: EMailServer.java,v 1.1 2002/09/29 03:45:56 jjanke Exp $
*/
public class EMailServer
{
/**
* Mail server
*/
public EMailServer (String host, String user, String password)
{
// Session
System.out.println("Session ... ");
Properties props = new Properties();
props.put("mail.store.protocol", "smtp");
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", host);
props.put("mail.user", user);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// Connect to Store
System.out.println("Store ... ");
Store store;
try
{
store = session.getStore("imap");
}
catch (NoSuchProviderException nsp)
{
System.out.println(nsp.getMessage());
return;
}
System.out.println("Connect ... ");
try
{
store.connect(host, user, password);
}
catch (MessagingException mex1)
{
System.out.println(mex1.getMessage());
return;
}
// Folder
System.out.println("Folder ... ");
Folder folder;
try
{
folder = store.getDefaultFolder();
}
catch (MessagingException mex2)
{
System.out.println(mex2.getMessage());
return;
}
if (folder == null)
{
System.out.println("No default folder");
return;
}
try
{
Folder[] ff = folder.list("*");
// for (int i = 0; i < ff.length; i++)
// System.out.println(ff[i].getFullName());
System.out.println("Opening Folder ...");
folder = folder.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
System.out.println("Folder: " + folder.getName() + "; Messages Total=" + folder.getMessageCount()
+ " New=" + folder.getNewMessageCount());
System.out.println("Get messages ...");
Message[] msg = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(msg, fp);
//
for (int i = 0; i < msg.length; i++)
{
printEnvelope(msg[i]);
printBody((Part)msg[i]);
}
folder.close(false);
store.close();
}
catch (MessagingException mex3)
{
System.out.println(mex3.getMessage());
return;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
} // EMail
private void printEnvelope(Message m) throws Exception
{
pr("-----------------------------------------------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null)
{
for (int j = 0; j < a.length; j++)
pr("FROM: " + a[j].toString());
}
// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null)
{
for (int j = 0; j < a.length; j++)
pr("TO: " + a[j].toString());
}
// SUBJECT
pr("SUBJECT: " + m.getSubject());
// DATE
java.util.Date d = m.getSentDate();
pr("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));
// FLAGS
Flags flags = m.getFlags();
StringBuffer sb = new StringBuffer();
Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
boolean first = true;
for (int i = 0; i < sf.length; i++)
{
String s;
Flags.Flag f = sf[i];
if (f == Flags.Flag.ANSWERED)
s = "\\Answered";
else if (f == Flags.Flag.DELETED)
s = "\\Deleted";
else if (f == Flags.Flag.DRAFT)
s = "\\Draft";
else if (f == Flags.Flag.FLAGGED)
s = "\\Flagged";
else if (f == Flags.Flag.RECENT)
s = "\\Recent";
else if (f == Flags.Flag.SEEN)
s = "\\Seen";
else
continue; // skip it
if (first)
first = false;
else
sb.append(' ');
sb.append(s);
}
String[] uf = flags.getUserFlags(); // get the user flag strings
for (int i = 0; i < uf.length; i++)
{
if (first)
first = false;
else
sb.append(' ');
sb.append(uf[i]);
}
pr("FLAGS: " + sb.toString());
// X-MAILER
String[] hdrs = m.getHeader("X-Mailer");
if (hdrs != null)
pr("X-Mailer: " + hdrs[0]);
else
pr("X-Mailer NOT available");
} // printEnvelope
private void printBody(Part p) throws Exception
{
pr("CONTENT-TYPE: " + p.getContentType());
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (p.isMimeType("text/plain"))
{
pr("Plain text ---------------------------");
System.out.println((String)p.getContent());
}
else if (p.isMimeType("multipart/*"))
{
pr("Multipart ---------------------------");
Multipart mp = (Multipart)p.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++)
printBody(mp.getBodyPart(i));
}
else if (p.isMimeType("message/rfc822"))
{
pr("Nested ---------------------------");
printBody((Part)p.getContent());
}
else
{
/*
* If we actually want to see the data, and it's not a
* MIME type we know, fetch it and check its Java type.
*/
Object o = p.getContent();
if (o instanceof String)
{
pr("This is a string ---------------------------");
System.out.println((String)o);
}
else if (o instanceof InputStream)
{
pr("This is just an input stream ---------------------------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
}
else
{
pr("This is an unknown type ---------------------------");
pr(o.toString());
}
}
} // printBody
private static void pr(String s)
{
// System.out.print(indentStr.substring(0, level * 2));
System.out.println(s);
}
/**************************************************************************
* Main Test
*/
public static void main (String[] arg)
{
// EMailServer m = new EMailServer("sbs", "jjanke", "xxx");
// System.out.println(EMailServer.send("main", "jjanke@compiere.org", "jjanke@yahoo.com", "test1", "test1 message"));
// System.out.println(EMailServer.send("main", "administrator@compiere.org", "jjanke@yahoo.com", "test2", "test2 message"));
// System.out.println(EMailServer.send("main", "jjanke@compiere.org", "jjanke@yahoo.com", "test3", "test3 message"));
} // main
} // EMail
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -