mardi 14 avril 2015

ProtocolException with Amazon SimpleEmailService SDK for Java

I'm trying to send an email with the AWS Java SDK. I'm stuck using version 1.3.0 of the SDK at the moment. When I try to send an email, the SDK logs this:



- Sending Request: POST http://ift.tt/1Czzwtj / Parameters: (Source: sender@mydomain.com, Destination.ToAddresses.member.1: recipient@mydomain.com, Action: SendEmail, Message.Body.Text.Data: [My message body here], Version: 2010-12-01, Message.Subject.Data: [My message subject], )


...then pauses for a few seconds while it tries to send the email, then this stacktrace:



com.amazonaws.AmazonClientException: Unable to execute HTTP request: null
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:294)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:169)
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.invoke(AmazonSimpleEmailServiceClient.java:479)
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.sendEmail(AmazonSimpleEmailServiceClient.java:279)
at my.package.mail.MailUtils.send(MailUtils.java:82)
at my.package.mail.MailUtils$2.run(MailUtils.java:60)
at java.lang.Thread.run(Thread.java:701)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:262)
... 6 more
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:109)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)


This is the code I'm using:



public class MailUtils {

public static String MAIL_SENDER = "sender@mydomain.com";
public static AmazonSimpleEmailServiceClient SES_CLIENT;

static {
System.out.println("Initializing Amazon SES client");
SES_CLIENT = new AmazonSimpleEmailServiceClient(Config.MY_CREDS);
SES_CLIENT.setEndpoint("email-smtp.eu-west-1.amazonaws.com");
}

public static void send(String subject, String[] recipients, String msg) throws Exception {
Destination destination = new Destination().withToAddresses(recipients);
Content mailSubject = new Content().withData(subject);

Content emailBody = new Content().withData(msg);
Body body = new Body().withText(emailBody);

Message message = new Message().withSubject(mailSubject).withBody(body);
SendEmailRequest request = new SendEmailRequest()
.withSource(MAIL_SENDER)
.withDestination(destination)
.withMessage(message);

SES_CLIENT.sendEmail(request);
}


The credentials are valid because I use them successfully for other services. I'm using the endpoint string based on the value found at this page. There is an example of sending an email using the SDK at this page that uses the following technique for setting the region/endpoint:



Region REGION = Region.getRegion(Regions.US_WEST_2);
client.setRegion(REGION);


I have used these methods in another project without problems, but I can't use them here because the v1.3.0 of the SDK only has setEndpoint(String).


The sender address that I'm using is definitely valid (I've replaced it with sender@mydomain.com in the log output above, but the real one works). It has been whitelisted for my AWS account, and is in use in other projects.


Does anyone know why this might be failing?





Aucun commentaire:

Enregistrer un commentaire