Có ai có ví dụ về gửi email, kèm theo tệp đính kèm, qua Amazon SES (trong Java) không?Ví dụ về việc gửi email có tệp đính kèm qua Amazon trong Java
7
A
Trả lời
14
Có lẽ một chút muộn, nhưng bạn có thể sử dụng mã này (bạn cũng cần 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){...}
}
}
10
Có lẽ một chút muộn quá. Alternative để gửi mail sử dụng Java Mail và Amazon liệu Thư 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();
}
}
3
Tại ngày năm 2014, một số API amazon đã thay đổi, Dưới đây là một ví dụ làm việc:
http://mintylikejava.blogspot.hk/2014/05/example-of-sending-email-with-multipal.html
+0
Cố gắng tránh các câu trả lời chỉ có liên kết - thay vào đó, hãy sao chép mã có liên quan vào câu trả lời của bạn (trong trường hợp liên kết bị ngắt). – stkent
Các vấn đề liên quan
- 1. Gửi email có tệp đính kèm trong Ruby
- 2. Tìm kiếm một ví dụ Amazon SES gửi thư thô bao gồm tệp đính kèm
- 3. python không thể gửi tệp đính kèm qua email
- 4. Gửi tệp đính kèm iphone qua email theo chương trình
- 5. Cách gửi email có tệp đính kèm trong Android
- 6. Gửi email có tệp đính kèm trong django
- 7. Cách gửi email có tệp đính kèm trong số
- 8. php gửi email html kèm theo tệp đính kèm .csv
- 9. Mục đích Android: Gửi email có tệp đính kèm
- 10. Gửi email có tệp đính kèm Khách hàng Agnostic
- 11. Email file đính kèm
- 12. Mở tệp đính kèm email trong Android
- 13. Nhiều tệp đính kèm qua phpmailer
- 14. đính kèm tệp văn bản vào email
- 15. iOS: gửi email với tập tin đính kèm thêm một đính kèm (.txt) nộp tự động
- 16. Gửi email có tệp đính kèm tệp PDF bằng cách sử dụng PHP
- 17. cách gửi email qua mailx kèm theo tập tin đính kèm
- 18. Cách đính kèm tệp vào thông báo email trong Teamcity
- 19. gửi e-mail có nhiều tệp đính kèm
- 20. Cách lưu tệp đính kèm email trong C#
- 21. Cách gửi email có tệp đính kèm bằng cách sử dụng GmailSender trong android
- 22. Cách gửi email có tệp đính kèm từ R trong cửa sổ
- 23. Thư Java, gửi nhiều tệp đính kèm không hoạt động
- 24. tập tin đính kèm email phân tích qua mime4j
- 25. Gửi email có tệp đính kèm bằng cách sử dụng javamail API
- 26. Sử dụng thư() để gửi tệp đính kèm VÀ văn bản/html trong email trong PHP4
- 27. MacOSX: thư mới có tệp đính kèm
- 28. đính kèm email trên iphone
- 29. Làm cách nào để gửi email có tệp đính kèm bằng SmtpClient.SendAsync?
- 30. Cách đính kèm nhiều tệp vào một email bằng JavaMail?
Cảm ơn, vẻ này như những gì tôi theo sau. – Jason
Tôi không thể tìm thấy lớp "MailAWSCredentials" và "AWStransport". Vui lòng giúp –