javax.mail.NoSuchProviderException: No provider for smtp
843830Aug 23 2004 — edited Oct 1 2004I downloaded the javamail and activation.jar.
I'm getting a NoSuchProviderException.
It prints the name of the server I'm on correctly.
Using Unix - Solaris platform.
I also tried simply using "localhost" but that gave the same exception.
import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendApp {
public static void send (String smtpHost, int smtpPort,
String from, String to,
String subject, String content)
throws AddressException, MessagingException {
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
Session session = Session.getDefaultInstance(props,null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);
Transport.send(msg);
}
public static void main(String[] args) throws Exception {
String host = InetAddress.getLocalHost().getHostName();
System.out.println(host);
send(host, 25, "myemail@server.com",
"myemail@server.com","Testing Emails",
"Testing some email options.");
}
}
Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtp
at javax.mail.Session.getProvider(Session.java:436)
at javax.mail.Session.getTransport(Session.java:631)
at javax.mail.Session.getTransport(Session.java:612)
at javax.mail.Session.getTransport(Session.java:667)
at javax.mail.Transport.send0(Transport.java:148)
at javax.mail.Transport.send(Transport.java:80)
at SendApp.send(SendApp.java:25)
at SendApp.main(SendApp.java:30)
The thing is I used this very simple socket program and it emailed fine using localhost. I figured javamail would make the code more cleaner and reusable.
import java.io.*;
import java.net.*;
import java.util.*;
public class EmailTest {
public static void main(String[] args) throws Exception{
Date dDate = new Date();
Socket smtpSocket = new Socket("localhost", 25);
PrintWriter out = new PrintWriter(smtpSocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
smtpSocket.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();
out.print("HELO " + hostName + "\r\n");
out.print("MAIL FROM: <me@myserver.com>\r\n");
out.print("RCPT TO: <me@myserver.com>\r\n");
out.print("DATA\r\n");
String sMessage = "This is a test for this email program.\n";
sMessage += "Have a nice day!\n";
out.print("Subject: Testing \r\n");
out.print(sMessage + "\r\n");
out.print(".\r\n");
out.print("QUIT\r\n");
out.flush();
String responseLine;
int count = 1;
while((responseLine = in.readLine()) != null) {
if(responseLine.indexOf("accepted") != -1)
break;
count++;
}
smtpSocket.close();
}
}