Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3199
  • Last Modified:

Authentication with JavaMail not working

Hi,

I am trying to send mail using JavaMail, via an SMTP server, I use authentication..

However I get an authentication required error....

Here is the output script


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]

DEBUG SMTP: useEhlo true, useAuth false

DEBUG: SMTPTransport trying to connect to host "[XXX.XXX.XXX.XXX]", port 25

DEBUG SMTP RCVD: 220 ArGoSoft Mail Server Freeware, Version 1.8 (1.8.4.5)

DEBUG: SMTPTransport connected to host "[XXX.XXX.XXX.XXX]", port: 25

DEBUG SMTP SENT: EHLO MSBEJ01W
DEBUG SMTP RCVD: 250-Welcome [XXX.XXX.XXX.XXX-2], pleased to meet you
250-AUTH=LOGIN
250-AUTH LOGIN
250-SIZE 5242880
250 HELP

DEBUG SMTP Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP Found extension "AUTH", arg "LOGIN"
DEBUG SMTP Found extension "SIZE", arg "5242880"
DEBUG SMTP Found extension "HELP", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<admin@meganexus.com>
DEBUG SMTP RCVD: 250 Sender "admin@meganexus.com" OK...

DEBUG SMTP SENT: RCPT TO:<janes@glocalnet.net>
DEBUG SMTP RCVD: 550 User not local.  Authentication required for relay

Invalid Addresses
  janes@glocalnet.net
DEBUG SMTPTransport: Sending failed because of invalid destination addresses
DEBUG SMTP SENT: RSET
DEBUG SMTP RCVD: 250 Reset state

DEBUG SMTP SENT: QUIT
javax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        javax.mail.SendFailedException: 550 User not local.  Authentication required for relay


Here is the Java
        Properties          mEmlProps   =new Properties(System.getProperties());
        Authenticator auth=new myAuth();
       
        if (mLog.isDebugEnabled()) {
            mLog.debug("to:"+to);
            mLog.debug("from:"+from);
            mLog.debug("subject:"+subject);
            mLog.debug("msg:"+msg);
            System.out.println("to:"+to);
            System.out.println("from:"+from);
            System.out.println("subject:"+subject);
            System.out.println("msg:"+msg);
        }
        mEmlProps.put("mail.smtp.host",mProps.getProperty("mail.smtp.host"));
        Session mSession=Session.getDefaultInstance(mEmlProps,auth);
        mSession.setDebug(true);//mLog.isDebugEnabled());
        MimeMessage mMsg=new MimeMessage(mSession);
        try {
            InternetAddress mFromAddress=new InternetAddress(from);
            mMsg.setFrom(mFromAddress);
        } catch (AddressException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        } catch (MessagingException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        } catch (Exception e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        }
        try {
            InternetAddress mToAddress=new InternetAddress(to);
            mMsg.setRecipient(RecipientType.TO,mToAddress);
        } catch (AddressException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        } catch (MessagingException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        } catch (Exception e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        }
        try {
            mMsg.setSubject(subject);
        } catch (MessagingException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        }
        try {
            mMsg.setText(msg);
//            mMsg.setContent(msg,"text/plain; charset=UTF-8");
        } catch (MessagingException e) {
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        }
        try {
            Transport.send(mMsg);
        } catch (MessagingException e) {
            System.out.println(e);
            if (mLog.isDebugEnabled()) {
                mLog.debug(e.toString());
            }
        }

       
    }    
0
BenJanes
Asked:
BenJanes
  • 2
1 Solution
 
objectsCommented:
try adding:

mEmlProps.put("mail.smtp.auth", "true");

if that don't work check with your mail servers administrator.
0
 
girionisCommented:
 Also

"When you connect to the Transport you can pass a username and password:

Transport transport =
  session.getTransport("smtp");
transport.connect(
  host, username, password);

Unfortunately, this doesn't use the Authenticator class in javax.mail, so you would have to build in your own mechanism to prompt the user for this information."

  from: http://www.jguru.com/faq/printablefaq.jsp?topic=JavaMail
0
 
objectsCommented:
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now