8

I want my program to be able to send emails from my corporate outlook account. I looked at many JMA examples the do not seem to be what I want.

  1. Where can I find simple examples of sending mails via outlook?
  2. Should I move mailing system to separate service-application? and if so, why?
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
VB_
  • 45,112
  • 42
  • 145
  • 293
  • Did you try any command-lines to try sending commands to the Outlook application via Java? – iWumbo Dec 16 '13 at 14:48
  • Have a look at a related post: http://stackoverflow.com/questions/4681703/how-to-send-an-email-using-ms-exchange-server – Axel Kemper Dec 16 '13 at 15:10

4 Answers4

11

You need to download javax.mail JAR first. Then try the following code:

import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    public static void main(String[]args) throws IOException {

        final String username = "enter your username";
        final String password = "enter your password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "outlook.office365.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("enter your outlook mail address"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("Enter the recipient mail address"));
            message.setSubject("Test");
            message.setText("HI");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Rakshith
  • 644
  • 1
  • 8
  • 24
  • Not working for me. I keep getting an error saying "550 5.2.1 Mailbox cannot be accessed". A colleague of mine is using the same credentials, although in C#, and calling an exchange web service, and it's working for him. – Kirill Yunussov Feb 24 '17 at 20:44
  • Thank you for the code, But getting hunged in Transport.send(message); line. – MukeshKoshyM Mar 25 '19 at 19:10
  • It could be because of proxy. Could you please check the same? – Rakshith Mar 26 '19 at 03:54
5

All you need is your SMTP settings for your corporate account. Set these in your program using Java mail API and thats it. e.g.

Properties props = System.getProperties();
props.put("mail.smtp.host", "your server here");
Session session = Session.getDefaultInstance(props, null);

example: here and here

adi
  • 1,711
  • 3
  • 29
  • 50
  • what should be the server for outlook? Provide me example, please – VB_ Dec 16 '13 at 15:30
  • Check your outlook configuration, or ask the admin\network team. Or if you are using Outlook web access, check outlook about page e.g. https://server_name_here/owa/?ae=Options&opturl=Messaging – adi Dec 16 '13 at 15:46
  • thanks it works. But I need to send emails on behalf of special user/acount. How should I set username/password? – VB_ Dec 17 '13 at 08:42
  • please see the examples posted. You can use e.g. msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin")); – adi Dec 17 '13 at 09:54
  • I just can't grasp what is the second argument in InternetAddress constructor. As I understand without providing username/password I will send email on behalf of already logined user. Am I right? But I need to send emails on behalf of testUser/12345. Can you provide me a litle example? – VB_ Dec 17 '13 at 12:22
  • Hello. What if I don't want to include the username/password of the sender on the code? Can I do that? – Erick Apr 15 '15 at 02:41
2

I tried with outlook.office365.com as a host name and got authentication unaccepted exception. When trying with smtp-mail.outlook.com I'm able to send mails through outlook with Javamail API.

For further detail Check out the settings of Outlook on outlook official site.

For complete working demo code Read this answer.

Community
  • 1
  • 1
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
-2

From what I can see, you are missing the following import:

import java.io.IOException;

Just include it in the class imports at the peginning of this class and your issue should be solved.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
  • 3
    Can you expand on this answer? Moderators generally look for answers with more substance that give a more complete example of the necessary solution. – Jordan Kasper Dec 01 '14 at 15:35