sivea
sivea

Reputation: 21

Sending Mail in java

Hi i want to send mail using below code, if it is executed, it will thrown an exception given below what i need to do..I am using java,eclipse,i have added javaMailAPI jars also....do i need additional jars to this programme.

    package firstpackage;

    /*public class SendMail {  }*/

    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import org.apache.commons.lang3.exception.ExceptionUtils;

    public class SendMail {
      public SendMail(String fromMail, String tomail)
      {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("Email_Id","password");
          }
        });
      try {
          Message message = new MimeMessage(session);
          message.setFrom(new InternetAddress(fromMail));
          message.setRecipients(Message.RecipientType.TO,
          InternetAddress.parse(tomail));
          message.setSubject("Script failed");
          //message.setText("your test has failed for script name:Name of your scipt <============================>"+ ExceptionUtils.getStackTrace(e) );
          Transport.send(message);
          System.out.println("Done");
        } catch (MessagingException ex) {
          throw new RuntimeException(ex);
        }
      }

      public static void main(String args[])
      {
        new SendMail("[email protected]","[email protected]");
      }
    }

and exception is:

    Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
        at firstpackage.SendMail.<init>(SendMail.java:42)
        at firstpackage.SendMail.main(SendMail.java:48)
    Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp

     at javax.mail.Session.getProvider(Session.java:237)
            at javax.mail.Session.getTransport(Session.java:346)
            at javax.mail.Session.getTransport(Session.java:376)
            at javax.mail.Transport.send(Transport.java:67)
            at javax.mail.Transport.send(Transport.java:48)
            at firstpackage.SendMail.<init>(SendMail.java:39)
            ... 1 more

Upvotes: 0

Views: 1455

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

First, you'll want to fix these common mistakes, although they aren't the source of your problem.

Did you follow these instructions to add JavaMail to your project?

Try running your program outside of Eclipse, e.g., by packaging your application in a jar file and then using "java -cp SendMail.jar:javax.mail.jar firstpackage.SendMail".

Upvotes: 1

Related Questions