Qualcuno ha un esempio di invio di un'e-mail, con un allegato, tramite Amazon SES (in Java)?Esempio di invio di un'e-mail con allegato tramite Amazon in Java
7
A
risposta
14
Forse un po 'in ritardo, ma è possibile utilizzare questo codice (è anche necessario 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
Forse un po' in ritardo troppo. alternativa per inviare la posta utilizzando Java Mail e Amazon Raw Mail 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
Al 2014, alcune delle API Amazon ha cambiato, Ecco un esempio di lavoro:
http://mintylikejava.blogspot.hk/2014/05/example-of-sending-email-with-multipal.html
+0
Cercare di evitare le risposte di solo collegamento - invece, copiare il codice in questione nella risposta (nel caso in cui il collegamento si interrompa mai). – stkent
Problemi correlati
- 1. Invio di e-mail con allegato tramite NodeJS
- 2. Invio di e-mail con allegato
- 3. Invio di allegati di posta tramite Java
- 4. Esempio di applicazione .net tramite Amazon SQS
- 5. Invio di un messaggio di posta elettronica con un allegato di file PDF tramite PHP
- 6. Invio di un allegato da Shiny
- 7. Invio di e-mail con allegato in django
- 8. Invio di un'e-mail con un allegato utilizzando l'API javamail
- 9. Terminale Mac Invio e-mail con allegato
- 10. Invio di un'e-mail con allegato utilizzando SendGrid
- 11. Invio int tramite socket in Java
- 12. Cercare un esempio Amazon SES inviare un messaggio di posta elettronica incluso allegato
- 13. Invio di e-mail tramite server smtp in JAVA
- 14. Invio di oggetti complessi tramite JMS in Java
- 15. Invio di SMS tramite Gmail tramite delphi
- 16. API di Amazon Product Advertising tramite Java/SOAP
- 17. Invio di comandi al server remoto tramite ssh tramite Java con JSch
- 18. Invio di dati da node.js a Java tramite i socket
- 19. Amazon EC2 esempio metadati
- 20. Invio di email tramite email.mime.multipart in Python
- 21. Invio di posta tramite SmtpClient in .net
- 22. allegato e-mail parsing tramite mime4j
- 23. Invio di un modulo html con java
- 24. posta Java con allegato: ClassCastException su javax.mail.Multipart
- 25. Invio di e-mail tramite java in account Gmail con autenticazione a due vie
- 26. Esempio di bitSet Java
- 27. Invio polymorphic in Java
- 28. Come velocizzare l'invio di posta tramite Amazon SES?
- 29. Invio di un array multidimensionale tramite POST con php
- 30. php: invio di e-mail non riuscito con più di un allegato
Grazie, questo sembra come quello che sto cercando – Jason
Non riesco a trovare la classe "MailAWSCredentials" e "AWStransport". Si prega di aiutare –