我正在编写一个简单的 java 程序来使用发送邮件到特定的电子邮件 IDjava mail using SSL
以下是我的代码
public static String sendMail(String to)
{
String status_message = null;
try
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("sender@gmail.com","password");//change accordingly
}
});
//compose message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Email Subject");
message.setText("Text Content included in inside Email");
//send message
Transport.send(message);
status_message = "success";
}
catch (MessagingException ex)
{
status_message = ex.getMessage();
}
}
当我在特定的主线程中运行此代码时。它成功了。
但是当我包含它时,这同样不起作用inside a function called within a servlet
当我读取 status_message 值时。它正在显示smtp
。
谁能帮我找出问题所在。我错过了什么吗?我想通过 jsp servlet 发送邮件。