Java邮件在公司网络中无法使用

标签 java email jakarta-mail

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

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

最佳答案

检查您公司的网络是否使用代理。如果是,请添加以下代码和代理IP:端口详细信息:

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");

关于Java邮件在公司网络中无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26077297/

相关文章:

java - 井号在使用 java.mail 包的邮件内容中不起作用?

java - Eclipse 文本光标已更改,编辑器的行为有所不同

java - 将 ISBN10 转换为 ISBN13

java - 在 for 循环期间从列表中删除对象

java - 类型不匹配 : cannot convert from double to Double

c# MailMessage : Send mail using default email account 收藏

php - 为什么我必须在 mail() 函数中使用 "-f from@email.com"

java - mailto URI 在 Java.Desktop 和 Windows/MS outlook 之间被截断

Javax.mail 或 java email - 如何在不直接发送的情况下打开电子邮件

email - 如何在JAVA中从ckeditor生成的html中提取纯文本