Solved

Authentication with JavaMail not working

Posted on 2003-12-09
3
3,184 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
ID: 9910389
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
ID: 9910413
 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
ID: 9916274
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

820 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