automated mail code throwing javax.mail.AuthenticationFailedException
hi...
i want to generate and send automated mails from my jsp application and had downloaded a code for the same... but now when i run this code it gives me javax.mail.AuthenticationFailedException...can any one pls tell me how to rectify this...plzzz...
here is the code...
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendEmail
{
public static void main(String[] args)
{
System.out.println(args.length);
if (args.length != 6)
{
System.out.println("usage: sendmessage <to> <from> <smtphost> <true|false> <subject> <text>");
System.exit(1);
}SendEmail m=new SendEmail();
m.SendMessage(args[0],args[1], args[2], args[3], args[4], args[5]);
}
public static String SendMessage(String emailto, String emailfrom, String smtphost, String emailmultipart, String msgSubject, String msgText)
{
boolean debug = false; // change to get more information
String msgText2 = "multipart message";
boolean sendmultipart = Boolean.valueOf(emailmultipart).booleanValue();
// set the host
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.username", "ditty@yahoo.com");
props.put("mail.smtp.password", "mypassword");
Authenticator loAuthenticator = new SMTPAuthenticator();
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props,loAuthenticator);
session.setDebug(debug);
try
{
// create a message
Message msg = new MimeMessage(session);
// set the from
InternetAddress from = new InternetAddress(emailfrom);
msg.setFrom(from);
InternetAddress[] address =
{
new InternetAddress(emailto)
};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(msgSubject);
if(!sendmultipart)
{
// send a plain text message
msg.setContent(msgText, "text/plain");
}
else
{
// send a multipart message// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(msgText, "text/plain");
// create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setContent(msgText2, "text/plain");
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
}
Transport transport = session.getTransport("smtp");
transport.connect(smtphost,"ditty@yahoo.com","mypassword");
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
catch(MessagingException mex)
{
mex.printStackTrace();
}
return "Email sent to " + emailto;
}
}
class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = "ditty@yahoo.com";
String password = "mypassword";
return new PasswordAuthentication(username, password);
}
}

