gpt4 book ai didi

java - 为什么我无法从 servlet 发送电子邮件并获取 java.security.AccessControlException?

转载 作者:行者123 更新时间:2023-12-01 15:15:24 24 4
gpt4 key购买 nike

Possible Duplicate:
Why can't i send email from my servlet?

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

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

  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;

谁能告诉我出了什么问题吗?提前致谢。

最佳答案

设置以下属性

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

关于java - 为什么我无法从 servlet 发送电子邮件并获取 java.security.AccessControlException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11672491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com