Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting javax.mail.NoSuchProviderException when try to send mail using java

I have read and tried all solution given in stackoverflow and in other various sites but still getting issue and getting exception.

Code :

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class sendmail {

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

public static void main(String args[]) throws AddressException,
        MessagingException {

    sendmail javaEmail = new sendmail();

    javaEmail.setMailServerProperties();
    javaEmail.createEmailMessage();
    javaEmail.sendEmail();
}

public void setMailServerProperties() {

    String emailPort = "587";//gmail's smtp port

    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.starttls.enable", "true");

}

public void createEmailMessage() throws AddressException,
        MessagingException {
    String[] toEmails = { "[email protected]" };
    String emailSubject = "Java Email";
    String emailBody = "This is an email sent by JavaMail api.";

    mailSession = Session.getDefaultInstance(emailProperties, null);
    emailMessage = new MimeMessage(mailSession);

    for (int i = 0; i < toEmails.length; i++) {
        emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
    }

    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");//for a html email
    //emailMessage.setText(emailBody);// for a text email

}

public void sendEmail() throws AddressException, MessagingException {

    String emailHost = "smtp.gmail.com";
    String fromUser = "emailid";//just the id alone without @gmail.com
    String fromUserEmailPassword = "test";

    Transport transport = mailSession.getTransport("smtp");

    transport.connect(emailHost, fromUser, fromUserEmailPassword);
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    System.out.println("Email sent successfully.");
}

 }

When I debug code , it stop working at line : Transport transport = mailSession.getTransport("smtp");

I have added following JARs :

Mail.jar , pop3.jar , smtp-1.4.2.jar , Activation.jar , additional.jar

Full exception :

Exception in thread "main" javax.mail.NoSuchProviderException: smtp at javax.mail.Session.getService(Session.java:764) at javax.mail.Session.getTransport(Session.java:689) at javax.mail.Session.getTransport(Session.java:632) at javax.mail.Session.getTransport(Session.java:612) at javax.mail.Session.getTransport(Session.java:667) at javax.mail.Transport.send0(Transport.java:154) at javax.mail.Transport.send(Transport.java:80) at JannyaPaid_Device.sendmail.sendEmail(sendmail.java:68) at JannyaPaid_Device.sendmail.main(sendmail.java:26)

Also I want to ask that firewall can prevent this things to send mail? As we have some firewall installed but I able to open and send mail thrugh gmail manually.

like image 703
Helping Hands Avatar asked May 27 '15 03:05

Helping Hands


2 Answers

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;

public class SendMailTLS {

public static void main(String[] args) {

    final String username = "[email protected]";
    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("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
        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);
    }
}

}

like image 186
Pavan Kumar K Avatar answered Dec 16 '22 19:12

Pavan Kumar K


For - Must issue a STARTTLS command(start transport layer security), you might need a certificate in the path(in JDK/JRE) and same on the Email server side as well.

like image 28
Shailendra Singh Avatar answered Dec 16 '22 17:12

Shailendra Singh