使用javamail,gmail因应用程序不太安全而拒绝身份验证 [英] Using javamail, gmail refusing authentication due to application being less secure

查看:157
本文介绍了使用javamail,gmail因应用程序不太安全而拒绝身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个非常基本的Javamail程序来尝试发送电子邮件。这是带main()的独立程序。一旦我开始工作,我计划在一个在tomcat下运行的servlet中使用Javamail。

I am running a very basic Javamail program to try sending emails. This is stand-alone program with main(). Once I get it working, I plan to use Javamail in a servlet running under tomcat.

我在运行这个程序时遇到AUTH LOGIN失败错误。我尝试了几种不同的属性设置,但都没有解决问题。

I was getting AUTH LOGIN failed error when running this program. I tried several different property settings, none of which solved the problem.

然后我在SO上发现了一条帖子,建议降低我的Google帐户所需的安全级别。当我降低安全设置时,身份验证成功。

Then I found a post on SO, which suggested lowering the required security level in my Google account. The authentication was successful when I lowered the security setting.

当然,我立即回到Google帐户的更高安全级别。

Of course, I went back to higher security level on the Google account immediately.

我的问题是,如何让我的应用程序更安全,以便gmail不拒绝身份验证?

The question I have is, how can I make my application more secure so that gmail does not refuse authentication?

程序代码如下所示。该程序非常类似于SO上的许多其他Javamail问题中的代码。

Program code shown below. The program is very similar to code in many other Javamail questions on SO.

TryJavamail.java

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class TryJavamail {

  public static void main(String args[]) throws MessagingException {

    String submitName = "John Doe";
    String submitEmail = "from@example.com";
    String submitMessage = "This is the message";

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.ssl.enable", "true");
    props.setProperty("mail.smtp.port", "465");
    Session session = Session.getInstance(props, null);

    session.setDebug(true);

    Message message = new MimeMessage(session);
    message.setSubject("Message from myapp website submit");
    message.setText(submitName + "; " + submitMessage);

    Address toAddress = new InternetAddress(submitEmail);
    message.setRecipient(Message.RecipientType.TO, toAddress);

    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.gmail.com", "---userid---", "---password---");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
  }
}


推荐答案

你可能想使用 OAuth2身份验证

这篇关于使用javamail,gmail因应用程序不太安全而拒绝身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆