I am currently using AWS SDK to send mails:
// from: [email protected]
public void sendMail(String from, String to, String subject, String htmlBody) {
LOGGER.info(String.format("Sending Mail from: %s, to: %s, with subject: %s", from, to, subject));
try {
AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecret);
AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
.withRegion(Regions.AP_SOUTH_1).withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
SendEmailRequest request = new SendEmailRequest().withDestination(new Destination().withToAddresses(to))
.withMessage(new Message()
.withBody(new Body().withHtml(new Content().withCharset("UTF-8").withData(htmlBody)))
.withSubject(new Content().withCharset("UTF-8").withData(subject)))
.withSource(from);
client.sendEmail(request);
} catch (Exception e) {
LOGGER.error(String.format("Error while sending email: %s", e.getMessage()));
throw new IllegalArgumentException(MessageEnum.FAILED_TO_SEND_AN_EMAIL.name());
}
}
The method works fine but the email is sent as:
But I am expecting the email to be sent with the sender name so that it can be displayed like:
Does anyone have any idea on how to send an email including the sender name? Any help is appreciated.
CodePudding user response:
If you check documentation of SendEmailRequest
you can find out that it supports standard way to define friendly name in email:
source - The email address that is sending the email. This email address must be either individually verified with Amazon SES, or from a domain that has been verified with Amazon SES. For information about verifying identities, see the Amazon SES Developer Guide. If you are sending on behalf of another user and have been permitted to do so by a sending authorization policy, then you must also specify the SourceArn parameter. For more information about sending authorization, see the Amazon SES Developer Guide. Amazon SES does not support the SMTPUTF8 extension, as described in RFC6531. For this reason, the local part of a source email address (the part of the email address that precedes the @ sign) may only contain 7-bit ASCII characters. If the domain part of an address (the part after the @ sign) contains non-ASCII characters, they must be encoded using Punycode, as described in RFC3492. The sender name (also known as the friendly name) may contain non-ASCII characters. These characters must be encoded using MIME encoded-word syntax, as described in RFC 2047. MIME encoded-word syntax uses the following form: =?charset?encoding?encoded-text?= .
So that string should look like:
John Doe <[email protected]>
InternetAddress
can be used for this, but you need to pay attention on encoding restrictions that SendEmailRequest
has for from
argument
request.withSource((new InternetAddress("[email protected]", "Your Name")).toString());
CodePudding user response:
You are using an old Amazon Simple Email Service API (V1). For best practice, use the SES Java V2 API. V2 packages always start with software.amazon...
The Amazon SDK team strongly recommends moving from V1 to V2:
The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8 and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.
You can set this information (sender name) by setting the SendEmailRequest object properly.
Here is the Java V2 SES example.
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.*;
import software.amazon.awssdk.services.ses.model.Message;
import software.amazon.awssdk.services.ses.model.Body;
import javax.mail.MessagingException;
public class SendMessageEmailRequest {
public static void main(String[] args) {
final String USAGE = "\n"
"Usage:\n"
" <sender> <recipient> <subject> \n\n"
"Where:\n"
" sender - an email address that represents the sender. \n"
" recipient - an email address that represents the recipient. \n"
" subject - the subject line. \n" ;
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String sender = "[email protected]" ;//args[0];
String recipient = "[email protected]" ;//args[1];
String subject = "Test Email"; //args[2];
Region region = Region.US_EAST_1;
SesClient client = SesClient.builder()
.region(region)
.build();
// The email body for non-HTML email clients
String bodyText = "Hello,\r\n" "See the list of customers. ";
// The HTML body of the email
String bodyHTML = "<html>" "<head></head>" "<body>" "<h1>Hello!</h1>"
"<p> See the list of customers.</p>" "</body>" "</html>";
try {
send(client, sender, recipient, subject, bodyText, bodyHTML);
client.close();
System.out.println("Done");
} catch (MessagingException e) {
e.getStackTrace();
}
}
public static void send(SesClient client,
String sender,
String recipient,
String subject,
String bodyText,
String bodyHTML
) throws MessagingException {
Destination destination = Destination.builder()
.toAddresses(recipient)
.build();
Content content = Content.builder()
.data(bodyHTML)
.build();
Content sub = Content.builder()
.data(subject)
.build();
Body body = Body.builder()
.html(content)
.build();
Message msg = Message.builder()
.subject(sub)
.body(body)
.build();
SendEmailRequest emailRequest = SendEmailRequest.builder()
.destination(destination)
.message(msg)
.source(sender)
.build();
try {
System.out.println("Attempting to send an email through Amazon SES " "using the AWS SDK for Java...");
client.sendEmail(emailRequest);
} catch (SesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}