SignatureDoesNotMatch,我们计算的请求签名与您提供的签名不匹配

时间:2014-01-30 06:19:25

标签: amazon-web-services amazon-ses

我已经查看了其他关于SO的问题,所有这些问题都没有SES和Java作为他们问题的一部分。

异常

Exception in thread "main" com.amazonaws.AmazonServiceException: Status Code: 403, AWS Service: AmazonSimpleEmailService, AWS Request ID: 6828fb27-8972-11e3-9700-b705133266ce, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:773)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:417)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:229)
    at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.invoke(AmazonSimpleEmailServiceClient.java:1254)
    at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.listVerifiedEmailAddresses(AmazonSimpleEmailServiceClient.java:296)
    at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.listVerifiedEmailAddresses(AmazonSimpleEmailServiceClient.java:1132)
    at com.mcruiseon.server.amazonses.test.AWSJavaMailSample.verifyEmailAddress(AWSJavaMailSample.java:169)
    at com.mcruiseon.server.amazonses.test.AWSJavaMailSample.main(AWSJavaMailSample.java:84)

源代码

public class AWSJavaMailSample {
    private static final String TO = "personalemail@gmail.com";
    private static final String FROM = "admin@myproductdomain.com";
    private static final String BODY = "Hello World!";
    private static final String SUBJECT = "Hello World!";

    public static void main(String[] args) throws IOException {
        AWSCredentials credentials = new ClasspathPropertiesFileCredentialsProvider()
                .getCredentials();
        AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(
                credentials);
        Region usWest2 = Region.getRegion(Regions.US_EAST_1);
        ses.setRegion(usWest2);
        verifyEmailAddress(ses, FROM);
        // verifyEmailAddress(ses, TO);
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "aws");
        props.setProperty("mail.aws.user", "ses-smtp-user.some_number") ; 
        props.setProperty("mail.aws.password", "password_setup_for_this_user");
        Session session = Session.getInstance(props);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(FROM));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
            msg.setSubject(SUBJECT);
            msg.setText(BODY);
            msg.saveChanges();

            Transport t = new AWSJavaMailTransport(session, null);
            t.connect();
            t.sendMessage(msg, null);

            t.close();
        } catch (AddressException e) {
            e.printStackTrace();
            System.out
                    .println("Caught an AddressException, which means one or more of your "
                            + "addresses are improperly formatted.");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out
                    .println("Caught a MessagingException, which means that there was a "
                            + "problem sending your message to Amazon's E-mail Service check the "
                            + "stack trace for more information.");
        }
    }
    private static void verifyEmailAddress(AmazonSimpleEmailService ses,
            String address) {
        ListVerifiedEmailAddressesResult verifiedEmails = ses
                .listVerifiedEmailAddresses();
        if (verifiedEmails.getVerifiedEmailAddresses().contains(address))
            return;

        ses.verifyEmailAddress(new VerifyEmailAddressRequest()
                .withEmailAddress(address));
        System.out.println("Please check the email address " + address
                + " to verify it");
        System.exit(0);
    }
}

SES设置

  1. 将myproductdomain.com添加到域名。
  2. 将admin@myproductdomain.com添加到电子邮件地址。
  3. AwsCredentials.properties

    1. 对于secretKey =“aws网站登录密码”
    2. 对于accesskey =“https://console.aws.amazon.com/iam/home#users,安全凭证,对于ses-smtp-user.some_number,访问密钥”
    3. 我正在尝试从eclipse插件中获取SES的“hello world”。应该是开箱即用的。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您使用的是" ses-smtp-user.some_number"。 您可以看到,使用SES,有两种方式可以发送电子邮件。

  • 使用网络服务(AmazonSimpleEmailServiceClient)
  • 使用SMTP

创建的用户是smtp,ses-smtp-user.some_number。 您正在使用该凭据进行Web服务调用。

我的建议是创建一个普通用户(不是来自SES屏幕),并分配SES权限。 请注意,新创建的凭据现在无法使用SMTP。

如果您想要一个同时适用于SMTP和Web服务的凭据,您可能需要阅读以下内容:http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html#smtp-credentials-convert