我正在尝试从我的 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[] = {"mygmail@gmail.com"};
String host = "smtp.gmail.com";
String username = "mygmail@gmail.com";
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)
谁能告诉我为什么我没有收到电子邮件以及这段代码有什么问题。提前致谢