Send mail in javax.mail without authentication

67,440
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.

Share:
67,440
muffin
Author by

muffin

Updated on February 14, 2020

Comments

  • muffin
    muffin over 4 years

    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?

  • Bill Shannon
    Bill Shannon over 10 years
    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.
  • Sami
    Sami almost 8 years
    Still says Authentication Required. Please contact support
  • Kris
    Kris almost 8 years
    Authentication is a server policy. If the server requires authentication - you have to provide it!!
  • Farooque
    Farooque over 6 years
    put properties.put("mail.smtp.starttls.enable", "false"); as well.It worked for this after this change.
  • R.A
    R.A over 4 years
    Pelase explain your answer.
  • Vishwa Pratap
    Vishwa Pratap about 4 years
    This does not work if change mobile date, can anyone help me in this.