2014-09-27 28 views
0

我开发访问公司的网络Java邮件不会在公司的网络中工作

一切工作除了Java邮件库

当我拔下它仅罚款的桌面应用程序设备从公司的网络连接到一个普通的无线网络,但是一旦我连接到公司的网络,它就不会发送任何电子邮件。

我该如何解决问题?

我用于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(); 
     } 
    } 
} 
+0

那么,您正在使用无法从您的公司网络访问的smtp服务器(smtp.gmail.com)。要么让您的公司使其可访问,要么使用另一台可从网络访问的smtp服务器。 – 2014-09-27 17:33:05

+0

哦,我会检​​查他们!感谢您的快速回复 – Wesamz 2014-09-27 17:40:11

+0

请问除smtp之外是否还有其他选项可能会起作用? @JB Nizet – Wesamz 2014-09-27 17:43:15

回答

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

非常感谢!我会问他们的 – Wesamz 2014-09-27 18:14:57

0

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

+0

感谢您的回复 – Wesamz 2014-09-27 17:40:33

0

通常的方式来处理,这是使用贵公司的电子邮件服务器,而不是使用Gmail。

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

相关问题