我想从我的servlet发送电子邮件。没有例外,但我没有在我的帐户中收到电子邮件。我使用下面的代码:为什么我无法从我的servlet发送电子邮件?
public class SendEmailServlet extends HttpServlet {
@SuppressWarnings({ "unchecked" })
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
PersistenceManager pm = PMF.get().getPersistenceManager();
try
{
String to[] = {"[email protected]"};
String host = "smtp.gmail.com";
String username = "[email protected]";
String password = "password";
Properties props = new Properties();
props.put("mail.smtps.auth", "true");
// ...
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
msg.setFrom(new InternetAddress(username,"Me"));
msg.setSubject("Testing");
msg.setText("Testing...");
Address[] addresses = new Address[to.length];
for (int i = 0; i < to.length; i++) {
Address address = new InternetAddress(to[i]);
addresses[i] = address;
// Add the given addresses to the specified recipient type.
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
Transport t = session.getTransport("smtps");
t.connect(host, username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
out.println(0);
}
}
catch (Exception e)
{
System.out.println("Exception error: " + e);
out.println(e);
}
finally
{
pm.close();
}
}
}
现在我得到以下异常:
Exception error: java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve)
谁能告诉我为什么我不recieving电子邮件和什么是错用此代码。 在此先感谢
尝试通过本地主机上的SMTP从命令行或电子邮件客户端发送,并检查它是否工作正常。也许本地SMTP服务器配置不正确,那么这可能是在另一个论坛(可能是超级用户?)要求的。 – Axel 2012-07-26 13:19:04
这个问题与servlet无关。首先使用通过简单的'main()'方法执行/测试的普通Java类来工作,然后在servlet中重用它。请不要一遍又一遍地重发同一个问题。编辑而不是改进问题。 http://stackoverflow.com/questions/11672491/why-cant-i-send-email-from-my-servlet-and-getting-java-security-accesscontrolex – BalusC 2012-07-26 16:58:29
当你构造你应该传递一个SMTPAuthenticator到你的属性它。 – Joel 2012-07-26 17:17:20