Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java mail API连接存储在本地服务器中,但在开发服务器中未连接_Java_Email - Fatal编程技术网

Java mail API连接存储在本地服务器中,但在开发服务器中未连接

Java mail API连接存储在本地服务器中,但在开发服务器中未连接,java,email,Java,Email,我是Java邮件API新手,我曾尝试使用Tomcat服务器实现一个web应用程序,该服务器读取我的gmail收件箱,工作正常。但当我将代码上传到开发服务器时,它会抛出以下异常 javax.mail.AuthenticationFailedException: [ALERT] Please log in via your web browser: http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure)

我是Java邮件API新手,我曾尝试使用Tomcat服务器实现一个web应用程序,该服务器读取我的gmail收件箱,工作正常。但当我将代码上传到开发服务器时,它会抛出以下异常

javax.mail.AuthenticationFailedException: [ALERT] Please log in via your web browser: http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:660)
    at javax.mail.Service.connect(Service.java:317)
    at sales_order.ReadInquiryViaEmail.run(ReadInquiryViaEmail.java:89)
    at java.lang.Thread.run(Thread.java:662)
我的代码是

public class ReadInquiryViaEmail implements Runnable {

    public ReadInquiryViaEmail(ServletContext context, String emailAddress,
                               String domainName, String protocol, String password) {

        this.sContext = context;
        this.domainName = domainName;
        this.emailAddress = emailAddress;
        this.password = password;
        this.protocol = protocol;
    }

    public void run() {
        Properties imapProps;=new Properties();
        imapProps.setProperty("mail.host", domainName);
        imapProps.setProperty("mail.port", "993");
        imapProps.setProperty("mail.transport.protocol", protocol);
        imapProps.setProperty("mail.imaps.starttls.enable", "true");
        imapProps.setProperty("username", emailAddress);
        imapProps.setProperty("password", password);

        int port = Integer.parseInt("993");

        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailAddress, password);
            }
        };

        Session mailSession = Session.getInstance(imapProps, authenticator);

        try {

            Store store = mailSession.getStore(protocol);
            store.connect(domainName, port, emailAddress, password);

            Folder folder = store.getFolder("Inbox");
            folder.open(Folder.READ_WRITE);

            folder.getMessages();
            FlagTerm term = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
            Message[] messages = folder.search(term);
            System.out.println("messages.length > " + messages.length);
            if (messages.length > 0) {
                InternetAddress[] autoResponseEmailAddress =
                        new InternetAddress[messages.length];

                String autoResponseNames[] = new String[messages.length];
                Inquiry[] emailInquiries = new Inquiry[messages.length];
                String name = "", subject, emailAddress;

                int index = 0;
                System.out.println(" reading mails start ");
                for (int j = (messages.length - 1); j > (-1); j--, index++) {
                    messages[j].setFlag(Flags.Flag.SEEN, true);

                    Address address = messages[j].getFrom()[0];

                    autoResponseEmailAddress[j] = (InternetAddress) address;
                    autoResponseNames[j] = autoResponseEmailAddress[j].getPersonal();
                    emailAddress = autoResponseEmailAddress[j].getAddress();

                    name = autoResponseNames[j];
                    subject = messages[j].getSubject();

                    System.out.println(" name " + name);
                    System.out.println(" subject " + subject);

                }
            }
            folder.close(false);
            store.close();
            System.out.println("\n\ndone reading mails\n\n");
        } catch (javax.mail.NoSuchProviderException nspe) {
            logError(nspe);
        } catch (MessagingException e) {
            logError(e);
        } catch (Exception exp) {
            logError(exp);
        }
    }
}
上面的代码有什么遗漏吗

请帮忙


提前感谢。

谷歌会阻止身份验证尝试。首先尝试从浏览器登录到Gmail帐户。并确保您使用相同的帐户:

还要在代码中添加以下内容:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");

这是由于默认情况下gmail安全功能处于打开状态

要解决此问题,请执行以下操作:

  • 从web浏览器手动登录您的gmail帐户

  • 转到-选择打开