Hello,
I have the
java program to send an email. I have my mail.jar and activation.jar in the classpath. I have attached my output also with this
thread. I am not sure. why the mail is not sent. It has not got into the Password Authenticator itself. Is it because the static usage. Can somebody help me out with how I can correct this program to send an email.
import java.util.Properties;
import java.util.Hashtable;
import javax.mail.MessagingException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Transport;
class MailCheck
{
/**
* To store the email from address
*/
private
String emailFromAddress;
/**
* To store the email list of recipients
*/
private String[] emailList;
/**
* To store the email subject
*/
private String emailSubject;
/**
* To store the SMTP host name
*/
private String smtpHostName;
/**
* To store the mail protocol
*/
private String mailProtocol;
/**
* To store the SMTP authentication value
*/
private String smtpAuth;
/**
* To store the SMTP port number
*/
private String smtpPort;
/**
* To store the SMTP authenticated user name
*/
private String smtpAuthUser;
/**
* To store the SMTP authenticated user password
*/
private String smtpAuthPwd;
/**
* This is called by the evaluate method to set From Address for sending mail
* @param emailFromAddress The email From Address to set.
*/
public void setEmailFromAddress(String emailFromAddress) {
this.emailFromAddress = emailFromAddress;
}
/**
* This is called by the evaluate method to set email List of recipients for sending mail
* @param emailList The email List of recipients to set.
*/
public void setEmailList(String[] emailList) {
this.emailList = emailList;
}
/**
* This is called by the evaluate method to set email Subject for sending mail
* @param emailSubject The email Subject to set.
*/
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
/**
* This is called by the evaluate method to set mail protocol used for sending mail
* @param mailProtocol The mail protocol used for sending mail to set.
*/
public void setMailProtocol(String mailProtocol) {
this.mailProtocol = mailProtocol;
}
/**
* This is called by the evaluate method to set SMTP Authentication for sending mail
* @param smtpAuth The SMTP Authentication to set.
*/
public void setSmtpAuth(String smtpAuth) {
this.smtpAuth = smtpAuth;
}
/**
* This is called by the evaluate method to set SMTP Authentication Password for sending mail
* @param smtpAuthPwd The SMTP Authentication Password to set.
*/
public void setSmtpAuthPwd(String smtpAuthPwd) {
this.smtpAuthPwd = smtpAuthPwd;
}
/**
* This is called by the evaluate method to set SMTP Authentication User Name for sending mail
* @param smtpAuthUser The SMTP Authenticated User Name to set.
*/
public void setSmtpAuthUser(String smtpAuthUser) {
this.smtpAuthUser = smtpAuthUser;
}
/**
* This is called by the evaluate method to set SMTP Host Name for sending mail
* @param smtpHostName The SMTP Host Name to set.
*/
public void setSmtpHostName(String smtpHostName) {
this.smtpHostName = smtpHostName;
}
/**
* This is called by the evaluate method to set SMTP Port number for sending mail
* @param smtpPort The SMTP Port number to set.
*/
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
}
public static void main(String[] args)
{
String msgTxt = "Hello";
String emailAddr = "";
String msgTtlTxt = "Title";
String emailAddrVal = "
abcdef@gmail.com";
msgTxt = "Hello";
msgTtlTxt = "Hi";
System.out.println("Hello World!");
try{
System.out.println("BEFORE");
postMail(emailAddrVal.split(","),msgTtlTxt,msgTxt,"my corporate email id");
System.out.println("AFTER");
} catch (MessagingException mg) {
}
}
public static void postMail(String recipients[], String subject, String message,String from) throws MessagingException {
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.host", "10.208.122.153");
props.put("mail.smtp.port", "25");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
props.put("mail.debug", "true");
System.out.println("oNE");
Authenticator auth = new SMTPAuthenticator();
System.out.println("three");
Session session = Session.getDefaultInstance(props, auth);
System.out.println("TWO");
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
System.out.println("after session");
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
System.out.println("after the from");
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
System.out.println("hello");
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
System.out.println("hi");
msg.setContent(message, "text/plain");
System.out.println("plain");
Transport.send(msg);
System.out.println("plain2");
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private static class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
System.out.println("authentication1111111111");
String username = "corporate email id"; // we use outlook email. I have given my corporate email id
String password = "password";// the corresponding password
System.out.println("authentication&&&&&&&&&");
return new PasswordAuthentication(username, password);
}
}
}
OUTPUT
=====
Hello World!
BEFORE
oNE
three
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.5.0_15\jre\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.SMTPSSLTransport=javax
.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsyste
ms, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com
.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLSt
ore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsyst
ems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.su
n.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=jav
ax.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.POP
3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.
sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STOR
E,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Prov
ider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc],
pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems
, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun
Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.S
MTPTransport,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\jdk1.5.0_15\jre\lib\
javamail.address.map (The system cannot find the file specified)
TWO
after session
after the from
hello
hi
plain