zoukankan      html  css  js  c++  java
  • POP3_收取QQ邮箱邮件的问题

    今天纠结了一天的时间,使用pop3协议收取qq邮箱中邮件时,因为一个特别坑爹的问题重新写n次,最后发现是因为qq邮箱设置了独立邮箱密码,必须的用独立邮箱密码登陆才行,/(ㄒoㄒ)/~~!!!!

    但今天干的活也不能白干,所以发出来记录一下代码。

    package com.lkb.thirdUtil.emailbank.test;
    
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class MailExample {
    
      public static void main (String args[]) throws Exception {
        
    
        // Get system properties
        Properties props = System.getProperties();
    
        // Setup mail server
        props.put("mail.pop3.host", "pop.qq.com"); 
        props.put("mail.pop3.port", "995"); 
        // Get session
        props.put("mail.smtp.auth", "true"); //这样才能通过验证
        props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.pop3.socketFactory.fallback", "false");
        props.setProperty("mail.pop3.socketFactory.port", "995"); 
    
        MyAuthenticator myauth = new MyAuthenticator("*****@qq.com", "******");
        Session session = Session.getDefaultInstance(props, myauth);
        Store store = session.getStore("pop3"); 
        store.connect();
        //session.setDebug(true);
        System.out.println("成功!");
        Folder folder = store.getFolder("INBOX"); 
        // 只读 
        folder.open(Folder.READ_ONLY);
        Message[] mess=folder.getMessages();
        for (Message message : mess) {
            System.out.println(message.getSubject());
        }
      }
    }

    --------------------------------------------------------

    package com.lkb.thirdUtil.emailbank.test;
    
    import javax.mail.PasswordAuthentication;
    
    class MyAuthenticator
          extends javax.mail.Authenticator {
        private String strUser;
        private String strPwd;
        public MyAuthenticator(String user, String password) {
          this.strUser = user;
          this.strPwd = password;
        }
    
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(strUser, strPwd);
        }
      }

    IMAP形式:

    package com.lkb.thirdUtil.emailbank.test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Folder;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Store;
    public class ImapProtocolImpl extends Authenticator 
    {
        private Session session;
        private PasswordAuthentication authentication;
        
        public ImapProtocolImpl(String username, String password)
        {
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.qq.com");
            props.setProperty("mail.imap.port", "993");
            props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.imap.socketFactory.fallback", "false");
            props.setProperty("mail.imap.auth.login.disable", "true");
            props.setProperty("mail.imap.auth.plain.disable","true");
            
            authentication = new PasswordAuthentication(username, password);
            session = Session.getInstance(props, this);
            session.setDebug(true);
        }
        
        @Override
        public PasswordAuthentication getPasswordAuthentication()
        {
            return this.authentication;
        }
        
        public void connect()
        {
            try
            {
                Store store = session.getStore();
                store.connect();
                Folder root = store.getDefaultFolder();
                Folder inbox = root.getFolder("inbox");
                inbox.open(Folder.READ_WRITE);
                System.out.println(inbox.getMessageCount());
            }
            catch (MessagingException e)
            {
                try
                {
                    byte[] buf = e.getMessage().getBytes("ISO-8859-1");
                    System.out.println(new String(buf, "GBK"));
                }
                catch (UnsupportedEncodingException e1)
                {
                    e1.printStackTrace();
                }
                throw new RuntimeException("登录失败", e);
            }
        }
    }
  • 相关阅读:
    python 函数嵌套
    python 函数对象
    python 函数参数
    python 连接MySQL报错及解决方案
    解决 No module named pip
    python 文件处理
    python
    python 元祖
    python 读取域名信息
    ubuntu 配置网卡,DNS, iptables
  • 原文地址:https://www.cnblogs.com/gisblogs/p/4363638.html
Copyright © 2011-2022 走看看