Java邮件在公司的网络中不起作用

时间:2014-09-27 17:28:40

标签: java email javamail

我正在开发一个访问公司网络的桌面应用程序

除Java Mail库外,一切正常。

仅当我将设备与公司网络断开连接并将其连接到常规wifi时,它才有效,但是一旦我连接到公司的网络,它就不会发送任何电子邮件。

我该如何解决这个问题?

我用于java邮件的电子邮件是Gmail帐户。

以下是邮件类的源代码:

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();
        }
    }
}

3 个答案:

答案 0 :(得分:3)

检查您公司的网络是否使用代理。如果是,请使用代理IP:port details添加以下代码:

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

以下示例:

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

答案 1 :(得分:0)

您的公司阻止防火墙上的端口25以防止垃圾邮件。

答案 2 :(得分:0)

解决此问题的常用方法是使用您公司的电子邮件服务器而不是Gmail。

如果您需要使用代理服务器进入公司网络,请参阅this JavaMail FAQ entry