[英]AWSSDK V2 Java SES sending email - AmazonSimpleEmailService cannot be resolved
[英]Sending emails using AWS SES v2 with attachments
我試圖構建一個 spring 啟動 api 以使用 AWS SES v2 發送電子郵件,但我不知道如何在最后階段使用 SES v2 的客戶端發送 email。 對於 SES 版本 1,我們可以制作 RawMessage object,然后使用SendRawEmailRequest
構建消息,最后使用客戶端 ( client.sendEmail(rawEmailRequest);
) 發送它,但對於 v2,不幸的是,客戶端不接受SendRawEmailRequest
' s object 作為參數,我沒有看到任何關於如何實現它的文檔。 如果有人可以幫助我解決這個問題,那將很有幫助。
這是來自 AWS 網站的 AWS 代碼示例的鏈接,我已將其作為參考:
下面是我使用 SES v2 客戶端發送帶有附件的 email 的代碼:
@Override
public EmailContent sendemailAttachment() throws MessagingException, IOException {
SesV2Client client = awssesConfig.getAmazonSESCient();
final String sender = "dibyanshu.chatterjee@phoenix.edu";
final String recipient = "alpeshkumar.patel@phoenix.edu";
final String subject = "SES test";
final String bodyText = "Testing out SES";
final String bodyHTML = "";
final String fileLocation = "C:\\Users\\dchatter\\Downloads\\vedic.csv";
File theFile = new File(fileLocation);
byte[] fileContent = Files.readAllBytes(theFile.toPath());
Session session = Session.getDefaultInstance(new Properties());
// Create a new MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Add subject, from and to lines.
message.setSubject(subject, "UTF-8");
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
// Create a multipart/alternative child container.
MimeMultipart msgBody = new MimeMultipart("alternative");
// Create a wrapper for the HTML and text parts.
MimeBodyPart wrap = new MimeBodyPart();
// Define the text part.
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(bodyText, "text/plain; charset=UTF-8");
// Define the HTML part.
if (!bodyHTML.equals("")){
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
msgBody.addBodyPart(htmlPart);
}
// Add the text and HTML parts to the child container.
msgBody.addBodyPart(textPart);
// Add the child container to the wrapper object.
wrap.setContent(msgBody);
// Create a multipart/mixed parent container.
MimeMultipart msg = new MimeMultipart("mixed");
// Add the parent container to the message.
message.setContent(msg);
// Add the multipart/alternative part to the message.
msg.addBodyPart(wrap);
// Define the attachment.
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(fileContent, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
att.setDataHandler(new DataHandler(fds));
String reportName = "vedic.csv";
att.setFileName(reportName);
// Add the attachment to the message.
msg.addBodyPart(att);
try {
System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport("smtp");
transport.connect("smtp.office365.com ",587,"dibyanshu.chatterjee@phoenix.edu","Neel@ndakash27");
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
System.out.println("email sent");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
byte[] arr = new byte[buf.remaining()];
buf.get(arr);
SdkBytes data = SdkBytes.fromByteArray(arr);
RawMessage rawMessage = RawMessage.builder()
.data(data)
.build();
SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
.rawMessage(rawMessage)
.build();
client.sendEmail(rawEmailRequest);
SES SDK V1代碼:
RawMessage rawMessage = RawMessage.builder().data(data).build();
SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
SendRawEmailResponse resp = sesClient.sendRawEmail(rawEmailRequest);
SES SDK V2代碼:
SendEmailRequest emailRequest = SendEmailRequest.builder()
.content(EmailContent.builder().raw(rawMessage).build())
.build();
SendEmailResponse resp = sesV2Client.sendEmail(emailRequest);
原始 email 發送代碼的其他部分以相同的方式工作,附件不再有 10MB 的限制,40MB 是限制,但無論如何我不建議發送超過 18MB 的附件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.