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", "root@localhost.com", "");
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
Amit Jain - 26 Aug 2007 08:50 GMT
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="from_user@gmail.com";
static String SMTP_USER_NAME="smtp_user_name@gmail.com";
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("visitindiaportal@gmail.com");
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,"to@gmail.com","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);
}
}
}
Roedy Green - 26 Aug 2007 10:47 GMT
>i wrote a simple program as follows.
>i am trying to send mail through SMTP server.
[quoted text clipped - 5 lines]
> 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

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com