【发布时间】:2013-03-10 01:31:52
【问题描述】:
我正在编写一个简单的 java 程序来使用 java mail using SSL 将邮件发送到特定的电子邮件 ID
以下是我的代码
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 发送邮件。
【问题讨论】:
-
发布诸如“没用”之类的无用声明基本上是在浪费时间。发布异常、堆栈跟踪等。
-
@EJP status_message 包含错误消息并显示 smtp...
标签: java jsp servlets smtp jakarta-mail