• Post Reply Bookmark Topic Watch Topic
  • New Topic

AuthenticatedFailedException is thrown javamail  RSS feed

dinakarkalaga kalaga
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following Code works fine,but once in a while i am getting authentication failedexception even if i am setting properties for authentication to true and including authenticator mentioned in the comments
please help me to solve this problem

code is mentioned below

import java.security.Security;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main
private static final String SMTP_HOST_NAME = "smtp.mycompanyname.com";
private static final String SMTP_PORT = "25";
private static final String emailMsgTxt = "Test Message Contents";
private static final String emailSubjectTxt = "A test from gmail";
private static final String emailFromAddress = "checkit@mycompanyname.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"mymailid@gmail.com"};

public static void main(String args[]) throws Exception {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new Main().sendSSLMessage(sendTo, emailSubjectTxt,emailMsgTxt, emailFromAddress);
catch(Exception e)
System.out.println("Sucessfully Sent mail to All Users");
}//end of mainmethod

public void sendSSLMessage(String recipients[], String subject,String message, String from) throws MessagingException,AuthenticationFailedException
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "false");
//props.put("mail.debug", "false");
//props.put("mail.smtp.port", SMTP_PORT);
//props.put("mail.smtp.socketFactory.port", SMTP_PORT);

//props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
//props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,null);
/* new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("uhelpme@mycompanyname.com","asdafd");
}}; */
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
addressTo[i] = new InternetAddress(recipients[i]);
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setContent(message, "text/plain");
Transport transport = session.getTransport("smtp");
transport.sendMessage(msg, msg.getAllRecipients());

}//end of sendsslmessage
}//end of class
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!