I am trying to send mails use SMTP.GMAIL.COM to send mail via JavaMail
1. I use properties file to get the port information etc in message.properties file
2. When I hard code the username and password - the program sends mail sucessfully.
3. When I use the variable with smtp_username and smtp_password , the program throws an error:
Exception in
thread "main" javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1
http://mail.google.com/support/bin/answer.py?answer=14257 u32sm4165052weq.35
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:313)
at com.bmb.services.MailJavaMail.test(MailJavaMail.java:177)
at com.bmb.services.MailJavaMail.main(MailJavaMail.java:44)
_____________________________________________________
import java.io.*;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.bmb.util.PropsUtils;
import java.net.URL;
public class MailJavaMail {
private static
String mail_transport_protocol = null, smtp_host_name=null, smtp_username=null,smtp_password=null;
public static void main(String[] args) throws Exception{
String myexception_message=null;
new MailJavaMail().test(myexception_message);
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtp_username, smtp_password);
}
}
public static void
test(String myexception_message) throws Exception{
System.out.println("test");
final Properties props = new Properties();
String txtmessage = null,from_message=null,to_message=null,to_address=null,cc_address=null,from_address=null,from_name=null,message_subject=null,message_body=null;
//private static final String smtp_host_name = null,smtp_username=null,smtp_password=null;
int smtp_host_port=0;
try {
PropsUtils.load( props, PropsUtils.class, "message.properties" );
} catch (IOException e) {
e.printStackTrace();
}
//mail_transport_protocol
if(props.getProperty("mail_transport_protocol") !=null)
{
mail_transport_protocol = props.getProperty("mail_transport_protocol");
// System.out.println("smtp_host_name"+smtp_host_name);
}
if(props.getProperty("smtp_host_name") !=null)
{
smtp_host_name = props.getProperty("smtp_host_name");
// System.out.println("smtp_host_name"+smtp_host_name);
}
if(props.getProperty("smtp_host_port") !=null)
{
String st_smtp_host_port = props.getProperty("smtp_host_port");
smtp_host_port = Integer.parseInt(st_smtp_host_port);//st_smtp_host_port.parseInt();
System.out.println("smtp_host_port"+smtp_host_port);
}
//smtp_username
if(props.getProperty("smtp_username") !=null)
{
smtp_username = props.getProperty("smtp_username");
System.out.println("smtp_username"+smtp_username);
}
//smtp_password
if(props.getProperty("smtp_password") !=null)
{
smtp_password = props.getProperty("smtp_password");
System.out.println("smtp_password"+smtp_password);
}
if(props.getProperty("to_address") !=null)
{
to_address = props.getProperty("to_address");
System.out.println("to_address"+to_address);
}
if(props.getProperty("cc_address") !=null)
{
cc_address = props.getProperty("cc_address");
System.out.println("cc_address"+cc_address);
}
if(props.getProperty("from_address") !=null)
{
from_address = props.getProperty("from_address");
System.out.println("from_address"+from_address);
}
if(props.getProperty("from_name") !=null)
{
from_name = props.getProperty("from_name");
System.out.println("from_name"+from_name);
}
if(props.getProperty("message_subject") !=null)
{
message_subject = props.getProperty("message_subject");
System.out.println("message_subject"+message_subject);
}
if(props.getProperty("message_body") !=null)
{
message_body = props.getProperty("message_body");
System.out.println("message_body"+message_body);
}
//SMTPAuthenticator auth = new SMTPAuthenticator(smtp_username,smtp_password);
//Session session = Session.getInstance(props,auth);
//session.setDebug(true);
//Authenticator auth = new SMTPAuthenticator();
//Session session = Session.getInstance(props, auth);
props.put("mail.transport.protocol", mail_transport_protocol);
props.put("mail.host",smtp_host_name);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.sendpartial","true");
//props.put("mail.smtp.socketFactory.port", "465");
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
//props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
//props.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtp_username, smtp_password);
}} );
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
myexception_message=message_subject.trim()+myexception_message;
message_body=myexception_message+message_body.trim();
message.setSubject((myexception_message));
message.setContent(message_body.trim(), "text/plain");
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to_address.trim()));
message.addRecipient(Message.RecipientType.CC,new InternetAddress(cc_address.trim()));
//transport.connect(smtp_host_name, smtp_host_port,SMTP_AUTH_USER,SMTP_AUTH_PWD);
System.out.println("-------------------smtp_username"+smtp_username);
System.out.println("-------------------smtp_username"+smtp_password);
transport.connect(smtp_host_name, smtp_host_port,smtp_username,smtp_password);
//transport.connect(smtp_host_name, smtp_host_port,"xxx@googlemail.com","xxx");
System.out.println("Message Recipient"+Message.RecipientType.TO);
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.sendMessage(message,message.getRecipients(Message.RecipientType.CC));
transport.close();
}
}
_______________________________________________________
PropsUtils.java
package com.bmb.util;
import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.URL;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
/**
* PropertiesUtil is used to load and save properties to files.
* <p>
* @author Robin Sharp
*/
public class PropsUtils
{
public PropsUtils()
{
}
public static Map getByValue( Properties properties, String value )
{
Map map = new HashMap();
Iterator iter = properties.entrySet().iterator();
do
{
if(!iter.hasNext() )
break;
java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();
if(entry.getValue().equals(value ) )
map.put(entry.getKey(), entry.getValue() );
} while(true );
return map;
}
public static Properties load( Properties properties, String fileName ) throws IOException
{
if(fileName == null )
{
throw new IllegalArgumentException( "FileName is not set." );
}
else
{
return load( properties, new File(fileName ) );
}
}
public static Properties load( Properties properties, File file ) throws IOException
{
if(file == null )
{
throw new IllegalArgumentException( "File is not set." );
}
else
{
return load( properties, ((InputStream ) ( new FileInputStream(file ) )) );
}
}
public static Properties load( Properties properties, URL url ) throws IOException
{
if( url == null )
{
throw new IllegalArgumentException( "Url is not set." );
}
else
{
URLConnection connection = url.openConnection();
return load( properties, connection.getInputStream() );
}
}
public static Properties load( Properties properties, Class relativeClass, String relativeFileName ) throws IOException
{
if(relativeClass == null )
{
throw new IllegalArgumentException( "Relative Class is not set." );
}
if(relativeFileName == null )
{
throw new IllegalArgumentException( "Relative File Name is not set." );
}
else
{
return load( properties, relativeClass.getResourceAsStream(relativeFileName ) );
}
}
public static Properties load( Properties properties, InputStream inputStream ) throws IOException
{
try
{
if( properties == null )
{
throw new IllegalArgumentException( "Properties is not set." );
}
if( inputStream == null )
{
throw new IllegalArgumentException( "InputStream is not set." );
}
properties.load( new BufferedInputStream(inputStream ) );
}
finally
{
if(inputStream != null )
inputStream.close();
}
return properties;
}
public static void store( Properties properties, String fileName ) throws IOException
{
if(fileName == null )
{
throw new IllegalArgumentException( "FileName is not set." );
}
else
{
store( properties, new File(fileName ) );
return;
}
}
public static void store( Properties properties, File file )
throws IOException
{
if(file == null )
{
throw new IllegalArgumentException( "File is not set." );
} else
{
store( properties, ((OutputStream ) ( new FileOutputStream(file ) )) );
return;
}
}
public static void store( Properties properties, URL url ) throws IOException
{
if( url == null )
{
throw new IllegalArgumentException( "Url is not set." );
} else
{
URLConnection connection = url.openConnection();
connection.setDoOutput(true );
store( properties, connection.getOutputStream() );
return;
}
}
public static void store( Properties properties, Class relativeClass, String relativeFileName )
throws IOException
{
if(relativeClass == null )
throw new IllegalArgumentException( "Relative Class is not set." );
if(relativeFileName == null )
{
throw new IllegalArgumentException( "Relative File Name is not set." );
}
else
{
String className = relativeClass.getName().substring(relativeClass.getName().lastIndexOf( "." ) + 1);
URL url = relativeClass.getResource(String.valueOf(String.valueOf( className ) ).concat( ".class" ) );
String fileName = url.getFile();
fileName = String.valueOf(fileName.substring(1, fileName.length() - className.length() - 6) ) + String.valueOf(relativeFileName );
store( properties, fileName );
return;
}
}
public static void store( Properties properties, OutputStream outputStream ) throws IOException
{
try
{
if( properties == null )
{
throw new IllegalArgumentException( "Properties is not set." );
}
if( outputStream == null )
{
throw new IllegalArgumentException( "OutputStream is not set." );
}
properties.store( new BufferedOutputStream( outputStream ), null );
}
finally
{
if( outputStream != null )
{
outputStream.close();
}
}
}
public static void main(String args[])
{
try
{
Properties props = new Properties();
load( props, PropsUtils.class, "message.properties" );
String to_address=null;
if(props.getProperty("to_address") !=null)
{
to_address = props.getProperty("to_address");
System.out.println("to_address"+to_address);
}
else
{
System.out.println("null");
}
}
catch(Exception e )
{
e.printStackTrace();
}
}
}