text AWS SES
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text AWS SES相关的知识,希望对你有一定的参考价值。
package notification;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.*;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.typesafe.config.Config;
import exceptions.BadResponseException;
import play.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* @author James Lin
*/
public class SESServiceImpl implements EmailService {
private static final Logger.ALogger logger = Logger.of(SESServiceImpl.class);
private AmazonSimpleEmailService client;
private static String SENDER;
private static final int MAX_RECIPIENTS = 50; // AWS SES 限制每次的有收信者最多 50 人
@Inject
public SESServiceImpl(Config config) {
client = AmazonSimpleEmailServiceClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
SENDER = config.getString("email.sender");
}
@Override
public void send(Notification notification) {
Preconditions.checkArgument((notification.users != null && !notification.users.isEmpty()) &&
(notification.message.type.isPublic() || (notification.users.size() == 1 && notification.message.template != null)));
try {
List<String> toAddresses = notification.users.stream().map(user -> user.email).collect(Collectors.toList());
int fromIndex = 0;
while(fromIndex < toAddresses.size()) {
int toIndex = (fromIndex + MAX_RECIPIENTS) < toAddresses.size() ? (fromIndex + MAX_RECIPIENTS) : toAddresses.size();
SendEmailRequest request = new SendEmailRequest().withDestination(new Destination().withToAddresses(toAddresses.subList(fromIndex, toIndex)))
.withSource(SENDER).withMessage(new com.amazonaws.services.simpleemail.model.Message()
.withSubject(new Content().withCharset("UTF-8").withData(notification.message.title))
.withBody(new Body().withHtml(new Content().withCharset("UTF-8").withData(notification.message.content))));
client.sendEmail(request);
fromIndex += MAX_RECIPIENTS;
if(fromIndex < toAddresses.size()) {
Thread.sleep(3000); // 等X秒再寄信,比較不會被擋 ???
}
}
} catch (Throwable e) {
throw new BadResponseException(String.format("fail to send email %s", notification), e, "AWS-SES");
}
}
@Override
public void send(EmailMessage myMessage) {
try {
Session session = Session.getDefaultInstance(new Properties());
MimeMessage javaMailMessage = new MimeMessage(session);
javaMailMessage.setSubject(myMessage.subject, "UTF-8");
javaMailMessage.setFrom(new InternetAddress(SENDER));
javaMailMessage.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(Joiner.on(",").join(myMessage.addresses)));
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(myMessage.body, "text/html; charset=UTF-8");
multipart.addBodyPart(htmlBodyPart);
MimeBodyPart attachmentPart = new MimeBodyPart();
for(EmailMessage.Attachment attachment: myMessage.attachments) {
DataSource fds = new ByteArrayDataSource(attachment.content, attachment.mimeType);
((ByteArrayDataSource) fds).setName(attachment.fileName);
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName(fds.getName());
multipart.addBodyPart(attachmentPart);
}
javaMailMessage.setContent(multipart);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
javaMailMessage.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest request = new SendRawEmailRequest(rawMessage);
client.sendRawEmail(request);
} catch (Throwable e) {
throw new BadResponseException(String.format("fail to send email %s", myMessage), e, "AWS-SES");
}
}
}
public class EmailMessage {
public EmailMessage(String subject, String body, Set<String> addresses) {
this.subject = subject;
this.body = body;
this.addresses = addresses;
}
public String subject;
public String body;
public Set<String> addresses;
public List<Attachment> attachments = new ArrayList<>();
public static class Attachment {
public String mimeType = "application/vnd.ms-excel";
public String fileName;
public InputStream content;
public Attachment(String fileName, InputStream content) {
this.fileName = fileName;
this.content = content;
}
}
}
public class Notification {
public Message message;
public Set<User> users;
public Notification(Message message, Set<User> users) {
this.message = message;
this.users = users;
}
}
以上是关于text AWS SES的主要内容,如果未能解决你的问题,请参考以下文章