简体   繁体   中英

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 = "saman@thal.com";
    final private String TO_EMAIL = "dumidu@thal.com";
    final private String FROM_EMAIL = "samanchandana@thal.com";
    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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM