An example of sending email with an application through Amazon in Java

Does anyone have an example of sending email with an application through Amazon SES (in Java)?

+8
source share
4 answers

Maybe a little late, but you can use this code (you also need Java Mail):

public class MailSender { private Transport AWSTransport; ... //Initialize transport private void initAWSTransport() throws MessagingException { String keyID = <your key id> String secretKey = <your secret key> MailAWSCredentials credentials = new MailAWSCredentials(); credentials.setCredentials(keyID, secretKey); AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "aws"); props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId()); props.setProperty("mail.aws.password", credentials.getAWSSecretKey()); AWSsession = Session.getInstance(props); AWStransport = new AWSJavaMailTransport(AWSsession, null); AWStransport.connect(); } public void sendEmail(byte[] attachment) { //mail properties String senderAddress = <Sender address>; String recipientAddress = <Recipient address>; String subject = <Mail subject>; String text = <Your text>; String mimeTypeOfText = <MIME type of text part>; String fileMimeType = <MIME type of your attachment>; String fileName = <Name of attached file>; initAWSTransport(); try { // Create new message Message msg = new MimeMessage(AWSsession); msg.setFrom(new InternetAddress(senderAddress)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress)); msg.setSubject(subject); //Text part Multipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(text, mimeTypeOfText); multipart.addBodyPart(messageBodyPart); //Attachment part if (attachment != null && attachment.length != 0) { messageBodyPart = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(attachment,fileMimeType); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName); multipart.addBodyPart(messageBodyPart); } msg.setContent(multipart); //send message msg.saveChanges(); AWSTransport.sendMessage(msg, null); } catch (MessagingException e){...} } } 
+14
source

Maybe a little late. Alternative for sending mail using Java Mail and Amazon mailing sender

 public static void sendMail(String subject, String message, byte[] attachement, String fileName, String contentType, String from, String[] to) { try { // JavaMail representation of the message Session s = Session.getInstance(new Properties(), null); MimeMessage mimeMessage = new MimeMessage(s); // Sender and recipient mimeMessage.setFrom(new InternetAddress(from)); for (String toMail : to) { mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toMail)); } // Subject mimeMessage.setSubject(subject); // Add a MIME part to the message MimeMultipart mimeBodyPart = new MimeMultipart(); BodyPart part = new MimeBodyPart(); part.setContent(message, MediaType.TEXT_HTML); mimeBodyPart.addBodyPart(part); // Add a attachement to the message part = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(attachement, contentType); part.setDataHandler(new DataHandler(source)); part.setFileName(fileName); mimeBodyPart.addBodyPart(part); mimeMessage.setContent(mimeBodyPart); // Create Raw message ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); mimeMessage.writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray())); // Credentials String keyID = "";// <your key id> String secretKey = "";// <your secret key> AWSCredentials credentials = new BasicAWSCredentials(keyID, secretKey); AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials); // Send Mail SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage); rawEmailRequest.setDestinations(Arrays.asList(to)); rawEmailRequest.setSource(from); client.sendRawEmail(rawEmailRequest); } catch (IOException | MessagingException e) { // your Exception e.printStackTrace(); } } 
+13
source

As of 2014, some of the Amazon APIs have changed. Here is a working example:

http://mintylikejava.blogspot.hk/2014/05/example-of-sending-email-with-multipal.html

+3
source

Here is an updated, cleaned version with registration and production verification.

 public void sendEmail(String to, String subject, String body, String attachment, String mimeType, String fileName) { if (to == null) return; String environment = System.getProperty("ENVIRONMENT", System.getenv("ENVIRONMENT")); String logMessage; if (environment != null && environment.equals("production")) { logMessage = "Sent email to " + to + "."; } else { to = " success@simulator.amazonses.com "; logMessage = "Email sent to success@simulator.amazonses.com because $ENVIRONMENT != 'production'"; } // https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-raw-using-sdk.html Session session = Session.getDefaultInstance(new Properties()); MimeMessage message = new MimeMessage(session); try { message.setSubject(subject, "UTF-8"); message.setFrom(new InternetAddress(FROM)); message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to)); MimeMultipart msg = new MimeMultipart("mixed"); MimeBodyPart wrap = new MimeBodyPart(); MimeMultipart msgBody = new MimeMultipart("alternative"); MimeBodyPart textPart = new MimeBodyPart(); MimeBodyPart htmlPart = new MimeBodyPart(); textPart.setContent(body, "text/plain; charset=UTF-8"); htmlPart.setContent(body,"text/html; charset=UTF-8"); msgBody.addBodyPart(textPart); msgBody.addBodyPart(htmlPart); wrap.setContent(msgBody); msg.addBodyPart(wrap); MimeBodyPart att = new MimeBodyPart(); att.setDataHandler(new DataHandler(attachment, mimeType)); att.setFileName(fileName); // DataSource fds = new FileDataSource(attachment); // att.setDataHandler(new DataHandler(fds)); // att.setFileName(fds.getName()); msg.addBodyPart(att); message.setContent(msg); AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder .standard().withRegion(Regions.US_EAST_1).build(); message.writeTo(System.out); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); message.writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray())); SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage); // .withConfigurationSetName(CONFIGURATION_SET); client.sendRawEmail(rawEmailRequest); Logger.info(this.getClass(), "sendEmail()", logMessage); } catch (Exception ex) { Logger.info(this.getClass(), "sendEmail()", "The email was not sent. Error: " + ex.getMessage()); } } 
0
source

All Articles