问题
Possible Duplicate:
Why can't i send email from my 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();
But i am getting following exception:
Exception error: java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve)
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.
回答1:
set following properties
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");
回答2:
Let's go.
In your servlet code you can add a Thread start. The email will be sent in another class and not in the servlet. Like this:
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();
The MailObj stores email necessary data, such as the address of who will receive the email, the email text and subject. (Very and very simple approach)
public class MailObj {
private String emailSource;
private String msgText;
private String subject;
// getters and setters...
}
The MailClass has to implement the Runnable interface, which tells you to Override the run() method.
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();
}
Sending the email with a thread is a good way because you the email is sent "behind the runtime code", which means that the user WILL NOT wait for the email be sent. Sending an email takes a long time. Make some tests and you'll notice that. So use a thread is a good method to avoid long time waiting...
Hope it helps! =]
回答3:
Here, have a look at my outlook client here; it is under the src folder. I struggled with the Javamail api for a long time and this seems to work well. Also, you should be using the Transport class statically to send a message, I am not sure if that is where your problem lies or not.
You should also be passing your properites object a class that extends STMPAuthenticator..
来源:https://stackoverflow.com/questions/11672491/why-cant-i-send-email-from-my-servlet-and-getting-java-security-accesscontrolex