简体   繁体   中英

How to resolve com.sun.mail.smtp.SMTPSendFailedException: 554 Email rejected

I am writing an email client to send email using yahoo SMTP server. Getting error: com.sun.mail.smtp.SMTPSendFailedException: 554 Email rejected Passed Authentication but throwing 554 while sending email:-

   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");

    }

Output:-

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)

What is the problem here.

Here is class EmailUtil: public class 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();
        }
    }

} This client sends email using gmail SMTP server.

If you read here https://serversmtp.com/port-outgoing-mail-server/ and also here https://getmailspring.com/setup/access-yahoo-com-via-imap-smtp

You will notice that yahoo uses port 465 and in your code i see 587

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