0

可能重复:
为什么我不能从我的 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;

谁能告诉我有什么问题?提前致谢。

4

3 回答 3

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");
于 2012-07-26T15:34:07.607 回答
0

我们走吧。

在您的 servlet 代码中,您可以添加一个线程启动。电子邮件将在另一个类中发送,而不是在 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();
        }

使用线程发送电子邮件是一种好方法,因为您的电子邮件是在“运行时代码之后”发送的,这意味着用户不会等待电子邮件发送。发送电子邮件需要很长时间。做一些测试,你会注意到这一点。所以使用线程是避免长时间等待的好方法......

希望能帮助到你!=]

于 2012-07-26T16:30:21.207 回答
0

在这里,看看我的 Outlook 客户端;它位于 src 文件夹下。我与 Javamail api 斗争了很长时间,这似乎运作良好。此外,您应该静态使用 Transport 类来发送消息,我不确定这是否是您的问题所在。

您还应该将您的properites 对象传递给扩展STMPAuthenticator 的类。

于 2012-07-26T17:03:50.647 回答