Solved

Authentication with JavaMail not working

Posted on 2003-12-09
3
3,181 Views
Last Modified: 2013-11-24
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
Comment
Question by:BenJanes
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
try adding:

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

if that don't work check with your mail servers administrator.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 66
Safari, Firefox, IE, Java, Plugin, Java Security error 13 39
count11 challenge 6 47
maven example error 3 34
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now