如何解决 com.sun.mail.smtp.SMTPSendFailedException: 554 电子邮件被拒绝

时间:2021-01-15 17:39:32

标签: java email smtp client

我正在编写电子邮件客户端以使用雅虎 SMTP 服务器发送电子邮件。 获取错误:com.sun.mail.smtp.SMTPSendFailedException:554 电子邮件被拒绝 通过身份验证但在发送电子邮件时抛出 554:-

   public static void main(String[] args) {
            final String fromEmail = "myyahoo@yahoo.com"; //requires valid id
            final String password = "xxxxxxx"; // correct password for gmail id
            final String toEmail = "test.existing@gmail.com"; // can be any email id

            System.out.println("TLSEmail Start");
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.mail.yahoo.com"); //SMTP Host
            props.put("mail.smtp.port", "587"); //TLS Port
            props.put("mail.smtp.auth", "Required"); //enable authentication
            props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS

            //create Authenticator object to pass in Session.getInstance argument
                    System.out.println("Calling getPasswordAuthentication");
            Authenticator auth = new Authenticator() {
                    //override the getPasswordAuthentication method
                    protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(fromEmail, password);
                    }
            };
                    System.out.println("After getPasswordAuthentication");
            Session session = Session.getInstance(props, auth);

            EmailUtil.sendEmail(session, toEmail,"My First TLSEmail Testing Subject", "My first email client. TLSEmail Testing Body");

    }

输出:-

TLSEmail Start
Calling getPasswordAuthentication
After getPasswordAuthentication
Message is ready
com.sun.mail.smtp.SMTPSendFailedException: 554 Email rejected
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2358)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1823)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1300)
    at javax.mail.Transport.send0(Transport.java:255)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailUtil.sendEmail(EmailUtil.java:48)
    at MyEmail.main(MyEmail.java:38)

这里有什么问题。

这是EmailUtil类: 公共类 EmailUtil {

    public static void sendEmail(Session session, String toEmail, String subject, String body){
            try
        {
          MimeMessage msg = new MimeMessage(session);
          msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
          msg.addHeader("format", "flowed");
          msg.addHeader("Content-Transfer-Encoding", "8bit");

          msg.setFrom(new InternetAddress("no_reply@example.com", "NoReply-JD"));
          msg.setReplyTo(InternetAddress.parse("no_reply@example.com", false));

          msg.setSubject(subject, "UTF-8");
          msg.setText(body, "UTF-8");
          msg.setSentDate(new Date());
          msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
          System.out.println("Message is ready");
      Transport.send(msg);

          System.out.println("EMail Sent Successfully!!");
        }
        catch (Exception e) {
          e.printStackTrace();
        }
    }

} 此客户端使用 gmail SMTP 服务器发送电子邮件。

0 个答案:

没有答案