Java EE api mail error sending mail

I made a CLASSPATH environment variable binding jaf and javamail :

name of user variable (windows): CLASSPATH

value of this variable: set CLASSPATH=%CLASSPATH%;C:\Java EE\Java SMTP\javamail-1.4.5\mail.jar;C:\Java EE\Java SMTP\jaf-1.1.1\activation.jar

I also put this 2 .jar files into my WebContent directory (into my java web application) and I add it to my buildpath.

I'm using Tomcat Apache Server 7.0.33.

Here is my servlet:

    @WebServlet (urlPatterns="/register")
    public class RegisterServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        private void sendMailRegistration(String email, int code)
                throws ServletException, IOException{
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            String msgBody = "Here is the code you have to put for confirmation: "+Integer.toString(code);

            try {
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress("dd@supinfo.com", "Admin SupLink"));
                msg.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(email, "User"));
                msg.setSubject("[SupLink] Activation");
                msg.setText(msgBody);
                Transport.send(msg);

            } catch (AddressException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
               e.printStackTrace();
            }
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String emailRegister = request.getParameter("emailRegister");
            int code = 1223;
                sendMailRegistration(emailRegister, code);

    }
}

and here is my error stacktrace:

    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at com.supinfo.miniprojet.servlets.RegisterServlet.sendMailRegistration(RegisterServlet.java:43)
    at com.supinfo.miniprojet.servlets.RegisterServlet.doPost(RegisterServlet.java:81)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
    ... 26 more

Someone know if there is a problem with my source mail address : dd@supinfo.com, or my CLASSPATH, because I just took Google implementation exemple here Google java mail ?

Answers


You need to identify a mail server to use (as you're obviously not running one on the localhost). Once identified, you need to configure Java Mail to use it. You can refer to the code below as an example of utilizing Google Mail servers:

public static void main(String[] args) {

    final String username = "username@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to-email@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

Note: This is not my code, you can reference the complete examples here


Need Your Help

How to integrate Wordpress with moodle 2.5

wordpress connect moodle integrate

I'm trying to integrate wordpress with moodle 2.5 , what I want to do exactly is: I want that the users login in wordpress and from there they can link to moodle, also I'd like wordpress to use mo...

Is it possible to set subjectAltName using pyOpenSSL?

python ssl openssl pyopenssl

I need to generate SSL certificates from Python using pyOpenSSL. Does anyone know if it's possible to set subjectAltName? From the documentation

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.