Reputation: 4792
I am using a simple code to send mail programmatically
private String HOST_NAME = "gmail-smtp.l.google.com";
String messageBody;
public static void main(String [] args){
String recipients[]={"[email protected]"};
String subject = "Report";
String message = " Hi All.";
String from ="[email protected]";
String emailPassword = "******";
String [] files = {"/home/ubuntu/RportSystem.txt"};
try {
new MailServerTest().postMail(recipients, subject, message, from, emailPassword, files);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void postMail(String recipients[], String subject, String message,
String from, String emailPassword, String[] files) throws MessagingException {
boolean debug = false;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator authenticator = new CustomAuthenticator (from, emailPassword);
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
addAtachments(files, multipart);
msg.setContent(multipart);
Transport.send(msg);
System.out.println("Sucessfully Sent mail to All Users");
}
protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException {
for (int i = 0; i <= attachments.length - 1; i++) {
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(filename);
multipart.addBodyPart(attachmentBodyPart);
}
}
private class CustomAuthenticator extends javax.mail.Authenticator {
String username;
String password;
private CustomAuthenticator (String authenticationUser, String authenticationPassword) {
username = authenticationUser;
password = authenticationPassword;
}
@Override
public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
}
My project also has aws-java-sdk dependency.
Now whenever i try to execute the code it gives me excveption message saying
13/07/20 02:19:24 INFO amazonaws.request: Sending Request: POST https://email.us-east-1.amazonaws.com / Parameters: (Action: SendRawEmail, [email protected],Algorithm=HmacSHA256,Signature=Y4sqXX2bw7N0uiPoCVsZavgnbTR1j30NaIryVBbbn9I=, x-amz-nonce: a335527c-e25a-48f8-8ec1-67348173cc68, Date: Fri, 19 Jul 2013 20:49:23 GMT, )
13/07/20 02:19:31 INFO amazonaws.request: Received error response: Status Code: 403, AWS Request ID: b2064183-f0b4-11e2-9355-21325bfa2b4f, AWS Error Code: InvalidClientTokenId, AWS Error Message: The security token included in the request is invalid
javax.mail.SendFailedException: Unable to send email;
nested exception is:
Status Code: 403, AWS Request ID: b2064183-f0b4-11e2-9355-21325bfa2b4f, AWS Error Code: InvalidClientTokenId, AWS Error Message: The security token included in the request is invalid
at com.amazonaws.services.simpleemail.AWSJavaMailTransport.sendEmail(AWSJavaMailTransport.java:276)
at com.amazonaws.services.simpleemail.AWSJavaMailTransport.sendMessage(AWSJavaMailTransport.java:95)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
Now if the same code i ran after removing the amazon-aws dependency, the thing is working fine.
Can anyone suggest how AWSJavaMailTransport.sendEmail
is getting called instead of Transport.send
method
Any help will be appreciated
Upvotes: 1
Views: 886
Reputation: 301
If you are not using amazon mail transport - simply remove aws-java-sdk-X.X.X.jar/META-INF/javamail.providers and install it to your local repo (better to rename library version to another one , for ex. 1.5.3a) or your maven repo (Nexus etc.) if you have one.
pom example ver 1.5.3 http://db.tt/cI9f2DFS jar example ver 1.5.3 http://db.tt/lgwJYJYr
Upvotes: 1