Total Pageviews

2016/08/03

[Java Mail] How to check Exchange Server status?

Problem
If I would like to write a Java code to check the status of SMTP server, how to do it?

How-to
The sample code is as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package albert.practice.mail;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;

import lombok.extern.slf4j.Slf4j;

import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * Used to check the status of exchange server .
 */
@Slf4j
public class CheckSmtpStatus {

    /**
     * The main method.
     * 
     * @param args
     *            the arguments
     * @throws MessagingException
     *             the messaging exception
     */
    public static void main(String[] args) throws MessagingException {
        String host = "smtp host name";
        int port = 25;
        String emailU = "username";
        String emailP = "password";

        CheckSmtpStatus checkSmtpStatus = new CheckSmtpStatus();
        boolean isConnected = checkSmtpStatus.isConnected(host, port, emailU, emailP);
        log.debug("isConnected = " + isConnected);
    }

    /**
     * Checks if is connected.
     * 
     * @param host
     *            the host
     * @param port
     *            the port
     * @param emailU
     *            the email u
     * @param emailP
     *            the email p
     * @return true, if is connected
     * @throws MessagingException
     *             the messaging exception
     */
    public boolean isConnected(String host, int port, String emailU, String emailP)
            throws MessagingException {
        boolean isConnected = false;
        Transport transport = null;
        Session session = createJavaMailSender(host, port, emailU, emailP);
        try {
            transport = session.getTransport("smtp");
            transport.connect(host, port, emailU, emailP);

            // used to check this SMTP service is currently connected or not
            isConnected = transport.isConnected();
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } finally {
            transport.close();
        }
        return isConnected;
    }

    private Session createJavaMailSender(String host, int port, String emailU, String emailP) {
        Properties properties = new Properties();
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.trust", "*");

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setJavaMailProperties(properties);
        javaMailSender.setHost(host);
        javaMailSender.setPort(port);
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setUsername(emailU);
        javaMailSender.setPassword(emailP);

        return javaMailSender.getSession();
    }

}




No comments: