Authentication failed while using the function from JSP.

I am facing a issue with the mail functionality with JAVA Mailing.The problem is that when I am running the stand alone program it is working fine but when same function is called from a JSP it gives Authentication failed Exception.I am attaching the code with the case.testMail() Is the function called from the JSP.

Please Look in to the issue its urgent.

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

import javax.activation.DataHandler;

import javax.mail.MessagingException;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimePartDataSource;

/*

To use this program, change values for the following three constants,

SMTP_HOST_NAME -- Has your SMTP Host Name

SMTP_AUTH_USER -- Has your SMTP Authentication UserName

SMTP_AUTH_PWD -- Has your SMTP Authentication Password

Next change values for fields

emailMsgTxt -- Message Text for the Email

emailSubjectTxt -- Subject for email

emailFromAddress -- Email Address whose name will appears as "from" address

Next change value for "emailList".

This String array has List of all Email Addresses to Email Email needs to be sent to.

Next to run the program, execute it as follows,

SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();

*/

public class SendMailUsingAuthentication

{

private static final String SMTP_HOST_NAME = "host";

private static final String SMTP_AUTH_USER = "username";

private static final String SMTP_AUTH_PWD = "password";

private static final String emailMsgTxt= "Test Msg";

private static final String emailSubjectTxt = "Notification: New User created";

private static final String emailFromAddress = "xyz.@abc.com";

// Add List of Email address to who email needs to be sent to

private static final String[] emailList = {"xyz.@abc.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 testMail(String msgBody,String senderEmail)throws MessagingException

{

try{

String mailBody = msgBody;

String senderEmailAdd = senderEmail;

SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();

smtpMailSender.postMail( emailList, emailSubjectTxt, mailBody , senderEmail);

}

catch(MessagingException me) {

//System.out.println("Mail not sent");

throw me;

}

}

public void postMail( String recipients[ ], String subject,

String message , String from) throws MessagingException

{

try{

boolean debug = false;

//Set the host smtp address

Properties props = System.getProperties();

props.put("mail.smtp.host", SMTP_HOST_NAME);

props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getInstance(props, auth);

session.setDebug(debug);

// create a message

Message msg = new MimeMessage(session);

// set the from and to address

InternetAddress addressFrom = new InternetAddress(from);

msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];

for (int i = 0; i < recipients.length; i++)

{

addressTo = new InternetAddress(recipients);

}

msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type

msg.setSubject(subject);

msg.setContent(message, "text/plain");

Transport.send(msg);

}

catch(MessagingException me) {

//System.out.println("Mail not sent");

throw me;

}

}

/**

* SimpleAuthenticator is used to do simple authentication

* when the SMTP server requires it.

*/

public static class SMTPAuthenticator extends javax.mail.Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

String username = SMTP_AUTH_USER;

String password = SMTP_AUTH_PWD;

return new PasswordAuthentication(username, password);

}

}

}

[4598 byte] By [sunil962a] at [2007-11-27 7:58:47]
# 1

Your code doesn't work in stand alone program... just little mistake!

Miss port property :

props.put("mail.smtp.port", "25");

props.put("mail.smtp.starttls.enable", "true"); // tls for gmail

AddressTo method didn' t compile :

InternetAddress[] addressTo = new InternetAddress[recipients.length];

for (int i = 0; i < recipients.length; i++)

{

addressTo[i] = new InternetAddress((String) recipients[i]);

}

msg.setRecipients(Message.RecipientType.TO, addressTo);

And see this post for jsp integration :

http://forum.java.sun.com/thread.jspa?threadID=5184860&tstart=0

alterna at 2007-7-12 19:40:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Is your authenticator getting called? What's the debug output show?See the JavaMail FAQ for a simpler way to do SMTP authentication.
bshannona at 2007-7-12 19:40:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Hi All I am able to send mail now there was some problem in the jsp which was unable to send proper request to the java file.Thanks for the help.Sunil
sunil962a at 2007-7-12 19:40:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...