Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email using MS exchange server

I am trying to sent an email using my company's mail server. But I am getting the following exception

Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
    at javax.mail.Transport.send0(Transport.java:169)
    at javax.mail.Transport.send(Transport.java:98)

Here is my sample code,

Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", "example.server.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "25");
// Get session
//Session session = Session.getDefaultInstance(props, null);
Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("username", "password");
        }
    });

// 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 JavaMail");

// Send message
Transport.send(message);

What piece of code is wrong? As as username and password , I am using my company's email address and password.

like image 495
user509755 Avatar asked Jan 13 '11 15:01

user509755


People also ask

How do I send an email from Microsoft Exchange?

Here's how to send email through your Microsoft Exchange or Office 365 account: Choose “Direct Mail > Preferences…” from the menu bar at the top of your screen. Click on the Accounts tab. Click the “+” button at the bottom of the window and select “Microsoft Account”

Does Microsoft Exchange have an SMTP Server?

Find your Exchange mailbox server settings In Outlook Web App, on the toolbar, select Settings. > Mail > POP and IMAP. The POP3, IMAP4, and SMTP server name and other settings you may need to enter are listed on the POP and IMAP settings page.


1 Answers

The 5.7.1 is probably caused by exchange and not your code. You may just need to enable relaying on the server. Either for anonymous users or from a certain IP address. I'm not an expert on Exchange but I have got this working before. Here is the last solution I tested that works:

If a 5.7.1 error is encountered when trying to send an email via SMTP on an exchange server when the user has been authenticated..

For ref the issue you just had was caused by a setting on the Exchange 2007 server – this would not normally be a problem on 2003 server

Fixed by doing below...

You can set this authentication setting via the GUI

  • In Server configuration / Hub Transport / Default <ServerName>
  • Right click, properties, Permission Groups
  • Check "Anonymous users" and then click OK

Obviously anon users is not too secure but you could see if this solves the problem.

like image 139
WraithNath Avatar answered Oct 21 '22 01:10

WraithNath