2012-07-26 62 views
0

可能重复:
Why can't i send email from my servlet?为什么我不能从我的servlet发送电子邮件并获取java.security.AccessControlException?

我使用谷歌应用程序引擎。我想从我的servlet发送电子邮件。我用下面的代码:

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"); 
      props.put("mail.smtp.starttls.enable", "true"); 
      props.put("mail.smtp.host", host); 
      props.put("mail.smtp.user", username); 
      props.put("mail.smtp.password", password); 
      props.put("mail.smtp.port", "587"); 
      props.put("mail.smtp.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(); 

但我得到以下异常:

Exception error: java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve) 

以下是我的servlet的所有进口:

import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.util.List; 
    import java.util.Properties; 

    import javax.mail.Address; 
    import javax.mail.BodyPart; 
    import javax.mail.Message; 
    import javax.mail.Multipart; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 

任何人能告诉我什么问题?提前致谢。

+0

我不知道为什么我们不能在servlet中发送一封电子邮件,但我知道,如果你运行一个线程来发送它会工作... – 2012-07-26 16:13:42

+0

请举例吗? – Piscean 2012-07-26 16:14:28

+0

我在写答案... – 2012-07-26 16:19:33

回答

1

集以下属性

 props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", username); 
     props.put("mail.smtp.password", password); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 
+0

补充。没有什么区别:( – Piscean 2012-07-26 15:40:32

0

走吧。

在您的servlet代码中,您可以添加一个Thread开始。电子邮件将在另一个类中发送,而不是在servlet中发送。就像这样:

MailObj mo = new MailObj(); 
mo.setMsgText("text blah blah blah"); 
mo.setEmailSource("[email protected]"); 
mo.setSubject("subject"); 
Runnable t1 = new MailClass(request, response, mo); 
new Thread(t1).start(); 

的MailObj存储电子邮件所需的数据,如谁都会收到邮件,邮件文本和主题的地址。 (非常非常简单的方法)

public class MailObj { 

    private String emailSource; 
    private String msgText; 
    private String subject; 
    // getters and setters... 

} 

MailClass必须实现Runnable接口,该接口告诉您重写run()方法。

public class MailClass implements Runnable { 

    private MailObj mo; 
    HttpServletRequest req; 
    HttpServletResponse res; 

    public MailClass (HttpServletRequest request, HttpServletResponse response, 
       MailObj mo){ //constructor 
     this.req = request; 
     this.res = response; 
     this.mo = mo; 

    } 

    public void run() { // method of the Runnable interface 
     try { 
      sendEmail(req, res, mo); 
     } catch (ServletException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    public void sendEmail(HttpServletRequest request, HttpServletResponse response,       MailObj mo) throws ServletException, IOException { 

     Properties props = new Properties(); 
// here you set the host information (information about who is sending the email) 
// in this case, who is sending the email is a gmail client... 
     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 ses2 = Session.getDefaultInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication  getPasswordAuthentication() { 
         return new PasswordAuthentication(your_account,your_password); 
        } 
       }); 

     try { 


      Message msg = new MimeMessage(ses2); 

      msg.setFrom(new InternetAddress(email_to)); 
      msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mo.getEmailSource())); 
      msg.setSentDate(new Date()); 
      msg.setSubject(mo.getSubject()); 
      msg.setText(mo.getMsgText()); 

     // sending message (trying) 
      Transport.send(msg); 

     } catch (AddressException e) { 
      e.printStackTrace(); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 

发送带有螺纹的电子邮件是因为你的电子邮件被发送,这意味着用户将不会等待电子邮件发送“的运行时代码背后”的好办法。 发送电子邮件需要很长时间。做一些测试,你会注意到这一点。所以使用线程是避免长时间等待的好方法...

希望它有帮助! =]

+0

根据iNan的说法:应用程序引擎不允许在应用程序中使用新线程,而我正在使用应用程序引擎,你说什么? – Piscean 2012-07-26 16:51:16

+0

对于应用程序引擎,你是什么意思?在TomCat和Jetty中使用这个过长的时间,使Web应用程序... – 2012-07-26 16:54:44

0

在这里,看看我的Outlook客户端here;它位于src文件夹下。很久以来,我一直在与Javamail API进行斗争,这似乎运作良好。另外,您应该静态使用Transport类来发送消息,我不确定这是否是您的问题所在。

你也应该通过你的性质在对象扩展STMPAuthenticator类..

相关问题