javamail error while authenticating to SMTP server

A

ameyas7

hi all,

i wrote a simple program as follows.
i am trying to send mail through SMTP server.
i have set the following property to true
props.put("mail.smtp.auth", "true");


Transport transport = session.getTransport("smtp");
transport.connect("localhost", "(e-mail address removed)", "");
transport.sendMessage(message,addr);
transport.close();




javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at sendmail.EMailHandler.send(EMailHandler.java:53)
at sendmail.demo.SendMailDemo.main(SendMailDemo.java:11)

i am not sure which username and password we need to provide while
connecting to the server.

i have installed hMailServer i tried using Administrator password for
server but did not work.
i tried using the from mail a/c user name , password but no luck.

can someone tell me what am i missing here ? which username , password
do we need to give. do we need any specific setting on smtp server ?
any example ?



On setting DEBUG true i get the following trace from the SMTP server.


DEBUG: setDebug: JavaMail version 1.3.2
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun
Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL
false
220 USER-E1B9B7EDB6 ESMTP
DEBUG SMTP: connected to host "localhost", port: 25

EHLO user-e1b9b7edb6
250-hmailserver
250-SIZE
250 AUTH LOGIN PLAIN
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
cm9vdEBsb2NhbGhvc3QuY29t
334 UGFzc3dvcmQ6

535 Authentication failed. Restarting authentication process.
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at sendmail.EMailHandler.send(EMailHandler.java:54)
at sendmail.demo.SendMailDemo.main(SendMailDemo.java:11)



thanks
amey
 
A

Amit Jain

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class MailClient{

static String SMTP_SERVER="smtp.gmail.com";
static String FROM_USER="(e-mail address removed)";
static String SMTP_USER_NAME="(e-mail address removed)";
static String SMTP_USER_PASSWD="smtp_user_passwd";
static String SMTP_PORT="465";

public void sendMailTo(Properties props,String from, String to,
String subject, String messageText){
try{
//boolean sessionDebug = false;
boolean sessionDebug = true;
String mailer = "VisitIndia";

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));

InternetAddress[] addressTo = new InternetAddress[2];
addressTo[0] = new InternetAddress(to);
addressTo[1] = new
InternetAddress("(e-mail address removed)");

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

//
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,
false));
msg.setSubject(subject);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
msg.setContent(messageText, "text/html");
Transport.send(msg);
System.out.println("\nMail was sent successfully.");
}catch (SendFailedException mssEx) {
System.out.print("SendFailedException:-> "+mssEx);
System.out.print("message "+mssEx.getLocalizedMessage());
System.out.print("invalid address:->
"+mssEx.getInvalidAddresses());
System.out.print("valid add:->
"+mssEx.getValidSentAddresses());
System.out.print("unsent add:->
"+mssEx.getValidUnsentAddresses());
}catch (MessagingException mex) {
mex.printStackTrace();
}
}catch(Exception e){

// Handle any exceptions, print error message.
//System.err.println(e);
}
}

public static void main(String str[]){


// set property for gmail smtp
Properties props = System.getProperties();
props.put("mail.smtp.host",SMTP_SERVER);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
//props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
new
MailClient().sendMailTo(props,FROM_USER,"(e-mail address removed)","Explore","message");
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{

public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_USER_NAME;
String password = SMTP_USER_PASSWD;
return new PasswordAuthentication(username, password);
}
}
}
 
R

Roedy Green

i wrote a simple program as follows.
i am trying to send mail through SMTP server.
i have set the following property to true
props.put("mail.smtp.auth", "true");


Transport transport = session.getTransport("smtp");
transport.connect("localhost", "(e-mail address removed)", "");
transport.sendMessage(message,addr);
transport.close();

you need something like this:

Properties props = System.getProperties();
if ( NEED_PASSWORD_TO_SEND )
{
props.setProperty( "mail.smtp.auth", "true" );
}
// Get a Session object
session = Session.getDefaultInstance( props, null );
session.setDebug( DEBUGGING );

Transport transport = session.getTransport( SEND_PROTOCOL );
transport.connect( SEND_HOST,
SEND_PORT,
SEND_LOGIN_ID,
SEND_PASSWORD );

There might also be a way to do it with an Authenticator.
See http://mindprod.com/jgloss/authentication.html
 
Joined
Aug 31, 2007
Messages
1
Reaction score
0
I think you're very close... the properties seem to be correct. You may use this:
Session session = Session.getDefaultInstance(properties, auth);
SMTPTransport transport = (SMTPTransport)session.getTransport("smtp");
transport.connect();

//create your SMTPMessage, "message".
transport.sendMessage(message, message.getAllRecipients());

Cheers, tommy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top