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?
The simplest way to send a message is to use QuickSend method of Smtp class (this method is static, it does not require you to create an instance of Smtp class). QuickSend method allows you to send e-mails out even if you do not have an SMTP relay server.
SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.
smtp. starttls. enable, to "true". When set, if the server supports the STARTTLS command, it will be used after making the connection and before sending any login information.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With