使用apache普通邮件发送多封邮件 [英] Sending multiple mails with apache common mail

查看:112
本文介绍了使用apache普通邮件发送多封邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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,用户名,密码);

Hi i have been trying to use the above code for sending mails to all those people whose appointment date is today. I am getting an error in Transport transport = session.getTransport ("smtp") ; transport.connect (smtpServer, userName, password) ;

请告诉我这段代码有什么问题.. :( 我已经使用了许多out.println来测试直到我的代码可以正常工作.

Please tell me What is wrong in this code..:( I have used many out.println just to test till where my code is working.

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)

推荐答案

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

How I predicted: The exception occurs because you need to authenticate your connection with gmail through TLS.

在属性中添加以下几行:

Add the following lines to you properties:

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

然后打开您的会话并按以下方式进行传输:

Then open your session and transport like this:

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

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

Obviously using your own accountname, password etc. :)

希望这会有所帮助!

这篇关于使用apache普通邮件发送多封邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆