Solved

Authentication with JavaMail not working

Posted on 2003-12-09
3
3,195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

630 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