- wuhua
- 等级:


- 性别:

- 文章: 546
- 积分: 1457
- 来自: 广州

|
兄弟我理论性的东西说不出来,不过实际运用咱还是有办法的
前几天由于工作需要,想了解下关于支持ssl的邮件收发,按照以前普通的做法是行不通的,所以就上网东找找,西瞧瞧。发现了个好东西,并且实验成功。
那天本想来javaeye看看有没有人有相关的经验,找了老半天,连个屁也没闻到,我就说我们,我们这些做程序员的不能老是吹吹水,谈谈道理,我们得拿出点实际的东西出来,就想fins一样,我就很佩服他的贡献精神。
不说废话了,看看源代码,大家有空也可以实验下。还真有用
java 代码
-
-
-
-
-
- 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";
-
-
- 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");
-
-
- Session session = Session.getDefaultInstance(props, null);
-
-
- URLName urln = new URLName("pop3", ApplicationContext.POP3, 995, null,
- ApplicationContext.GMAIL_MAIL_NAME, //这里替换您的gmail用户名
- 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邮件的库
改天有时间整理下关于发邮件的方法
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- easygodg
- 等级: 初级会员

- 文章: 2
- 积分: 4

|
这个东西前几天我刚改的可以根据各种配置发送邮件 :)
|
| 返回顶楼 |
|
|
- xuygfbi
- 等级: 初级会员

- 性别:

- 文章: 6
- 积分: 0

|
试了一下报错 [AUTH] Username and password not accepted. 我的用户名和密码没有写错啊
|
| 返回顶楼 |
|
|
- andyandyandy
- 等级: 初级会员

- 文章: 52
- 积分: 95

|
欣赏楼主的精神,可惜本人才疏学浅,hoho
|
| 返回顶楼 |
|
|
- jamesby
- 等级:


- 性别:

- 文章: 484
- 积分: 815

|
附件没有接收下来?
|
| 返回顶楼 |
|
|
- waryist
- 等级: 初级会员

- 文章: 9
- 积分: 0

|
晕,好多类包都引不进来!
|
| 返回顶楼 |
|
|
- janmy
- 等级: 初级会员

- 性别:

- 文章: 24
- 积分: 0
- 来自: 中国广东省广州市

|
Your account is not enabled for POP access. Please visit your Gmail settings page and enable your account for POP access.
我用你的程序试了一下,,出现了上面的异常,,可否解决一下
|
| 返回顶楼 |
|
|
- zmo_xu
- 等级: 初级会员

- 性别:

- 文章: 18
- 积分: 30
- 来自: 苏州

|
楼上的是不是因为没有设置 gmail的 pop3 断口 gmail端口和别的不一样的 去google看下哈
|
| 返回顶楼 |
|
|