Skip to Content
author's profile photo Former Member
Former Member

Unable to send mail from exchange server using javamail api


I am trying to send a mail from a java application using java mail api and my mail server is exchange server.please find the code which i used


// Get system properties

Properties props = System.getProperties();

// Setup mail server

props.put("", "");

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

// Get session

Authenticator loAuthenticator = new MailAuthenticator();

Session session = Session.getInstance(props, loAuthenticator);

// Define message

MimeMessage message =

new MimeMessage(session);


new InternetAddress(""));



new InternetAddress(""));


"Hello JavaMail Attachment");

// create the message part

MimeBodyPart messageBodyPart =

new MimeBodyPart();

//fill message


/*Multipart multipart = new MimeMultipart();


// Part two is attachment

messageBodyPart = new MimeBodyPart();

messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(pdf.toString(), "application/pdf")));



// Put parts in message


// Send the message

Transport.send( message );


catch(Exception ex){


do {

if (ex instanceof SendFailedException) {

SendFailedException sfex = (SendFailedException) ex;

Address[] invalid = sfex.getInvalidAddresses();

if (invalid != null) {

for (int i = 0; i < invalid.length; i++) {

System.out.println("Invalid address: " +




Address[] validUnsent =


if (validUnsent != null) {

for (int i = 0; i < validUnsent.length; i++) {

System.out.println("Valid unsent address: " +




Address[] validSent = sfex.getValidSentAddresses();

if (validSent != null) {

for (int i = 0; i < validSent.length; i++) {

System.out.println("Valid sent address: " +





if (ex instanceof MessagingException) {

ex = ((MessagingException) ex).getNextException();

} else {

ex = null;


} while (ex != null);



But i am getting the following exception


Sending failed;

nested exception is:

javax.mail.SendFailedException: Invalid Addresses;

nested exception is:

javax.mail.SendFailedException: 550 5.7.1 Unable to relay for

Invalid address:

Invalid address:

it is saying <b>to address</b> is invalid.

<b>I did check with my mail admin and added my ip address to the acl list of the exchange server .still i am getting the same problem.</b>

Any pointers on this will be rewarded with points

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Nov 06, 2007 at 07:38 PM

    The error you face is an SMTP error, that tells you, that the Exchange server is not allowing you to send mail through it to

    Is your <b>sender</b>address a valid one and does your Exchange server allow you to send via SMTP from this address to an external one?



    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 05, 2007 at 08:25 AM

    Hello Bala,

    if you want to send an email with an smtp server with authentification you must use this code:

        Transport transport = session.getTransport("smtp");
        transport.sendMessage(message, message.getAllRecipients());

    Best regards,


    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.