发送电子邮件与java没有接收电子邮件

时间:2013-01-17 10:47:44

标签: java javamail

 @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = null;
    String user = request.getParameter("username");
    Users p = usb.getMemberInfo(user);
    HttpSession session1 = request.getSession(true);
    request.getSession().setAttribute("passenger", p);

    String host = "smtp.gmail.com";
String username =     // to write sender email here 
String password =   // To Write Password here
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.username", username);
properties.put("mail.smtp.password", password);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");

String to = (String)session1.getAttribute(p.getEmail());
try {
    Session session = Session.getDefaultInstance(properties, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(username));

    InternetAddress toAddress = new InternetAddress(to);


    System.out.println(Message.RecipientType.TO);

    //for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
        message.addRecipient(Message.RecipientType.TO, toAddress);
    //}
    message.setSubject("Forgot password");
    message.setText(p.getPassword());
    Transport transport = session.getTransport("smtp");
    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
} catch (Exception e) {
    e.printStackTrace();
}

    response.sendRedirect("welcome.jsp");

}

}

方法getMemberInfo:

@Override
public Users getMemberInfo(String username) {
   String query = "SELECT M from Users M where M.username=:username";
   Query q = em.createQuery(query);
   q.setParameter("username", username);
   List results = q.getResultList();

   for (Object o:results) {
       Users m = (Users) o;
       return m;
   }

   return null;

}

问题是我没有收到任何电子邮件。我不知道问题是什么。用户需要编写用户名,然后从数据库中获取电子邮件,并将密码发送到从数据库中获取的用户电子邮件。

3 个答案:

答案 0 :(得分:0)

对于您没有收到邮件的问题,这不是一个好的答案。但是你的第一个问题似乎是你不知道究竟是什么问题阻止了邮件被发送......

您应该将TransportListener添加到Transport实例,并查看会发生什么:

transport.addTransportListener(new TransportListener {
    public void messageDelivered(TransportEvent e) {
      //Invoked when a Message is succesfully delivered. 
    }
    public void messageNotDelivered(TransportEvent e)  {
      //Invoked when a Message is not delivered. 
      System.out.println("Message not delivered: " + e.getMessage())
      //should be a proper log message anyway...
    } 
    public void messagePartiallyDelivered(TransportEvent e)  {
      //Invoked when a Message is partially delivered. 
    }
}) ;

推荐链接:

答案 1 :(得分:0)

实际上代码很好,问题是我必须写:

String to = p.getEmail(); 

而不是:

String to = (String)session1.getAttribute(p.getEmail());

谢谢你的帮助

答案 2 :(得分:-1)

我想你必须配置附加属性

        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");