關(guān)鍵字: 企業(yè)應(yīng)用 ?
????
????? 兄弟我理論性的東西說(shuō)不出來(lái),不過(guò)實(shí)際運(yùn)用咱還是有辦法的
前幾天由于工作需要,想了解下關(guān)于支持ssl的郵件收發(fā),按照以前普通的做法是行不通的,所以就上網(wǎng)東找找,西瞧瞧。發(fā)現(xiàn)了個(gè)好東西,并且實(shí)驗(yàn)成功。
????? 那天本想來(lái)javaeye看看有沒(méi)有人有相關(guān)的經(jīng)驗(yàn),找了老半天,連個(gè)屁也沒(méi)聞到,我就說(shuō)我們,我們這些做程序員的不能老是吹吹水,談?wù)劦览恚覀兊媚贸鳇c(diǎn)實(shí)際的東西出來(lái),就想fins一樣,我就很佩服他的貢獻(xiàn)精神。
????? 不說(shuō)廢話了,看看源代碼,大家有空也可以實(shí)驗(yàn)下。還真有用
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);
????????//?請(qǐng)將紅色部分對(duì)應(yīng)替換成你的郵箱帳號(hào)和密碼
????????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()?+?')');
????????????????//?郵件標(biāo)題
????????????????logger.debug("主題:"?+?messages[i].getSubject());
????????????????//?郵件大小
????????????????logger.debug("郵件大小:"?+?messages[i].getSize());
????????????????//?郵件發(fā)送時(shí)間
????????????????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);
????????//?請(qǐng)將紅色部分對(duì)應(yīng)替換成你的郵箱帳號(hào)和密碼
????????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()?+?')');
????????????????//?郵件標(biāo)題
????????????????logger.debug("主題:"?+?messages[i].getSubject());
????????????????//?郵件大小
????????????????logger.debug("郵件大小:"?+?messages[i].getSize());
????????????????//?郵件發(fā)送時(shí)間
????????????????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;
????}
}
上面代碼,完全可以封裝成一個(gè)收取ssl郵件的庫(kù)
改天有時(shí)間整理下關(guān)于發(fā)郵件的方法