为什么我不能从我的servlet发送电子邮件并获取java.security.AccessControlException? [英] Why can't i send email from my servlet and getting java.security.AccessControlException?

查看:153
本文介绍了为什么我不能从我的servlet发送电子邮件并获取java.security.AccessControlException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

为什么我不能从我的servlet发送电子邮件?

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

I am using google app engine. I want to send email from my servlet. i am using following code:

 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");
            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的所有导入:

Following are all imports of my 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;

有谁可以告诉我最新的问题?

Can anybody tell me whats the problem? Thanks in advance.

推荐答案

设置以下属性

        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");

这篇关于为什么我不能从我的servlet发送电子邮件并获取java.security.AccessControlException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆