2012年8月1日星期三
JAVA Connect MS Exchange Server with Third Party Product - exJello
/*
* Sample based on Sun Microsystems's JavaMail Example
* Sample based on exJello's Example
*/
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.TimeZone;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.exjello.mail.ExchangeConstants;
import org.exjello.mail.ExchangeStore;
import org.exjello.mail.ExchangeTransport;
/**
* You can use this test program in console mode with parameters or with
* inner static fields
*
* @param argv
* @throws Exception
*/
public class TestExJello {
/*
* Static field if you whant to test it without console parameters
*/
private static String server = "<hostname/IP>";
private static String username = "<domain>\\<account name>:<email address>";
private static String password = "<password>";
private static String folder = "Inbox";
public static final String FORMAT_ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static void main(String argv[]) throws Exception {
String lastCheck = "";
if (argv == null || argv.length == 0) {
argv = new String[] { server, username, password, folder };
}
if (argv.length != 4) {
System.out.println("Usage: TestExJello <host> <user> <password> <mbox>");
// System.exit(1);
}
/*
* A sample "for": if you want to test FILTER_LAST_CHECK exJello filter
*/
for (int i = 0; i < 5; i++) {
System.out.println(lastCheck);
CheckMail(argv, lastCheck);
SendMail(argv);
Thread.currentThread().sleep(5000);
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(FORMAT_ISO_8601);
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
lastCheck = dateFormatGmt.format(new GregorianCalendar().getTime());
// break;
}
}
private static void CheckMail(String argv[], String laskCheck) {
System.out.println("\nTesting exJello - Check Mail\n");
try {
Properties props = System.getProperties();
/*
* Use UNFILTERED_PROPERTY if you whant Readed and Unreaded emails
*/
props.setProperty(ExchangeConstants.UNFILTERED_PROPERTY, String.valueOf(true));
/*
* You can use other filter implemented on exJello, they are based
* on:
* http://blogs.msdn.com/b/andrewdelin/archive/2005/05/11/416312.aspx
*/
/*
* Use FILTER_FROM_PROPERTY to filter email only form certain
* address or domain
*/
//props.setProperty(ExchangeConstants.FILTER_FROM_PROPERTY, "@mydomain.com");
/*
* Use FILTER_TO_PROPERTY to fiter emails sent to specified address
* (usefull with alias of mailbox)
*/
//props.setProperty(ExchangeConstants.FILTER_TO_PROPERTY, "alias@mydomain.com");
/*
* You can use FILTER_LAST_CHECK to get email after certain date
*/
if (laskCheck != null && !laskCheck.isEmpty()) {
props.setProperty(ExchangeConstants.FILTER_LAST_CHECK, laskCheck);
}
// Get a Session object
Session session = Session.getInstance(props, null);
Store store = new ExchangeStore(session, null);
// Connect
store.connect(argv[0], argv[1], argv[2]);
Folder folder = store.getFolder(argv[3]);
if (folder == null || !folder.exists()) {
System.out.println("Invalid folder");
// System.exit(1);
}
// folder.open(Folder.READ_WRITE);
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
System.out.println("Got " + msgs.length + " new messages");
for (Message msg : msgs) {
System.out.println(String.format("From: %s", msg.getFrom().toString()));
System.out.println(String.format("Subject: %s", msg.getSubject()));
System.out.println(String.format("Content: %s", msg.getContent().toString()));
}
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
private static void SendMail(String argv[]) {
System.out.println("\nTesting exJello - Send Mail\n");
ExchangeTransport exTran = null;
try {
Properties props = System.getProperties();
// Get a Session object
Session session = Session.getInstance(props, null);
exTran = new ExchangeTransport(session, null);
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Test Message");
// Specify Sender
// InternetAddress fromAddr = new InternetAddress("<email address>");
// msg.setFrom(fromAddr);
// Specify Receiver
InternetAddress[] toAddr = {new
InternetAddress("<email address>"),
};
msg.setRecipients(Message.RecipientType.TO, toAddr);
// Message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hello from Java using exJello.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attachment part
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("d:\\attachment.txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("attachment.txt");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
// Connect
exTran.connect(argv[0], argv[1], argv[2]);
exTran.sendMessage(msg, toAddr);
System.out.println("\nMail sent\n");
} catch (Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
} finally {
if (exTran != null) {
try {
exTran.close();
} catch (Exception ignore) { }
}
}
}
}
訂閱:
文章 (Atom)