JavaMail Use (check whether the mailbox can receive and send parts)

Source: Internet
Author: User

Using JavaMail we all know that by javamail This tool, we can implement the mail transceiver function in our own system. In this article we will show you how to detect if the mailboxes we set are able to receive and send messages.

First, if you want to implement the message acceptance and delivery function, you need to open the POP3 and SMTP protocol in your mailbox. In general, the POP3 protocol is used for the acceptance of messages, and the SMTP protocol is used for sending messages. We take QQ mailbox as an example, if you need to use a QQ mailbox in your system to accept and send mail for you, first you need to enter the QQ Mailbox Settings page, open POP3 and SMTP service.

After the SMTP and POP3 services are turned on, you need to obtain an authorization code that needs to be used instead of the login password when receiving and sending messages. After everything is set up, let's see how you can use Java mail to check to see if the mailbox information you have set up is ready to receive and send.

1. Inspection of receiving parts

The JavaMail API defines a Java.mail.Store class that is used to perform mail acceptance tasks that encapsulate the underlying implementation details of a message acceptance protocol, and the application calls methods in this class to get information about the various mail folders in the user's mailbox. JavaMail uses a Folder object to represent the mail folder, which in turn obtains all the message information in the Mail folder through the method application of the Folder object, each of which is encapsulated with a message object separately JavaMail. The collection process is as follows:

Obtain a store object that implements a mail delivery protocol from the Session object

Connect the POP3 server as a mailbox account

Call the store's GetFolder method to obtain a Folder object that represents a message folder in the mailbox for that account.

Call the Getmessages or GetMessage method in the Folder object to get all the messages in the mail folder or a single message, each with a message object returned

Then if we need to check whether the mailboxes we set up can be collected, we just need to call the store's IsConnected method, which is used to detect if the client is connected to the mail server. The method returns true to indicate that it is connected, and returns false indicating that it is not connected. The implementation method is as follows:

public static Boolean checkreceive () {
    string host = "pop.qq.com";//QQ Mailbox For example
    String username = "Your mailbox";
    String Password = "Authorization Code";
    String port = "Service port number, QQ mailbox is 995";

    Boolean result = false;

    Properties P = new properties ();
    P.setproperty ("Mail.pop3.host", host); Change
    p.setproperty ("Mail.pop3.port", port) as needed;
    SSL secure connection parameter
    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. Inspection of the hair parts

JavaMail The API defines a Java.mail.Transport class that is designed to perform mail delivery services, which encapsulate the low-level implementation details of a mail delivery protocol that the application calls to send message data encapsulated by messages to the specified SMTP server. The process of sending a message using JavaMail is as follows:

Obtain a transport object that implements a mail delivery protocol from the Session object

Use the Session object to create a message object and invoke the method of the messages object to encapsulate mail data.

Connects to the specified SMTP server, calling the message sending method in the transport object to send the message data encapsulated in the Messages object.

Then we need to check whether we set the mailbox information is correct, but only to detect whether we can successfully connect to the SMTP server. 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 port");
    Prop.setproperty ("Mail.transport.protocol", "SMTP");
    Prop.setproperty ("Mail.smtp.auth", "true");

    Set timeout time to 20 seconds Prop.setproperty ("Mail.smtp.timeout", "20000");
           If ("SSL encrypted port") {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, to create session sessions session = Session.getinstance (prop);
    Turn on debug mode for session so that you can view the running status of the program sending email session.setdebug (true);
    2, through the session to get transport object transport TS;
    3, use the mailbox username and password to connect to the mail server, send a message, the sender needs to submit the user name and password of the mailbox to the SMTP server, the user name and password are authenticated before they can normally send the message to the recipient.
        try {ts = Session.gettransport (); Ts.connect ("smtp.qq.cOm "," Your Mailbox "," Mailbox Authorization Code ");
        Ts.close ();
    result = true;
    catch (nosuchproviderexception e1) {result = false;
    catch (Messagingexception e) {result = false;
return result; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.