发送未经身份验证的Java电子邮件/没有发件人密码

时间:2018-09-03 05:27:44

标签: java email javamail

我正在尝试发送java电子邮件,但没有发件人密码。您可以在下面看到代码。我将mail.smtp.authtrue更改为false。有一个覆盖方法,其名称为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);

如何在没有密码身份验证的情况下发送电子邮件?谢谢。

1 个答案:

答案 0 :(得分:2)

通常,您需要向邮件服务器进行身份验证。如果您运行自己的邮件服务器,则可以对其进行设置,使其不需要身份验证,但是出于我的明显原因,所有公共电子邮件服务供应商都不允许这样做。