33

I am using javax.mail to send mails in Java. Now that a part of the concept of my project changed I have to send a mail without authentication. I will have to change my createSession() method:

private void createSession() {
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", port);

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

It is rather obvious that I should change mail.smtp.auth to false, but what else should I change?

muffin
  • 1,456
  • 4
  • 21
  • 44

2 Answers2

34
private void createSession() {
    properties.put("mail.smtp.auth", "false");
     //Put below to false, if no https is needed
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", port);

    session = Session.getInstance(properties);
}

I think, this would suffice.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • 4
    And of course it depends on your server being configured to allow you to send mail without authenticating first. That might work fine within an intranet, otherwise it's an invitation to spam. – Bill Shannon Oct 01 '13 at 18:18
  • Still says `Authentication Required. Please contact support` – Sami Jun 24 '16 at 20:19
  • 4
    Authentication is a server policy. If the server requires authentication - you have to provide it!! – Kris Jul 15 '16 at 06:16
  • 1
    put properties.put("mail.smtp.starttls.enable", "false"); as well.It worked for this after this change. – Farooque Feb 22 '18 at 10:14
  • This does not work if change mobile date, can anyone help me in this. – Vishwa Pratap Mar 25 '20 at 12:48
-3
private void createSession() {
  properties.put("mail.smtp.auth",false);
  properties.put("mail.smtp.starttls.enable","true");
  properties.put("mail.smtp.port","587");
  properties.put("mail.smtp.host","smtp.gmail.com");
  properties.put("mail.smtp.username","username(emailid")");
  properties.put("mail.smtp.password","password(mail password)");

  Session session=Session.getDefaultInstance(properties,null);

}
R.A
  • 1,813
  • 21
  • 29