可能重复:
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;
任何人能告诉我什么问题?提前致谢。
我不知道为什么我们不能在servlet中发送一封电子邮件,但我知道,如果你运行一个线程来发送它会工作... – 2012-07-26 16:13:42
请举例吗? – Piscean 2012-07-26 16:14:28
我在写答案... – 2012-07-26 16:19:33