Discussions

EJB design: Java mail - javax.mail.NoSuchProviderException: Invalid protocol

  1. hi,
    i try to do a sending email program (testing version) by using java.mail but some error popup.
    <import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;

    public class SendMail
    {

    Transport smtp_service;

    public SendMail() throws Exception
    {
    Properties props = System.getProperties();
    Session ss = Session.getDefaultInstance(props,null);
    System.out.println("pass #1");
    try
    {
    smtp_service = ss.getTransport();
    }
    catch (MessagingException nspe)
    {
    System.err.println("ERROR..No SMTP mail provider! \n"+nspe);
    }
    System.out.println("pass #2");
    MimeMessage message = new MimeMessage(ss);
    InternetAddress sender = new InternetAddress("kktang at yahoo dot com","kokking");
    message.setFrom(sender);
    System.out.println("pass #2.5");
    InternetAddress[] to_list = {new InternetAddress("kk dot tang at pdx dot com dot my")};
    message.setRecipients(Message.RecipientType.TO,to_list);
    message.setSubject("Testing Emailing");
    message.setHeader("keyword","HEHEE");
    message.setText("Hello world...");
    message.saveChanges();
    System.out.println("pass #2.9");
    smtp_service.send(message);
    System.out.println("pass #3");


    }
    public static void main(String[] args)
    {
    try
    {
    SendMail xx = new SendMail();
    System.out.println("Msg Send");
    }
    catch (Exception ee)
    {
    System.out.println("main error started");
    System.err.println("ERROR AT MAIN\n" + ee);
    System.out.println("main error terminated");
    System.exit(1);
    }
    System.exit(0);
    }
    }

    <
    pass #1
    ERROR..No SMTP mail provider!
    javax.mail.NoSuchProviderException: Invalid protocol: null
    pass #2
    pass #2.5
    pass #2.9
    main error started
    main error terminated
    ERROR AT MAIN
    javax.mail.SendFailedException: Sending failed;
      nested exception is:
    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
      nested exception is:
    java.net.ConnectException: Connection refused: connect


    please help... Thanks
  2. You should specify exchange server IP address.

    Check do you have Mail provider server running on your machine?
  3. Problem solved.. Thanks[ Go to top ]

    Thanks a lot.. u solved my problem..
  4. I recently faced with the same issue and strived so hard to find a proper solution. Finally, it was something to do with conflicts of two jar files. Namely, javamail-1.4.3.jar and activation.jar.

    First, make sure to download them from sun official web site. 

    Ping your smtp server or telnet to check the user login credentials.

    When deployed in an application server make sure only one instance of each jar exists in lib folders. Also, if you are using a bundling tool like maven or ant double check to varefy that you have not buldled it alraday. 

    Above step is vary important since application server do NOT show any deployment time exceptions even if you have doundle it alrady but your program will fail with the 'javax.mail.NoSuchProviderException: Invalid protoc smtp' error.

    Hope this will be helpful.