This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Sending mail with authentication to Exchange Server 2007 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending mail with authentication to Exchange Server 2007" Watch "Sending mail with authentication to Exchange Server 2007" New topic
Author

Sending mail with authentication to Exchange Server 2007

sunil sushil
Greenhorn

Joined: Dec 25, 2006
Posts: 4
Hello all,

I heard that Exchange server 2007 has a differernt mechanism of authentication.
I am using a simple program to send a test mail to my outlook inbox (exchange server 2007). But it asks for auhentication when i ran it.
Then i tried giving my username and password. But i am still facing the same problem.

*com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated*

*at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1668)*
*at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1207)*
*at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:735)*
*at javax.mail.Transport.send0(Transport.java:191)*
*at javax.mail.Transport.send(Transport.java:120)*
*at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:64)*
*at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:25)*

I hear that exchange server 2007 has different way of giving username.(with domain name\username)
I tried the same, But i am still getting the same error.

Please help me.

I am adding the price of code:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SendingEmail
{
public static void main(String [] args)
{

String to = " vis.v@ap.abc.com";
String from = "sun.c@ap.abc.com";
String host = "43.11.11.11";

String user="ap\\pmtuser";
String pass="password123";
Properties properties = System.getProperties();
System.out.println(user);
// Setup mail server
properties.put("mail.smtp.host", host);
properties.put("mail.user", user);
properties.put("mail.password", pass);
//properties.setProperty("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
//System.out.println("Sent message successfully....");
}catch (Exception mex) {
mex.printStackTrace();
}
}
}

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't think it's anything specific to do with Exchange 2007 or how it requires you to specify user IDs. I think it's just that you don't have any code which does authentication of any kind.
sunil sushil
Greenhorn

Joined: Dec 25, 2006
Posts: 4
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailExample {
public static void main (String args[]) {

String to = " sunil.c@ap.sh.com";
String from = "sunil.c@ap.sh.com";
String host = "43.11.11.12";

try {
// Get system properties
Properties props = System.getProperties();

Authenticator authenticator = new Authenticator();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
// Get session
Session session = Session.getInstance(props, authenticator);
session.setDebug(true);

// Define message
MimeMessage message = new MimeMessage(session);

// Set the from address
message.setFrom(new InternetAddress(from));

// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

// Set the subject
message.setSubject("Hello JavaMail");

// Set the content
message.setText("Welcome to the JavaMail");
// Send message
Transport.send(message);
System.out.println("OK Man");
}

catch (MessagingException e) {e.toString();}
catch (Exception mex) {
mex.printStackTrace();}
}

static class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;

public Authenticator() {
String username = "ap\\pmtuser";
String password = "password123";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}



THis is the class i am using.
Here i have set property as mail.smtp.auth as TRUE.
After searching lot in forums..i hope i have added the authentication part in the code..

Please help me.

Also when i debug the code I get this:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "43.11.11.12", port 25, isSSL false
220 INSISCXHT02.ap.sony.com Microsoft ESMTP MAIL Service ready at Wed, 20 Jan 2010 12:21:58 +0530
DEBUG SMTP: connected to host "43.11.11.12", port: 25

EHLO INSARDWS-I420
250-INSISCXHT02.ap.sony.com Hello [43.80.70.90]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
DEBUG SMTP: mechanism LOGIN not supported by server
DEBUG SMTP: mechanism PLAIN not supported by server
DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
DEBUG SMTP: use8bit false
MAIL FROM:<sunil.c@ap.sh.com>
530 5.7.1 Client was not authenticated
DEBUG SMTP: got response code 530, with response: 530 5.7.1 Client was not authenticated

RSET
DEBUG SMTP: EOF: [EOF]
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1668)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1207)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:735)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at MailExample.main(MailExample.java:41)
QUIT
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Please use code tags. You can edit your post to add them. Also add them around the debugging output and stack trace.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sunil sushil
Greenhorn

Joined: Dec 25, 2006
Posts: 4
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailExample {
public static void main (String args[]) {

String to = " sunil.c@ap.sh.com";
String from = "sunil.c@ap.sh.com";
String host = "43.11.11.12";

try {
// Get system properties
Properties props = System.getProperties();

Authenticator authenticator = new Authenticator();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
// Get session
Session session = Session.getInstance(props, authenticator);

// Define message
MimeMessage message = new MimeMessage(session);

// Set the from address
message.setFrom(new InternetAddress(from));

// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

// Set the subject
message.setSubject("Hello JavaMail");

// Set the content
message.setText("Welcome to the JavaMail");
// Send message
Transport.send(message);
System.out.println("OK Man");
}

catch (MessagingException e) {e.toString();}
catch (Exception mex) {
mex.printStackTrace();}
}

static class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;

public Authenticator() {
String username = "ap\\pmtuser";
String password = "password123";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}



THis is the class i am using.
Here i have set property as mail.smtp.auth as TRUE.
After searching lot in forums..i hope i have added the authentication part in the code..

Please help me.

Also when i debug the code I get this:

I am trying to read the statements here.

I see this line.

DEBUG SMTP: trying to connect to host "43.11.12.13", port 25, isSSL false
220 INSXHT02.ap.sh.com Microsoft ESMTP MAIL Service ready at Wed, 20 Jan 2010 12:33:50 +0530
DEBUG SMTP: connected to host "43..11.12.13", port: 25


It shows that it is able to connect to our exchange server.

The second line:

EHLO INSARDWS-I420
250-INSXHT02.ap.sh.com Hello [43.88.11.11]


This is my machine name (INSARDWS-I420) and I feel hello message from server to my machine IP (INSXHT02.ap.sh.com Hello [43.88.11.11])

Third line:

DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
DEBUG SMTP: mechanism LOGIN not supported by server
DEBUG SMTP: mechanism PLAIN not supported by server
DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
DEBUG SMTP: use8bit false
MAIL FROM:<sunil.c@ap.sh.com>
530 5.7.1 Client was not authenticated


I feel there is some issue here : DEBUG SMTP: mechanism LOGIN not supported by server

ramesh ramineedi
Greenhorn

Joined: Dec 29, 2009
Posts: 6
You need activation.jar, smtp.jar, and mailapi.jar in your classpath for this to work.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, that's a slight improvement. You managed to use code tags for one line of code. The rest is still unformatted, as you should be able to see if you look at your post. So moving along from that code...
sunil sushil wrote:I feel there is some issue here : DEBUG SMTP: mechanism LOGIN not supported by server

I think it's time for you to talk to the administrator of your SMTP server. Ask them why the server isn't supporting any of those login mechanisms.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending mail with authentication to Exchange Server 2007
 
Similar Threads
Java Mail Problem
how to send a mail using java mail api
Getting SMTP transport error-Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 5
urgent help in java mail
JavaMail : "Access not allowed"