0
我正在嘗試使用SMTP發送電子郵件。 它在我的本地工作正常。但是,當我在AWS EC2服務器上構建它時,它不起作用。無法從AWS EC2發送SMTP郵件
這是我的代碼來配置和發送電子郵件!
對我而言有什麼想法?
try{
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable",true);
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.user","[email protected]");
props.put("mail.smtp.password","testpassword");
props.put("mail.smtp.port","587");
props.put("mail.smtp.auth",true);
props.put("mail.smtp.ssl.trust", "*");
String[] to = {toEmail};
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for(int i=0; i < to.length; i++) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println("EMAIL TO:"+toEmail);
for(int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(content);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com","[email protected]","testpassword");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception ex){
log.debug("send Email failed", ex);
}
你嘗試'的telnet smtp.gmail.com 587'從EC2實例?這將檢查到該端口上的服務器的連接是否打開 – mostafazh
檢查Ec2是否在公共子網中或連接了任何Nat網關。你可以請分享你得到的確切例外。 –