Use of JavaMail (to detect whether the mailbox can receive and send mail)

We all know that using JavaMail, through JavaMail, we can realize the function of sending and receiving mail in our own system. In this article, we will give how to detect whether the mailbox we set up can receive and send mail.

First of all, if you want to realize the function of receiving and sending emails, you need to enable POP3 and SMTP protocols in your mailbox. In general, the POP3 protocol is used for mail reception, and the SMTP protocol is used for mail sending. We take QQ mailbox as an example. If you need to use a QQ mailbox in your system to receive and send emails for you, first you need to enter the settings page of QQ mailbox and enable POP3 and SMTP services.

write picture description here

After the SMTP and POP3 services are enabled, you need to obtain an authorization code, which you need to use instead of the login password when receiving and sending emails. After everything is set up, let's see how to check whether the mailbox information you set can be received and sent through Java Mail.

1. Receipt detection

A java.mail.Store class is defined in the JavaMail API, which is used to perform mail accepting tasks. The instance object of this class encapsulates the underlying implementation details of a certain mail accepting protocol. The application calls the methods in this class to obtain the user Information about various mail folders in the mailbox. JavaMail uses the Folder object to represent the mail folder, and the application program obtains all the mail information in the mail folder through the method of the Folder object, and JavaMail uses a Message object to encapsulate each mail information. The collection process is as follows:

Obtain the Store object that implements a certain mail sending protocol from the Session object Connect to the POP3 server
as a mailbox account
Call the getFolder method of the Store to obtain the Folder object representing a mail folder in the account's mailbox.
Call the getMessages or getMessage method in the Folder object to get all the mails or a certain mail in the mail folder, each mail is returned as a Message object

Then if we need to check whether the mailbox we set can receive mail, we only need to call the isConnected method of Store, which is used to detect whether the client and the mail server are connected. The method returns true for connected, false for not connected. The specific implementation method is as follows:

public static boolean checkReceive() {
    String host = "pop.qq.com";//以QQ邮箱为例
    String username = "您的邮箱";
    String password = "授权码";
    String port = "服务端口号,QQ邮箱是995";

    boolean result = false;

    Properties p = new Properties();
    p.setProperty("mail.pop3.host", host); // 按需要更改
    p.setProperty("mail.pop3.port", port);
    // SSL安全连接参数
    p.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    p.setProperty("mail.pop3.socketFactory.fallback", "true");
    p.setProperty("mail.pop3.socketFactory.port", port);

    Session session = Session.getDefaultInstance(p, null);
    Store store;
    try {
        store = session.getStore("pop3");
        store.connect(host, username, password);

        result = store.isConnected();
    } catch (Exception e) {
        result = false;
    }

    return result;
}

2. Sending detection

A java.mail.Transport class is defined in the JavaMail API, which is specially used to execute the mail sending service. The instance object of this class encapsulates the underlying implementation details of a certain mail sending protocol. The application calls the methods in this class to transfer the Message object. The encapsulated mail data is sent to the specified SMTP server. When using JavaMail to send emails, the process is as follows:

Obtain a Transport object that implements a certain mail sending protocol from the Session object.
Use the Session object to create a Message object, and call the methods of the Message object to encapsulate the mail data.
Connect to the specified SMTP server and call the mail sending method in the Transport object to send the mail data encapsulated in the Message object.

Then we need to check whether the mailbox information we set is correct. In fact, we only need to check whether we can successfully connect to the SMTP server, so we only need to call the isConnected of Transport to check whether the connection is successful. The method returns true to indicate that it is connected, and returns false to indicate that it is not connected. connect. The specific code is as follows:

public static boolean checkSend() throws GeneralSecurityException {
    Properties prop = new Properties();
    prop.setProperty("mail.host", "smtp.qq.com");
    prop.setProperty("mail.smtp.port", "SMTP端口");
    prop.setProperty("mail.transport.protocol", "smtp");
    prop.setProperty("mail.smtp.auth", "true");
    //设置超时时间为20秒
    prop.setProperty("mail.smtp.timeout", "20000");

    if("SSL加密端口"){
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
           sf.setTrustAllHosts(true);
           prop.put("mail.smtp.ssl.enable", "true");
           prop.put("mail.smtp.ssl.socketFactory", sf);
    }

    boolean result = false;

    //1、创建session
    Session session = Session.getInstance(prop);
    //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
    session.setDebug(true);
    //2、通过session得到transport对象
    Transport ts;
    //3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
    try {
        ts = session.getTransport();
        ts.connect("smtp.qq.com", "您的邮箱", "邮箱授权码");

        result = ts.isConnected();

        ts.close();
    } catch (NoSuchProviderException e1) {
        result = false;
    } catch (MessagingException e) {
        result = false;
    }

    return result;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325717183&siteId=291194637