2012-07-26 114 views
-1

我想从我的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电子邮件和什么是错用此代码。 在此先感谢

+1

尝试通过本地主机上的SMTP从命令行或电子邮件客户端发送,并检查它是否工作正常。也许本地SMTP服务器配置不正确,那么这可能是在另一个论坛(可能是超级用户?)要求的。 – Axel 2012-07-26 13:19:04

+2

这个问题与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

+0

当你构造你应该传递一个SMTPAuthenticator到你的属性它。 – Joel 2012-07-26 17:17:20

回答

0

阅读https://developers.google.com/appengine/docs/java/mail/overview 基本上停止在您的应用程序中包括任何JavaMail库,并且不要尝试连接到主机。构建消息并直接跳到Transport.send(msg);

+0

加上我上传到应用程序引擎,并使用Gmail作为主机。本地主机它不工作,但是当我上传它在应用程序引擎,然后它工作正常 – Piscean 2012-08-08 10:20:25

0

你在本地主机上运行电子邮件服务器吗?如果是这样,它是否已禁用身份验证?

也许你可以在其日志中看到错误消息。

0

原因是,您使用localhost作为smtp主机,您应该在使用本地主机作为发件人之前在您的计算机上使用部署邮件服务器。即使在此之后,您可能需要注册您的服务器。

你也可以使用Gmail或雅虎邮件。使用谷歌邮件的smtp主机。使用您的信用进行身份验证,然后继续。

尝试此链接:

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

0

一般来说,你可以使用JavaMail来发送邮件使用的servlet。您声明您正在使用AppEngine(Why can't i send email from my servlet and getting java.security.AccessControlException?)。 Google App Engine允许您使用JavaMail发送电子邮件,但您不能使用appengine提供的JavaMail连接到任何邮件服务器。添加到传输或会话的MTP配置将被忽略。 https://developers.google.com/appengine/docs/java/mail/usingjavamail#JavaMail_Features_Not_Supported

相关问题