我正在使用谷歌应用引擎。我想从我的 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;
谁能告诉我有什么问题?提前致谢。