Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

javamail send error

Thread view: 
priyom - 08 Feb 2006 14:39 GMT
Hi,

I am using javamail 1.4 API to send messages through a SMTP server.
The problem is I get the following error:

550 5.7.1 Unable to relay for a@b.com
(I have changed the actual address here-but it's a valid address)

Note: The Outgoing server requires authentication.
Probably I'm going wrong there...
Is there any way to verify if authentication has succeeded or not?

One more thing, there is no problem accessing Inbox - Incoming mail
(IMAP) works absolutely fine ...

Here is the code:

package Mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.swing.*;
import com.sun.mail.smtp.SMTPMessage;

// Send a simple, single part, text/plain e-mail
public class TestEmail {

   public static void main(String[] args) {

       String to = "a@b.com";
       String from = "a@b.com";
       String host = "a.b.c.d";//actual host string is entered here

       // Create properties, get Session
       Properties props = new Properties();

       props.put("mail.smtp.host", host);
       // To see what is going on behind the scene
       props.put("mail.debug", "true");
               Session session = Session.getInstance(props);

       try {
           // Instantiate a message
           SMTPMessage msg = new SMTPMessage(session);

           //Set message attributes
           msg.setFrom(new InternetAddress(from));
           InternetAddress[] address = {new InternetAddress(toBofa)};
           msg.setRecipients(Message.RecipientType.TO, address);
           msg.setSubject("Test E-Mail through Java");
           msg.setSentDate(new Date());

           // Set message content
           msg.setText("This is a test of sending a " +
                       "plain text e-mail through Java.\n" +
                       "Here is line 2.");

           // to enable authentication
           Transport tr = session.getTransport("smtp");
           String username="domain\username";//here actual domain and
username are entered
           String password=JOptionPane.showInputDialog("Enter pwd for
"+username);

           try {
               System.out.println("Trying to connect...");
               tr.connect(host, username, password);
               System.out.println("Hopefully
connected..."+tr.toString());
           }
           catch (AuthenticationFailedException ex)
           {
               JOptionPane.showMessageDialog(null,"Transport connect
failed");
           }
           JOptionPane.showMessageDialog(null,"Authenticated");
           msg.saveChanges();
           tr.sendMessage(msg, msg.getAllRecipients());
           tr.close();
           // end of authentication snippet

        }
       catch (MessagingException mex) {
           // Prints all nested (chained) exceptions as well
           mex.printStackTrace();
       }
   }
}

Output:
DEBUG SMTP: trying to connect to host "a.b.c.d", port 25, isSSL false
220 <host string> Microsoft ESMTP MAIL Service, Version: 5.0.2195.6713
ready at  Wed, 8 Feb 2006 08:31:49 -0600
DEBUG SMTP: connected to host "a.b.c.d", port: 25
.
.
.
.
MAIL FROM:<a@b.com>
250 2.1.0 a@b.com....Sender OK
RCPT TO:<a@b.com>
550 5.7.1 Unable to relay for a@b.com
DEBUG SMTP: Invalid Addresses
DEBUG SMTP:   a@b.com
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Resetting
Nigel Wade - 09 Feb 2006 10:27 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> Probably I'm going wrong there...
> Is there any way to verify if authentication has succeeded or not?

How is authentication configured on your mail server?

A client must only attempt authentication if the server offers it. This will be
done in response to an EHLO message sent by the client. The response to this
indicates the services offered by the mail server. If AUTH is not among them
then it is incorrect for the client to attempt authentication.

Some mail servers will offer AUTH on the normal SMTP port of 25 and some will
only offer it on the mail submission port, 587. You can find out using telnet
what services your mail server offers on those ports. Telnet to port 25 and
send it a EHLO request. If the response doesn't include AUTH then try the same
on port 587. If that does include AUTH then you need to modify your smtp
Transport connection set the port to 587. If neither include AUTH then you need
to contact Microsoft and ask them what non-standard authentication scheme they
have implemented for mail submission in Exchange/Outlook.

> One more thing, there is no problem accessing Inbox - Incoming mail
> (IMAP) works absolutely fine ...

That's an entirely different protocol, on a different port, for reading mail.

....
> Output:
> DEBUG SMTP: trying to connect to host "a.b.c.d", port 25, isSSL false
[quoted text clipped - 14 lines]
> RSET
> 250 2.0.0 Resetting

You've missed out the interesting bits...

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

priyom - 10 Feb 2006 14:02 GMT
The port connection requires port 25.
I connect using LAN
In Microsoft Outlook, Email accounts->More Settings->Outgoing Server
Tab->"Outgoing server requires authentication" Chk Box is checked
"Log on using" Radio btn is selected and my username/password
(different from login) are entered

The interesting bits I missed out:
Hope this helps
Looking forward to your response...

EHLO <My computer name>
250-<server name> Hello [<some address>]
250-TURN
250-ATRN
250-SIZE 20480000
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-XEXCH50
250-X-LINK2STATE
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "ATRN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20480000"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM LOGIN"
DEBUG SMTP: Found extension "X-EXPS=LOGIN", arg ""
DEBUG SMTP: Found extension "AUTH", arg "GSSAPI NTLM LOGIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "X-LINK2STATE", arg ""
DEBUG SMTP: Found extension "OK", arg ""
Nigel Wade - 13 Feb 2006 12:00 GMT
> The port connection requires port 25.
> I connect using LAN
[quoted text clipped - 9 lines]
> EHLO <My computer name>
> 250-<server name> Hello [<some address>]
...
> 250-X-EXPS GSSAPI NTLM LOGIN
> 250-X-EXPS=LOGIN
> 250-AUTH GSSAPI NTLM LOGIN
> 250-AUTH=LOGIN
...
> DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM LOGIN"
> DEBUG SMTP: Found extension "X-EXPS=LOGIN", arg ""
[quoted text clipped - 3 lines]
> DEBUG SMTP: Found extension "X-LINK2STATE", arg ""
> DEBUG SMTP: Found extension "OK", arg ""

Ok, so it is advertising AUTH on port 25. That should allow the client to
attempt authentication. Now you need to find out if it being attempted. Unless
the debug info includes whether auth was attempted, and succeeded, it won't be
simple.

For this type of problem I generally use a network sniffer such as Ethereal.
This allows you to capture the packets sent and received by your computer, and
look at both sides of the conversation to see what is actually being sent over
the wire. If you are ok to do this, I would recommend you install Ethereal and
use that. You can find it at http://www.ethereal.com/

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Gordon Beaton - 10 Feb 2006 14:37 GMT
> I am using javamail 1.4 API to send messages through a SMTP server.
> The problem is I get the following error:
>
> 550 5.7.1 Unable to relay for a@b.com
> (I have changed the actual address here-but it's a valid address)

The fact that your address is valid is not sufficient, since a
correctly configured mail server will only accept mail to or from its
own mail domain.

This is to prevent senders outside the domain from using the server to
"relay" mail to recipients who are also outside the domain.

At leat one of the addresses (to or from) must belong to a domain that
the server is willing to relay for (e.g. its own domain).

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

priyom - 12 Feb 2006 07:52 GMT
"The fact that your address is valid is not sufficient, since a
correctly configured mail server will only accept mail to or from its
own mail domain."
-What I meant was it's not an address problem. I am in the same domain,
connecting through LAN and trying to send a mail to myself, which I
presume would be in the same domain (I do have an ID created in the
domain with send capability)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.