【问题标题】:Sending multiple mails with apache common mail使用 apache 普通邮件发送多封邮件
【发布时间】:2011-10-09 05:12:19
【问题描述】:
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Connection con=null;
    Statement st=null;
    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String useq="root";
    String paq="manager";
    String url="jdbc:mysql://localhost:3306/jayna";
    con=DriverManager.getConnection(url,useq,paq);
    st=con.createStatement();


    String q="Select * from appointment where date=curdate()";
    ResultSet rs= st.executeQuery(q);
    String smtpServer="smtp.gmail.com";
    String from="";
    String userName="username";
    String password="password";

    String sub="Appointment Remainder";
    String mailText="This is a remainder message from Jayna Dental Center. Please attend your appointment with us today at your prescribed clinic. Thank you.";

    out.println(mailText); 
               from = userName;
               while(rs.next())
               { 

                   String to="";
                  String pid=rs.getString("pid");
                   String q1="select * from patient where patient_no='"+pid+"'";
                   ResultSet rs1=st.executeQuery(q1);
                   out.println("inside while");
                   if(rs1.next())
                   { 

                   to=rs1.getString("email_id");
                   out.println("inside if");


                        Properties props = System.getProperties();
                        props.put( "mail.smtp.host", smtpServer ) ;
                        out.println("a");
                        //SMTP server authentication is set to false, by default. Setting it to true as shown below
                        props.put( "mail.smtp.auth", "true" ) ;
                        out.println("a");
                        Session session = Session.getDefaultInstance(props, null);
                        MimeMessage message = new MimeMessage(session);
                        out.println("a");
                        //Setting the 'from', 'to', 'cc' addresses and the 'subject'
                        message.setFrom(new InternetAddress(from));
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                        message.setSubject(sub);
                        out.println("a");
                        //Making the mail body as inline and of html type
                        MimeMultipart mp = new MimeMultipart();
                        MimeBodyPart text = new MimeBodyPart();
                        text.setDisposition(Part.INLINE);
                        text.setContent(mailText, "text/html");
                        out.println("a");
                        mp.addBodyPart(text);
                        message.setContent(mp);
                        out.println("a");
                        //SMTP authentication
                        Transport transport = session.getTransport ("smtp") ;
                        transport.connect (smtpServer, userName, password) ;
                        message.saveChanges();
                        out.println("a");
                        transport.sendMessage(message, message.getAllRecipients());
                            transport.close();                          
                        out.println("a");
                        out.println("Your mail has been sent. Please close this window");

                   }  
                   else{
                       out.println("No such pid");
                   }}
               } catch (Exception e){
                        System.err.println("Email could not be sent due to error: "+e);
                        e.printStackTrace();  
                        out.println("ex");
                }

}

/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

您好,我一直在尝试使用上述代码向所有预约日期为今天的人发送邮件。 我收到一个错误 传输传输 = session.getTransport ("smtp") ; transport.connect (smtpServer, 用户名, 密码) ;

请告诉我这段代码有什么问题..:( 我使用了很多 out.println 只是为了测试我的代码在哪里工作。

Stack trace 
    Email could not be sent due to error: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u6sm22896pbh.0

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u6sm22896pbh.0

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
    at mail.Sendmail.doPost(Sendmail.java:134)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

【问题讨论】:

  • 您得到的确切错误信息是什么?
  • 如果您提供更多关于“错误”到底是什么的信息(即带有相关信息的 Stacktrace),人们实际上可以帮助您:) 可能是 gmail 需要 TLS 或 SSL 身份验证为了使用他们的 smtp - 不确定。
  • 你需要添加整个堆栈跟踪
  • sigh 再也不回答 1..

标签: java smtp jakarta-mail


【解决方案1】:

gmail smtp服务器的端口是465,所以你应该:

1、添加

props.put("mail.smtp.port", "465");

2、更新

transport.connect (smtpServer, 465, userName, password);

【讨论】:

    【解决方案2】:

    我的预测:发生异常是因为您需要通过 TLS 验证与 gmail 的连接。

    将以下行添加到您的属性中:

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    

    然后像这样打开您的会话并传输:

    Session session = Session.getInstance(props);
    Transport transport = session.getTransport("smtp");
    transport.connect(D_HOST, D_PORT, D_USER, D_PASS);
    

    显然使用您自己的帐户名、密码等:)

    希望这会有所帮助!

    【讨论】:

    • com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 必须先发出 STARTTLS 命令。 e6sm38427pbm.39
    • 您是否使用端口 587 进行连接?你确定你正在实例化会话并按照我提供的方式传输吗?
    • 没问题 - 这就是 stackoverflow 的用途:) 如果它有帮助,请不要忘记将其标记为正确答案,以便其他有相同问题的人可以更快地得到帮助。
    【解决方案3】:

    我可以在这里看到两个问题。

    1. 在问题标题中,您放置了 apache commons 电子邮件,但是您的代码使用的是标准 JavaMail。如果可以的话,我建议使用Commons Email
    2. GMail 使用 TLS,即安全连接。您无法以标准方式连接到它。

    换句话说,我认为您的代码没有任何问题,但是您不应该真正连接到 GMail 以发送邮件。如果贵公司有 SMTP 服务器,我会使用它。

    如果您无法使用Java Email Server

    如果您确实必须使用 GMail 的 SMTP,或者更一般地说是通过安全 (TLS) 连接的 SMTP, this article 解释了如何做到这一点。

    【讨论】:

    • 谢谢菲尔。我会在阅读给定的链接后做出回应。请订阅此问题以在将来提供帮助。\
    • 但是我已经使用此代码发送单个邮件,但它不适用于发送多个邮件。
    • 您是否正在使用另一个 gmail 地址测试单个邮件,而在使用多个电子邮件进行测试时可能会有非 gmail 地址?可能是从一个 gmail 帐户发送到另一个帐户的电子邮件不需要 tls auth
    猜你喜欢
    • 1970-01-01
    • 2016-01-30
    • 2022-01-09
    • 1970-01-01
    • 2011-02-08
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多