java - 发送电子邮件无需身份验证

标签 java email authentication jakarta-mail

我正在尝试开发一个代码,允许使用 javamail 发送邮件而无需身份验证。

        Properties properties = System.getProperties();
        properties.put("mail.smtp.auth", "false");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", 587);
        Session session = Session.getInstance(properties);
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xxxxxx@hotmail.com"));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("yyyyyyy@gmail.com"));
            message.setSubject("This is the Subject Line!");
            message.setText("This is actual message");
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }

但是当我执行它时,我得到了这个异常

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.

是否可以不认证就发送邮件?我错过了什么吗?

最佳答案

试试这个

    Properties props = new Properties();
    props.put("mail.smtp.host", mailHost);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    session.setDebug(true);

    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress(fromAddress));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
    message.setSubject(subject);
    message.setContent(body, "text/html; charset=utf-8");
    message.setText(body);
    Transport.send(message);

关于java - 发送电子邮件无需身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378844/

相关文章:

swift - 用户已经登录并使用api将数据保存到核心数据中

使用 passport-facebook 的 Facebook OAuth 安全性

java - 液碱 : link main yaml changelog from test

html - 电子邮件模板管理器工具

php - 如何使用 PHP 向用户发送每日电子邮件通知?

php - 使用 Gmail 进行 SMTP 时,可以设置不同的 "from"地址吗?

security - 安全网络登录示例/教程

java - 反馈电子邮件android中的设备信息

java - OCSP 检查 Java 安全套接字

java - 当工作区与帐户上的默认设置不同时,使用 Java Rally Rest API 插入测试用例结果失败