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("", "Admin SupLink"));
                                 new InternetAddress(email, "User"));
                msg.setSubject("[SupLink] Activation");

            } catch (AddressException e) {
            } catch (MessagingException e) {

        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: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(
    at javax.mail.Service.connect(
    at javax.mail.Service.connect(
    at javax.mail.Service.connect(
    at javax.mail.Transport.send0(
    at javax.mail.Transport.send(
    at com.supinfo.miniprojet.servlets.RegisterServlet.sendMailRegistration(
    at com.supinfo.miniprojet.servlets.RegisterServlet.doPost(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.valves.AccessLogValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.coyote.http11.AbstractHttp11Processor.process(
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$ Source)
    at Source)
Caused by: Connection refused: connect
    at Method)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at com.sun.mail.util.SocketFetcher.createSocket(
    at com.sun.mail.util.SocketFetcher.getSocket(
    at com.sun.mail.smtp.SMTPTransport.openServer(
    ... 26 more

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


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 = "";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("", "");
    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(""));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");



    } 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.