Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发送未经身份验证的电子邮件_Java_Email_Authentication_Jakarta Mail - Fatal编程技术网

Java 发送未经身份验证的电子邮件

Java 发送未经身份验证的电子邮件,java,email,authentication,jakarta-mail,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", "

我正在尝试开发一个代码,允许在没有身份验证的情况下使用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);

Gmail特别不支持未经授权的邮件发送。事实上,大多数公共SMTP服务器不再使用,因为这会导致太多的滥用

您可能可以找到不需要身份验证的公共smtp,但这种情况越来越少。您可以始终运行自己的smtp或简单地…添加身份验证


特别是如果你使用的是gmail,你可以简单地传递你的凭证?

这是肯定的,2014年不会使用公共邮件服务。允许在未经身份验证的情况下发送邮件的SMTP服务器称为开放中继和垃圾邮件源。您需要密码进行身份验证,但我正在寻找在未经身份验证的情况下发送电子邮件。我正在开发一个联系表单,客户可以发送一封电子邮件而不必提供密码,因为他无法使用自己的电子邮件地址发送电子邮件;你可以从你的应用程序可以访问的其他帐户发送此类邮件。这不是一个好的答案,因为它没有解释,也没有回答问题。