Selva
Selva

Reputation: 556

Send Java email without authentication (no such provider exception: smtp)

The code I am currently using is as follows:

String to = "[email protected]";
String from = "[email protected]";
String host = "127.0.0.1";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));
    message.setSubject("This is the Subject Line!");
    message.setText("This is actual message");
    Transport.send(message);
    System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
    mex.printStackTrace();
}

If I paste the above code in my Java servlet and run it, the following exception gets thrown: javax.mail.NoSuchProviderException: smtp

I have also tried following the solutions outlined in these resources but to no avail: link1 link2 link3 link4.

Upvotes: 0

Views: 1451

Answers (4)

Bill Shannon
Bill Shannon

Reputation: 29971

NoSuchProviderException means something is messed up in your JavaMail API configuration. Where and how have you installed the JavaMail jar file?

Also, in case it's not clear from the other responses, the only way you're going to be able to send mail without authentication is if you're running your own mail server. You can't do it with general purpose online e-mail services (e.g. Gmail).

Upvotes: 1

Santanu Dey
Santanu Dey

Reputation: 2978

For this code you should get javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25; exception

But I suspect you have a jre issue. May be smtp implementation has a problem. Why don't you download java mail implementation from http://www.oracle.com/technetwork/java/javamail/index-138643.html and add it to your project class path and try?

Upvotes: 0

jmehrens
jmehrens

Reputation: 11045

It would help if you included the the full stacktrace for the javax.mail.NoSuchProviderException and JavaMail debug output. Because you are running this in a servlet container you could be running into Bug 6668 -skip unusable Store and Transport classes. Change your call to Transport.set to the following:

    final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(Session.class.getClassLoader());
    try {
        Transport.send(message);
    } finally {
        Thread.currentThread().setContextClassLoader(ccl);
    }

That code will isolate which transport class the code is allowed to see. That will rule out that bug in your code.

Upvotes: 1

Dashovsky
Dashovsky

Reputation: 137

First of all, if you wanna use gmail to send emails from your program you need to define stmp host as gmail. Your current host "127.0.0.1" is localhost meaning your own computer? Do you have mail server running on your computer?

Here you can see some tutorial how to send an email using gmail: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

If you're afraid to pass your normal gmail account details then create some kind of test email like kselvatest or something.

You are missing pretty much those:

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");

So basicly all you need to add is:

1.login and password so JavaMail can use some gmail account

2.declare which mail server you wanna use

change String host = "127.0.0.1"; to String host = "smtp.gmail.com";

3.set mail port

Upvotes: 0

Related Questions