我正在开发一个访问公司网络的桌面应用程序
除了 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/