1

我一直在尝试实现以下 servlet 代码来实现电子邮件功能,但我不断收到此代码错误。请看一下...

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.RequestDispatcher;

public class email extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String to="amanmaheshwari25@gmail.com"; //emailID at which the mail is send

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    Session session=null;

    try{
      Properties props = new Properties();
      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 = Session.getDefaultInstance(props,
       new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication("cconatus.2011@gmail.com","password");//change accordingly
       }
      });}
        catch(Exception e)
        {out.println("error234");}
      try {
      MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress("ccnatus.2011@gmail.com"));//change accordingly
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
       message.setSubject("Pls find the message inside");
       String dd = "hey";
       message.setText(dd);

       //send message
       Transport.send(message);
       out.println("success");
           } 
      catch(Exception e)
            {out.println("error");}
      finally {            
                out.close();
            }
        }

我得到的输出是:错误,即来自最后一个catch块的错误请尽快给出一些建议。

4

1 回答 1

1

以下是对我有用的属性:

mail.transport.protocol = smtp

mail.smtp.host = smtp.gmail.com

mail.smtp.auth = true

mail.smtp.starttls.enable = true

邮件.smtp.port = 587

于 2013-03-08T15:29:42.637 回答