I am using gmail account and I use thunderbird to download all the emails and to compose my emails. Now I'm trying to write a java application that will send out emails. Since I'm still in school I am using gmail account for the class project.
One t hing I noticed is that in my thunderbird I use secure connection using TLS. How should I incorporate that here in the java code. Is that why I'm getting the exception. Anyone knows any other smtp server using which I could submit my assignment.
Here is my code:
package com.imagework.email;
import java.util.Properties;
import javax.mail.Authenticator;
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 SendMailUsingAuthentication {
private static final String SMTP_HOST_NAME = "smpt.gmail.com";
private static final String SMTP_PORT = "587";
private static final String SMTP_AUTH_USER = "roger.rustin1@gmail.com";
private static final String SMTP_AUTH_PWD = "abcde";
private static final String emailMsgTxt = "email message text";
private static final String emailSubjectTxt = "email subject";
private static final String emailFromAddress = "roger.rustin1@gmail.com";
// Add List of Email address to who email needs to be sent to separated by comma
private static final String[] emailList = { "roger.rustin2@gmail.com" };
public static void main(String args[]) throws Exception {
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt,
emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail(String recipients[], String subject, String message,
String from) throws MessagingException {
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.user", SMTP_AUTH_USER);
props.put("mail.smtp.password", SMTP_AUTH_PWD);
props.put("mail.smtp.port", SMTP_PORT );
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
// Transport trans = session.getTransport();
// trans.connect(SMTP_HOST_NAME, SMTP_PORT , SMTP_AUTH_USER, SMTP_AUTH_PWD);
// trans.send(msg);
Transport.send(msg);
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
The error I get is given in detail:
DEBUG: JavaMail version 1.3.1
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\j2re1.4.2_06\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\j2re1.4.2_06\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smpt.gmail.com", port 587
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Unknown SMTP host: smpt.gmail.com;
nested exception is:
java.net.UnknownHostException: smpt.gmail.com
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.imagework.email.SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:70)
at com.imagework.email.SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:29)
Exception in thread "main"
All suggestions are welcome!
- Roger