3

I am trying this 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 NewClass {

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



final String username = "prashantsood_90";
final String password = "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("prashantsood_90@outlook.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("prashantsood_90@outlook.com"));
    message.setSubject("Test");
    message.setText("HI");

    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}
}
}

It is giving this exception:

Exception in thread "main" java.lang.RuntimeException:   javax.mail.SendFailedException: Sending failed;
 nested exception is:
 class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

  at NewClass.main(NewClass.java:47)
  Caused by: javax.mail.SendFailedException: Sending failed;
  nested exception is:
 class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at NewClass.main(NewClass.java:42) 
 Java Result: 1

Kindly help me how to send email from my outlook account to gmail or any other account.I am unable to send using outlook account but I am able to send gmail to gmail mails.

Chris McAtackney
  • 5,192
  • 8
  • 45
  • 69
AK2
  • 111
  • 2
  • 13
  • You can automate Outlook, see How to create an E-Mail in Outlook and make it visible for the User for more information at http://stackoverflow.com/questions/18057759/how-to-create-an-e-mail-in-outlook-and-make-it-visible-for-the-user. – Eugene Astafiev May 05 '15 at 12:33

1 Answers1

1

Try writing your own Authenticator class

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
    private String userName;
    private String password;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public SMTPAuthenticator(String userName,String password){
        this.userName=userName;
        this.password=password;
    }

    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(userName, password);
    }
}

Then use it to create the session:

SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", "outlook.office365.com");
properties.setProperty("mail.smtp.port", "587");

Session session = Session.getInstance(properties, authenticator);

Also remove this line if you don't have TSL protected connection

props.put("mail.smtp.starttls.enable", "true");
MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • Could you plz reply the whole code after editing.I am unable to understand it. – AK2 May 05 '15 at 10:21
  • 1
    @AK2 just create a new class for the Authenticator, then replace your code (the part with the properties and session) with this piece of code – MihaiC May 05 '15 at 10:28
  • 1
    When i write your SMTPAuthenticator then it gives error on this line `return new PasswordAuthentication(userName, password);` it says PasswordAuthentication cannot be applied to given types. – AK2 May 05 '15 at 11:01
  • @AK2 make sure you class extends Authenticator class; **SMTPAuthenticator extends Authenticator** and you have included the proper imports. check edit – MihaiC May 05 '15 at 13:13