我正在使用谷歌应用引擎。我想从我的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;
有谁能告诉我这个问题是什么?提前谢谢。
答案 0 :(得分: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");
答案 1 :(得分:0)
我们走吧。
在servlet代码中,您可以添加一个Thread start。电子邮件将在另一个类中发送,而不是在servlet中发送。像这样:
MailObj mo = new MailObj();
mo.setMsgText("text blah blah blah");
mo.setEmailSource("to@mail.com");
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();
}
使用线程发送电子邮件是一种好方法,因为您在“运行时代码后面”发送了电子邮件,这意味着用户不会等待发送电子邮件。 发送电子邮件需要很长时间。做一些测试,你会注意到。所以使用线程是避免长时间等待的好方法......
希望它有所帮助! =]
答案 2 :(得分:0)
在这里,看看我的Outlook客户端here;它在src文件夹下。我很长一段时间都在使用Javamail api,这看起来效果很好。此外,您应该静态使用Transport类来发送消息,我不确定这是否是您的问题所在。
您还应该将您的专业对象传递给扩展STMPAuthenticator的类。