Search code examples
javaemailjakarta-mail

Java mail doesn't work in the company's network


I'm developing a desktop application that accesses the company's network

Everything is working fine except for the Java Mail library

It works only when I disconnect the device from the company's network and connect it to a regular wifi, but once I connect to the company's network it doesn't send any email.

How can I solve the problem?

The email I'm using for the java mail is Gmail account.

Below is the source code for the mail class:

public class Email {

    public void sendEmail(String from, String recipent, String title, String name, String textmsg, String emp_id) throws IOException {

        final String user = "******@gmail.com";
        final String password = "*****";

        String host = "mail.javatpoint.com";
        String to = recipent;

        Properties properties = System.getProperties();
        properties.put("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user, "IT Communication Database"));

            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(title);
            message.setContent("Below are the assets that were found for the following user: " + emp_id + "\n" + textmsg, "text/html");

            Transport.send(message);
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    public void sendEmail(String from, String recipent, String title, String name, String textmsg, String search_word) throws IOException {

        final String user = "*****@gmail.com";
        final String password = "*****";

        String host = "mail.javatpoint.com";
        String to = recipent;

        Properties properties = System.getProperties();
        properties.put("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user, "IT Communication Database"));

            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(title);

            Transport.send(message);
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
}

Solution

  • Check if your company's network uses a proxy. If yes, add the below code with the proxy IP:port details:

    properties.setProperty("proxySet","true");
    properties.setProperty("socksProxyHost","(proxy IP)");
    properties.setProperty("socksProxyPort","(proxy port)");
    

    Sample below:

    properties.setProperty("proxySet","true"); 
    properties.setProperty("socksProxyHost","192.168.155.1");
    properties.setProperty("socksProxyPort","1080");