【问题标题】:Issue sending email using gmail's smtp server and id of my website使用 gmail 的 smtp 服务器和我的网站 ID 发送电子邮件问题
【发布时间】:2012-01-18 10:23:56
【问题描述】:

我使用 google 的 gmail 应用程序为我的网站管理员创建了一个电子邮件帐户 admin@mywebsite.com。我可以使用 gmail 登录网页上的凭据登录。 现在我想使用 java 和这个 id 发送电子邮件

String host = "smtp.gmail.com";
    String from = "admin@mywebsite.com";
    String pass = "myPass";
    Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
....
...
...
Session session = Session.getDefaultInstance(props, auth);
...
...
Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);

    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

这是抛出异常 线程“主”javax.mail.AuthenticationFailedException 中的异常

但是,当我使用 gmail 凭据 (myid@gmail.com) 尝试此操作时,它可以工作。 请帮我解决这个问题。提前致谢

【问题讨论】:

标签: java email


【解决方案1】:

What mechanism does Gmail use for user authentication? 的接受回答来看,你需要在端口465 或587 上连接到谷歌的邮件服务器,然后使用STARTTLS(大概只在端口587 上)和用户身份验证。所以你缺少的是端口 465 或 587 部分。

【讨论】:

  • 你的意思是说我应该使用语句 props.put("mail.smtp.port", "465");而不是 props.put("mail.smtp.port", "587");
  • 我没有看到在您发布的代码中的任何位置设置了任何端口号。所以它使用 SMTP 默认值 25,在这种特殊情况下似乎是错误的。
【解决方案2】:
  • 您是否已在您的 gmail 帐户设置中使用“代理发送”权限对 admin@mywebsite.com 进行了身份验证?
  • 如果是,那么您可能需要提供您的身份验证用户 ID 和密码作为您的 gmail 帐户。

【讨论】:

  • 抱歉,没听懂。请详细说明
  • 如果你去 gmail --> Settings --> Account settings,会有一个 Send Mail As 选项组。您应该首先为此启用 admin@mywebsite.com 帐户。在此之后,您可能必须使用“admin@mywebsite.com”作为发件人地址,使用“yourgmailid@gmail.com”作为用户 ID,使用“yourgmailpassword”作为密码。希望这会有所帮助。
  • 对不起,伙计们,我今天要走了。但我还没有解决这个问题。将在 15 小时后返回
  • 大家好,今天成功了。我今天早上刚来,试了一下,可以使用java发送邮件。我能想到这种行为的一个原因是我昨天才创建了这个帐户,而 gmail 需要一些时间来同步到它的所有服务器并授予通过 API 发送消息的权限
【解决方案3】:

您必须使用端口号才能连接 gmail。 TLS:587 SSL:465

您必须使用其中之一,才能通过 gmail 发送电子邮件。

【讨论】:

    猜你喜欢
    • 2016-09-07
    • 2014-10-28
    • 1970-01-01
    • 2023-03-07
    • 2015-05-10
    • 1970-01-01
    • 2022-01-09
    • 2014-10-15
    相关资源
    最近更新 更多