Amazon SES 535身份验证凭据无效

时间:2014-01-30 09:22:44

标签: amazon-ses

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

  1. 用户(参见附图),密码
  2. 访问ID和访问密钥
  3. 两者都给出了同样的错误。我在家庭网络的eclipse环境中运行了这段代码。

    错误

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

    Amazon SES Users

    代码

    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();          
            }
        }
    }
    

6 个答案:

答案 0 :(得分:6)

  

重要

     

您的SMTP用户名和密码与您的AWS访问密钥ID和秘密访问密钥不同。请勿尝试使用您的AWS凭据对SMTP端点进行身份验证。有关凭据的更多信息,请参阅将凭据与Amazon SES一起使用。

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

答案 1 :(得分:4)

如果您使用的是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身份验证所需的用户名和密码。

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

答案 2 :(得分:2)

我们的问题解决了以下问题:AWS SMTP的凭据中包含特殊字符。凭证必须进行URL编码,然后在配置中提供。

答案 3 :(得分:0)

  

获取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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

上所述。