我的知识库

知识等于力量

« 基金应该这样买ActiveMQ4.1 +Spring2.0的POJO JMS方案 扩展,以更加实用(基于ss) »

一个读取Gmail邮件的简单程序

      兄弟我理论性的东西说不出来,不过实际运用咱还是有办法的
前几天由于工作需要,想了解下关于支持ssl的邮件收发,按照以前普通的做法是行不通的,所以就上网东找找,西瞧瞧。发现了个好东西,并且实验成功。
      那天本想来javaeye看看有没有人有相关的经验,找了老半天,连个屁也没闻到,我就说我们,我们这些做程序员的不能老是吹吹水,谈谈道理,我们得拿出点实际的东西出来,就想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);

        
// 请将红色部分对应替换成你的邮箱帐号和密码
        URLName urln = new URLName("pop3", ApplicationContext.POP3, 995null,
                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(
"收件箱的邮件数:" + messages.length);
            
for (int i = 0; i < messages.length; i++) {
                
// 邮件发送者
                String from = decodeText(messages[i].getFrom()[0].toString());
                InternetAddress ia 
= new InternetAddress(from);
                logger.debug(
"发信人:" + ia.getPersonal() + '('
                        
+ ia.getAddress() + ')');
                
// 邮件标题
                logger.debug("主题:" + messages[i].getSubject());
                
// 邮件大小
                logger.debug("邮件大小:" + messages[i].getSize());
                
// 邮件发送时间
                logger.debug("发送日期:" + 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);

        
// 请将红色部分对应替换成你的邮箱帐号和密码
        URLName urln = new URLName("pop3", ApplicationContext.POP3, 995null,
                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(
"收件箱的邮件数:" + messages.length);
            
for (int i = 0; i < messages.length; i++) {
                
// 邮件发送者
                String from = decodeText(messages[i].getFrom()[0].toString());
                InternetAddress ia 
= new InternetAddress(from);
                logger.debug(
"发信人:" + ia.getPersonal() + '('
                        
+ ia.getAddress() + ')');
                
// 邮件标题
                logger.debug("主题:" + messages[i].getSubject());
                
// 邮件大小
                logger.debug("邮件大小:" + messages[i].getSize());
                
// 邮件发送时间
                logger.debug("发送日期:" + 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邮件的库
改天有时间整理下关于发邮件的方法

Search

导航

热门文章

最新文章

Powered By duduwolf's wiki 1.0

Copyright 1999-2007 duduwolf.com Some Rights Reserved.