jeudi 9 juillet 2015

Send Mails in Java servlet error 501

I have a problem, I'm trying to sent a sample email using the following servlet:

package mail;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author bog
 */
public class mail1 extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, AddressException, MessagingException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            PrintWriter out = response.getWriter();
            out.println("try");
            final String username = "progweb2";

            final String password = "lollosoissimo";
            out.println("set pwd");

 // Get a Properties object to set the mailing configuration 
 // parameters
            Properties props = System.getProperties();

            props.setProperty("mail.smtp.host", "smtp.mail.yahoo.com");

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

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

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

            props.put("mail.debug", "true");

 //We create the session object with the authentication information
            out.println("create session obj");
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                ;
                @Override

                protected PasswordAuthentication
                        getPasswordAuthentication() {

                    return new PasswordAuthentication(username, password);

                }

            });
            //Create a new message
            session.setDebug (true);
            Message msg = new MimeMessage(session);

 //Set the FROM and TO fields –
            msg.setFrom(new InternetAddress(username + "yahoo.com"));

            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("dest@mail.com"));

            msg.setSubject("Hello da gmail");

            msg.setText("come stai ");

            //msg.setSentDate(new Date());

            System.out.println("\nTrying to send email...\n");

 //We create the transport object to actually send the e-mail
            Transport transport = session.getTransport("smtps");

            transport.connect("smtp.mail.yahoo.com", 465, username, password);
            out.println("sensing msgto "+msg.getAllRecipients());
            transport.sendMessage(msg, msg.getAllRecipients());
            out.println("before closing tx");
            transport.close();
            out.println("close tx");

            System.out.println("\nEmail sent!\n");

        } catch (MessagingException e) {

            e.printStackTrace(); 

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (MessagingException ex) {
            Logger.getLogger(mail1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (MessagingException ex) {
            Logger.getLogger(mail1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Everything stopes right after printing out.println("sensing msgto "+msg.getAllRecipients()); on the web page, just like the following istruction fails. But I get this error in my Tomcat console:

09-Jul-2015 23:13:59.343 INFO [http-nio-8084-exec-92] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/CinemaBooking] has started
09-Jul-2015 23:13:59.349 INFO [http-nio-8084-exec-92] database.DBManager.shutdown No suitable driver found for jdbc:derby:;shutdown=true
09-Jul-2015 23:13:59.351 WARNING [http-nio-8084-exec-92] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [CinemaBooking] registered the JDBC driver [org.apache.derby.jdbc.ClientDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
09-Jul-2015 23:13:59.627 INFO [http-nio-8084-exec-92] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/CinemaBooking] is completed
09-Jul-2015 23:16:31.480 INFO [http-nio-8084-exec-82] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/CinemaBooking] has started
09-Jul-2015 23:16:31.484 INFO [http-nio-8084-exec-82] database.DBManager.shutdown No suitable driver found for jdbc:derby:;shutdown=true
09-Jul-2015 23:16:31.486 WARNING [http-nio-8084-exec-82] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [CinemaBooking] registered the JDBC driver [org.apache.derby.jdbc.ClientDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
09-Jul-2015 23:16:31.998 INFO [http-nio-8084-exec-82] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/CinemaBooking] is completed
09-Jul-2015 23:17:02.712 INFO [http-nio-8084-exec-97] null.null No suitable driver found for jdbc:derby:;shutdown=true
09-Jul-2015 23:17:02.715 WARNING [http-nio-8084-exec-97] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [CinemaBooking] registered the JDBC driver [org.apache.derby.jdbc.ClientDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
09-Jul-2015 23:17:03.220 INFO [http-nio-8084-exec-97] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/CinemaBooking]
09-Jul-2015 23:17:08.268 INFO [http-nio-8084-exec-101] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor /home/bog/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf/Catalina/localhost/CinemaBooking.xml
09-Jul-2015 23:17:08.270 WARNING [http-nio-8084-exec-101] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'antiJARLocking' to 'true' did not find a matching property.
09-Jul-2015 23:17:08.525 INFO [http-nio-8084-exec-101] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /home/bog/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf/Catalina/localhost/CinemaBooking.xml has finished in 257 ms
09-Jul-2015 23:17:08.537 INFO [http-nio-8084-exec-96] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/CinemaBooking]] after start() had already been called. The second call will be ignored.
DEBUG: JavaMail version 1.4.5
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.5

Trying to send email...

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 465, isSSL true
220 smtp.mail.yahoo.com ESMTP ready
DEBUG SMTP: connected to host "smtp.mail.yahoo.com", port: 465

EHLO bog-Lenovo-IdeaPad-S510p
250-smtp.mail.yahoo.com
250-PIPELINING
250-SIZE 41697280
250-8 BITMIME
250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41697280"
DEBUG SMTP: Found extension "8", arg "BITMIME"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XYMCOOKIE"
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<progweb2yahoo.com>
501 Syntax error in arguments
DEBUG SMTP: got response code 501, with response: 501 Syntax error in arguments

RSET
250 flushed
DEBUG SMTP: MessagingException while sending
com.sun.mail.smtp.SMTPSendFailedException: 501 Syntax error in arguments
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

com.sun.mail.smtp.SMTPSendFailedException: 501 Syntax error in arguments
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at mail.mail1.processRequest(mail1.java:105)
    at mail.mail1.doGet(mail1.java:144)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1618)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at mail.mail1.processRequest(mail1.java:105)
    at mail.mail1.doGet(mail1.java:144)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1625)
    ... 30 more
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1625)
    ... 30 more

Does it says my mail from wich I'm trying to send the email is bad formatted? i tyied several mail formats but nothing worked

Aucun commentaire:

Enregistrer un commentaire