關(guān)鍵字: 企業(yè)應(yīng)用 ?
????
????? 兄弟我理論性的東西說不出來,不過實際運用咱還是有辦法的
前幾天由于工作需要,想了解下關(guān)于支持ssl的郵件收發(fā),按照以前普通的做法是行不通的,所以就上網(wǎng)東找找,西瞧瞧。發(fā)現(xiàn)了個好東西,并且實驗成功。
????? 那天本想來javaeye看看有沒有人有相關(guān)的經(jīng)驗,找了老半天,連個屁也沒聞到,我就說我們,我們這些做程序員的不能老是吹吹水,談?wù)劦览恚覀兊媚贸鳇c實際的東西出來,就想fins一樣,我就很佩服他的貢獻精神。
????? 不說廢話了,看看源代碼,大家有空也可以實驗下。還真有用
package?org.job.six;
?
import?java.io.UnsupportedEncodingException;
import?java.security.Security;
import?java.util.Properties;
import?javax.mail.FetchProfile;
import?javax.mail.Folder;
import?javax.mail.Message;
import?javax.mail.Session;
import?javax.mail.Store;
import?javax.mail.URLName;
import?javax.mail.internet.InternetAddress;
import?javax.mail.internet.MimeUtility;
import?org.job.util.Logger;
import?org.job.util.mail.ApplicationContext;
/**
?*?用于收取Gmail郵件
?*?
?*?@author?wuhua
?*/
public?class?GmailFetch?{
????private?static?Logger?logger?=?Logger.getLogger(GmailFetch.class);
????public?static?void?main(String?argv[])?throws?Exception?{
????????logger.debug("開始讀取郵件
");
????????Security.addProvider(new?com.sun.net.ssl.internal.ssl.Provider());
????????final?String?SSL_FACTORY?=?"javax.net.ssl.SSLSocketFactory";
????????//?Get?a?Properties?object
????????Properties?props?=?System.getProperties();
????????props.setProperty("mail.pop3.socketFactory.class",?SSL_FACTORY);
????????props.setProperty("mail.pop3.socketFactory.fallback",?"false");
????????props.setProperty("mail.pop3.port",?"995");
????????props.setProperty("mail.pop3.socketFactory.port",?"995");
????????//?以下步驟跟一般的JavaMail操作相同
????????Session?session?=?Session.getDefaultInstance(props,?null);
????????//?請將紅色部分對應(yīng)替換成你的郵箱帳號和密碼
????????URLName?urln?=?new?URLName("pop3",?ApplicationContext.POP3,?995,?null,
????????????????ApplicationContext.GMAIL_MAIL_NAME,
????????????????ApplicationContext.GMAIL_MAIL_PASSWORD);
????????Store?store?=?session.getStore(urln);
????????Folder?inbox?=?null;
????????try?{
????????????store.connect();
????????????inbox?=?store.getFolder("INBOX");
????????????inbox.open(Folder.READ_ONLY);
????????????FetchProfile?profile?=?new?FetchProfile();
????????????profile.add(FetchProfile.Item.ENVELOPE);
????????????Message[]?messages?=?inbox.getMessages();
????????????inbox.fetch(messages,?profile);
????????????logger.debug("收件箱的郵件數(shù):"?+?messages.length);
????????????for?(int?i?=?0;?i?<?messages.length;?i++)?{
????????????????//?郵件發(fā)送者
????????????????String?from?=?decodeText(messages[i].getFrom()[0].toString());
????????????????InternetAddress?ia?=?new?InternetAddress(from);
????????????????logger.debug("發(fā)信人:"?+?ia.getPersonal()?+?'('
????????????????????????+?ia.getAddress()?+?')');
????????????????//?郵件標題
????????????????logger.debug("主題:"?+?messages[i].getSubject());
????????????????//?郵件大小
????????????????logger.debug("郵件大小:"?+?messages[i].getSize());
????????????????//?郵件發(fā)送時間
????????????????logger.debug("發(fā)送日期:"?+?messages[i].getSentDate());
????????????}
????????}?finally?{
????????????try?{
????????????????inbox.close(false);
????????????}?catch?(Exception?e)?{
????????????}
????????????try?{
????????????????store.close();
????????????}?catch?(Exception?e)?{
????????????}
????????}
????????
????????logger.debug("讀取郵件完畢
");
????}
????protected?static?String?decodeText(String?text)
????????????throws?UnsupportedEncodingException?{
????????if?(text?==?null)
????????????return?null;
????????if?(text.startsWith("=?GB")?||?text.startsWith("=?gb"))
????????????text?=?MimeUtility.decodeText(text);
????????else
????????????text?=?new?String(text.getBytes("ISO8859_1"));
????????return?text;
????}
}
package?org.job.six;
?
import?java.io.UnsupportedEncodingException;
import?java.security.Security;
import?java.util.Properties;
import?javax.mail.FetchProfile;
import?javax.mail.Folder;
import?javax.mail.Message;
import?javax.mail.Session;
import?javax.mail.Store;
import?javax.mail.URLName;
import?javax.mail.internet.InternetAddress;
import?javax.mail.internet.MimeUtility;
import?org.job.util.Logger;
import?org.job.util.mail.ApplicationContext;
/**
?*?用于收取Gmail郵件
?*?
?*?@author?wuhua
?*/
public?class?GmailFetch?{
????private?static?Logger?logger?=?Logger.getLogger(GmailFetch.class);
????public?static?void?main(String?argv[])?throws?Exception?{
????????logger.debug("開始讀取郵件
");
????????Security.addProvider(new?com.sun.net.ssl.internal.ssl.Provider());
????????final?String?SSL_FACTORY?=?"javax.net.ssl.SSLSocketFactory";
????????//?Get?a?Properties?object
????????Properties?props?=?System.getProperties();
????????props.setProperty("mail.pop3.socketFactory.class",?SSL_FACTORY);
????????props.setProperty("mail.pop3.socketFactory.fallback",?"false");
????????props.setProperty("mail.pop3.port",?"995");
????????props.setProperty("mail.pop3.socketFactory.port",?"995");
????????//?以下步驟跟一般的JavaMail操作相同
????????Session?session?=?Session.getDefaultInstance(props,?null);
????????//?請將紅色部分對應(yīng)替換成你的郵箱帳號和密碼
????????URLName?urln?=?new?URLName("pop3",?ApplicationContext.POP3,?995,?null,
????????????????ApplicationContext.GMAIL_MAIL_NAME,
????????????????ApplicationContext.GMAIL_MAIL_PASSWORD);
????????Store?store?=?session.getStore(urln);
????????Folder?inbox?=?null;
????????try?{
????????????store.connect();
????????????inbox?=?store.getFolder("INBOX");
????????????inbox.open(Folder.READ_ONLY);
????????????FetchProfile?profile?=?new?FetchProfile();
????????????profile.add(FetchProfile.Item.ENVELOPE);
????????????Message[]?messages?=?inbox.getMessages();
????????????inbox.fetch(messages,?profile);
????????????logger.debug("收件箱的郵件數(shù):"?+?messages.length);
????????????for?(int?i?=?0;?i?<?messages.length;?i++)?{
????????????????//?郵件發(fā)送者
????????????????String?from?=?decodeText(messages[i].getFrom()[0].toString());
????????????????InternetAddress?ia?=?new?InternetAddress(from);
????????????????logger.debug("發(fā)信人:"?+?ia.getPersonal()?+?'('
????????????????????????+?ia.getAddress()?+?')');
????????????????//?郵件標題
????????????????logger.debug("主題:"?+?messages[i].getSubject());
????????????????//?郵件大小
????????????????logger.debug("郵件大小:"?+?messages[i].getSize());
????????????????//?郵件發(fā)送時間
????????????????logger.debug("發(fā)送日期:"?+?messages[i].getSentDate());
????????????}
????????}?finally?{
????????????try?{
????????????????inbox.close(false);
????????????}?catch?(Exception?e)?{
????????????}
????????????try?{
????????????????store.close();
????????????}?catch?(Exception?e)?{
????????????}
????????}
????????
????????logger.debug("讀取郵件完畢
");
????}
????protected?static?String?decodeText(String?text)
????????????throws?UnsupportedEncodingException?{
????????if?(text?==?null)
????????????return?null;
????????if?(text.startsWith("=?GB")?||?text.startsWith("=?gb"))
????????????text?=?MimeUtility.decodeText(text);
????????else
????????????text?=?new?String(text.getBytes("ISO8859_1"));
????????return?text;
????}
}
上面代碼,完全可以封裝成一個收取ssl郵件的庫
改天有時間整理下關(guān)于發(fā)郵件的方法