user10257972
user10257972

Reputation:

Send java email without authentication/with out sender password

I'm trying to send java email without senders password. Below you can see the code. I change mail.smtp.auth from true to false. There is a override method which method name getPasswordAuthentication():

public class SendMailUtilNineThirty implements Runnable {

    final private String SMTP_SERVER = "192.186.16.14";
    final private String SMTP_PORT = "135";
    final private String TO_EMAIL = "[email protected]";
    final private String TO_EMAIL = "[email protected]";
    final private String FROM_EMAIL = "[email protected]";
    final private String FROM_EMAIL_PASSWORD = "1qaz2wsx@";

public void sendEmail(String emailContent, String subject) {
        try {
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", SMTP_SERVER);
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.auth", "false");
            Session mailSession = Session.getInstance(props, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(FROM_EMAIL, FROM_EMAIL_PASSWORD);
                }
            });

            mailSession.setDebug(true);
            MimeMessage message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(FROM_EMAIL));
            message.addHeader("site", "thal.com");
            message.addHeader("service", "Thal Service");
            message.setSentDate(new Date());
            message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(TO_EMAIL));
            message.setSubject(subject);

How to send email without password authentication? Thanks.

Upvotes: 3

Views: 4119

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

In general, you need to authenticate to the mail server. If you run your own mail server, you may be able to set it up so that it doesn't require authentication, but none of the public email service vendors is going to allow that, for what I hope are obvious reasons.

Upvotes: 3

Related Questions