Solved

Authentication with JavaMail not working

Posted on 2003-12-09
3
3,183 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
listing all functions in JavaScript 19 200
print map entry 34 67
Windows 10 and JDK 7 7u85 4 40
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

776 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