我使用简单的代码以编程方式发送邮件
private String HOST_NAME = "gmail-smtp.l.google.com";
String messageBody;
public static void main(String [] args){
String recipients[]={"abhijit@gmail.com"};
String subject = "Report";
String message = " Hi All.";
String from ="abhijit@gmail.com";
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);
}
}
我的项目也有 aws-java-sdk 依赖。
现在每当我尝试执行代码时,它都会给我一些激动人心的信息
13/07/20 02:19:24 INFO amazonaws.request: Sending Request: POST https://email.us-east-1.amazonaws.com / Parameters: (Action: SendRawEmail, AWSAccessKeyId=abhijit@gmail.com,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)
现在,如果在删除amazon-aws依赖项后运行相同的代码,那么事情就可以了。
有人可以建议如何调用AWSJavaMailTransport.sendEmail
而不是Transport.send
方法
任何帮助将不胜感激
答案 0 :(得分:1)
如果您不使用amazon邮件传输 - 只需删除aws-java-sdk-XXXjar / META-INF / javamail.providers并将其安装到您的本地存储库(最好将库版本重命名为另一个版本,例如。 1.5.3a)或你的maven repo(Nexus等),如果你有。
pom example ver 1.5.3 http://db.tt/cI9f2DFS jar示例ver 1.5.3 http://db.tt/lgwJYJYr