【问题标题】:Amazon SES 535 Authentication Credentials InvalidAmazon SES 535 身份验证凭证无效
【发布时间】:2021-06-15 02:48:17
【问题描述】:

在下面的代码中我尝试了 2 种组合

  1. 用户(参考附图),密码
  2. 访问 ID 和访问密钥

两者都给出了相同的错误。我在家庭网络的 eclipse 环境中运行了这段代码。

错误

Attempting to send an email through the Amazon SES SMTP interface...
The email was not sent.
Error message: 535 Authentication Credentials Invalid

代码

public class amazon_ses_smtp_test {

    static final String FROM = "someone@myemail.com";   // This has been verified on the Amazon SES setup
    static final String TO = "justanyone@anydomain";  // I have production access

    static final String BODY = "This email was sent through the Amazon SES SMTP interface by using Java.";
    static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";

    static final String SMTP_USERNAME = "tried username and tried accesskey here";
    static final String SMTP_PASSWORD = "tried the password and the access key"; 

    static final String HOST = "email-smtp.us-east-1.amazonaws.com";    

    static final int PORT = 25;

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

        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", PORT); 

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");

        Session session = Session.getDefaultInstance(props);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY,"text/plain");

        Transport transport = session.getTransport();

        try
        {
            System.out.println("Attempting to send an email through the Amazon SES SMTP interface...");

            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Email sent!");
        }
        catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        }
        finally
        {
            transport.close();          
        }
    }
}

【问题讨论】:

标签: amazon-ses


【解决方案1】:

重要

您的 SMTP 用户名和密码与您的 AWS 访问密钥 ID 和秘密访问密钥不同。不要尝试使用您的 AWS 凭证针对 SMTP 终端节点对自己进行身份验证。有关凭证的更多信息,请参阅将凭证与 Amazon SES 结合使用。

这是链接:http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html

【讨论】:

【解决方案2】:

在我们的案例中,问题通过以下方式得到解决:AWS SMTP 在其凭证中有特殊字符。凭据必须经过 url 编码,然后在配置中提供。

【讨论】:

  • 遇到了同样的问题。使用 MAILER_URL 时,密码必须是 url 编码
  • 天哪,我遇到了同样的问题。谢谢你们,否则我不会发现这一点。 AWS SES 生成的密码是 A-z 0-9,但它的末尾也有一个 / (UGH),因为 4 年后事实证明它的设计仍然很糟糕。将其转义为 %2F 为我的用例修复了它。
  • 编码为?什么格式。坚持这一点,但也不确定要使用哪个用户名
【解决方案3】:

如果您使用的是 Terraform,则可以使用以下 .tf 文件,以便获取正确的数据

resource "aws_iam_user" "smtp_user" {
  name = "smtp_user"
}

resource "aws_iam_access_key" "smtp_user" {
  user = aws_iam_user.smtp_user.name
}

data "aws_iam_policy_document" "ses_sender" {
  statement {
    actions   = ["ses:SendRawEmail"]
    resources = ["*"]
  }
}

resource "aws_iam_policy" "ses_sender" {
  name        = "ses_sender"
  description = "Allows sending of e-mails via Simple Email Service"
  policy      = data.aws_iam_policy_document.ses_sender.json
}

resource "aws_iam_user_policy_attachment" "test-attach" {
  user       = aws_iam_user.smtp_user.name
  policy_arn = aws_iam_policy.ses_sender.arn
}

output "smtp_username" {
  value = aws_iam_access_key.smtp_user.id
}

output "smtp_password" {
  value = aws_iam_access_key.smtp_user.ses_smtp_password_v4
}

这将输出该区域的 SMTP 身份验证所需的用户名和密码。

这将对密码进行正确的计算。

【讨论】:

  • 试过了,看起来不错,但仍然不适合我。有没有办法为这个 tf 模块指定区域?
  • 我认为你是在你的提供商上设置的。
  • 我发现了我的问题。我需要将 MIME 版本和内容类型指定为电子邮件中的标题并使用端口 587,因为我正在发送 HTML 模板电子邮件。这个 Terraform 模块运行良好。
【解决方案4】:

我发现创建 SES SMTP 凭据是不够的。完成此操作后,转到域,选择您的域、身份策略、策略生成器。您需要获取 SMTP 用户的 ARN,您可以在 IAM 部分找到它。

【讨论】:

    【解决方案5】:

    smtp 密码与秘密访问密钥不同。可以从您的访问密钥中获取 smtp 密码,如 AWS 文档中所述:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html#smtp-credentials-convert

    【讨论】:

    • 很少有人会同时使用两者。但肯定是一个有用的答案。
    • @Siddharth 我知道,我也觉得这很奇怪。但请检查我添加到答案中的链接。
    【解决方案6】:

    获取您的 Amazon SES SMTP 凭证

    您需要 Amazon SES SMTP 用户名和密码才能访问 Amazon SES SMTP 接口。您可以在所有 AWS 区域使用同一组 SMTP 凭证。

    重要

    您的 SMTP 用户名和密码与您的 AWS 访问密钥 ID 和秘密访问密钥不同。有关凭证的更多信息,请参阅将凭证与 Amazon SES 结合使用。

    按照此文档获取 SMTP 凭据

    https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2014-10-15
      • 2016-11-19
      • 2020-04-10
      • 1970-01-01
      相关资源
      最近更新 更多