简体   繁体   中英

How do I get email error code in javax.mail

Now I am using these dependencies. compile group: 'javax.mail', name: 'mail', version: '1.4' testCompile group: 'javax.mail', name: 'mail', version: '1.4'

I would like to get error code (like error 5.1.1) when email is sent failed. Could I get this error code from MessagingException? And how to get it?

import javax.mail.*;
import org.springframework.stereotype.Component;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

@Component
public class SendEmailClient
{
    /**
     * send email to receiver.
     */
    public void send(String _sender, String _receiver, String _subject, String _subjectCharset, String _body, String _bodyContentType, String _bodyCharset)
    {
        MimeMessage message = new MimeMessage(getSmtpSession());
        Multipart multipartMessage = new MimeMultipart();
        MimeBodyPart bodyPart = new MimeBodyPart();

        try {
            message.setFrom(new InternetAddress(_sender));
            message.setRecipients(Message.RecipientType.TO, _receiver);
            message.setSubject(_subject,_subjectCharset);
            bodyPart.setContent(_body, "text/" +_bodyContentType + "; charset=" + _bodyCharset);
            multipartMessage.addBodyPart(bodyPart);
            message.setContent(multipartMessage);
            Transport.send(message);

        } catch (MessagingException _e) {

        }
    }

    /**
     * Get smtp session.
     * @return
     */
    private Session getSmtpSession()
    {
        final String username = "email";
        final String password = "password";

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

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

You should catch the SMTPSendFailedException javax.mail returns when an error happens. This exception offers the call to the method getReturnCode() :

public int getReturnCode()

Return the return code from the SMTP server that indicates the reason for the failure. See RFC 821 for interpretation of the return code.

Looking at RFC821 , sections 4.2.1 and 4.2.2, you'll find the definition of the error codes. So, in your program, you should have something like:

try
{
   //connect,...

}
catch(SMTPSendFailedException e) 
{
    int errorCode = e.getReturnCode(); //f.e: 401
    //check what error message the code relates, print it, etc..
}

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