2014-09-27 59 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

相關問題