我正在编写一个简单的java程序,使用java 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();
}
}
当我在 pariticular主线程 中运行此代码时。它成功地 。
但 当我加入 时inside a function called within a servlet
当我读取status_message值时。它显示smtp
。
任何人都可以帮我找出问题所在。我错过了什么吗? 我想通过jsp servlet发送邮件。