从公司网络使用java邮件发送邮件时出现问题(连接被拒绝)
我已经编写了一段用于发送邮件的代码(smtp,启用tls)。当我的m/c(运行此代码)连接到任何商业网络时,它工作正常。但是,当我在办公室(公司网络)时,我在执行相同代码时收到以下错误:从公司网络使用java邮件发送邮件时出现问题(连接被拒绝),java,swing,jakarta-mail,Java,Swing,Jakarta Mail,我已经编写了一段用于发送邮件的代码(smtp,启用tls)。当我的m/c(运行此代码)连接到任何商业网络时,它工作正常。但是,当我在办公室(公司网络)时,我在执行相同代码时收到以下错误: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587; nested exception is: java.net.ConnectException: Connection
javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1545)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at Mailer.sendEmail(Mailer.java:129)
at Mailer.main(Mailer.java:36)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
为了进一步分析,我进行了网络跟踪(wireshark),我看到服务器没有响应客户端发送的TCP SYN(这里是我的代码)。我理解,这可能是由于网络防火墙或其他网络策略执行功能造成的
你有什么想法吗?我该如何推进这项工作
代码如下:
/*
*描述:此类介绍电子邮件生成和发送逻辑。
*@version |日期:0.1 | 2017年1月29日
*/
import java.io.IOException;
导入java.util.Properties;
导入javax.activation.DataHandler;
导入javax.activation.DataSource;
导入javax.activation.FileDataSource;
导入javax.mail.Address;
导入javax.mail.BodyPart;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart;
导入java.util.concurrent.ThreadLocalRandom;
公共类邮递员
{
会议邮递会议;
公共静态void main(字符串args[])引发AddressException、MessaginException、IOException
{
Mailer javaEmail=new Mailer();
javaEmail.setMailServerProperties();
javaEmail.draftEmailMessage();
javaEmail.sendmail();
}
私有void setMailServerProperties()
{
Properties emailProperties=System.getProperties();
emailProperties.put(“mail.smtp.port”,“587”);
emailProperties.put(“mail.smtp.auth”,“true”);
emailProperties.put(“mail.debug”,“true”);
emailProperties.put(“mail.smtp.starttls.enable”、“true”);
emailProperties.put(“mail.smtp.socketFactory.port”,“587”);
emailProperties.put(“mail.smtp.socketFactory.fallback”、“false”);
mailSession=Session.getDefaultInstance(emailProperties,null);
}
私有MimeMessage draftEmailMessage()引发AddressException、MessaginException、IOException
{
字符串[]toEmails={“x”。y@dd.com","xxyy@gmail.com" };
地址=新的Internet地址(“sender@dd.com");
地址[]fromAddress={Address};
字符串emailSubject=“导入更新”;
字符串emailBody=“是否调用”;
mimessage emailMessage=新的mimessage(mailSession);
/**
*设置邮件收件人
* */
for(int i=0;i”
+ " "
+ " "
+ "",
“US-ASCII”、“html”);
multipart.addBodyPart(messageBodyPart);
messageBodyPart=新的MimeBodyPart();
messageBodyPart.attachFile(“C:\\Users\\MyPlace\\Pictures\\ww.png”);
messageBodyPart.setContentID(“”);
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(messageBodyPart);
//发送完整的消息部分
emailMessage.setContent(多部分);
回复电子邮件;
}
private void sendmail()引发AddressException、MessaginException、IOException
{
/**
*发件人的凭据
* */
字符串fromUser=”sender@dd.com";
字符串fromUserEmailPassword=“(XXXXXX 1@)”;
字符串emailHost=“smtp.office365.com”;
传输=mailSession.getTransport(“smtp”);
transport.connect(emailHost、fromUser、fromUserEmailPassword);
/*
*起草信息
* */
mimessage emailMessage=draftEmailMessage();
/**
*寄信
* */
transport.sendMessage(emailMessage,emailMessage.getAllRecipients());
transport.close();
System.out.println(“电子邮件发送成功”);
}
}
提前谢谢 您可能需要将Java mail配置为通过代理服务器进行连接 请注意,Java mail不支持HTTP代理,因此必须将其配置为使用SOCKS代理 如果您的公司网络未启用SOCKS代理,则有以下几种替代方案:
- 请网络管理员打开服务器的传出端口
- 在通过corpo上的HTTP代理进行代理的服务器上本地运行SOCKS代理,从而击败HTTP代理
import java.io.IOException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.concurrent.ThreadLocalRandom; public class Mailer { Session mailSession; public static void main(String args[]) throws AddressException, MessagingException, IOException { Mailer javaEmail = new Mailer(); javaEmail.setMailServerProperties(); javaEmail.draftEmailMessage(); javaEmail.sendEmail(); } private void setMailServerProperties() { Properties emailProperties = System.getProperties(); emailProperties.put("mail.smtp.port", "587"); emailProperties.put("mail.smtp.auth", "true"); emailProperties.put("mail.debug", "true"); emailProperties.put("mail.smtp.starttls.enable", "true"); emailProperties.put("mail.smtp.socketFactory.port", "587"); emailProperties.put("mail.smtp.socketFactory.fallback", "false"); mailSession = Session.getDefaultInstance(emailProperties, null); } private MimeMessage draftEmailMessage() throws AddressException, MessagingException, IOException { String[] toEmails = { "x.y@dd.com","xxyy@gmail.com" }; Address addRess = new InternetAddress("sender@dd.com"); Address[] fromAddress = {addRess}; String emailSubject = "Import update"; String emailBody = "whether is called"; MimeMessage emailMessage = new MimeMessage(mailSession); /** * Set the mail recipients * */ for (int i = 0; i < toEmails.length; i++) { emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i])); } emailMessage.addFrom(fromAddress); emailMessage.setSubject(emailSubject); /** * If sending HTML mail * */ // Create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("This is Will be the meessage body"); // Create a multipart message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "D:\\Tradfri.java"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Part three is the embedded body messageBodyPart = new MimeBodyPart(); String cid = (Integer.toString(ThreadLocalRandom.current().nextInt(1, 60000 + 1))); System.out.println("CID is " + cid); messageBodyPart.setText("" + "<html>" + " <body>" + " <p>Here is my image:</p>" + " <img src=\"cid:" + cid + "\" />" + " </body>" + "</html>", "US-ASCII", "html"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.attachFile("C:\\Users\\MyPlace\\Pictures\\ww.png"); messageBodyPart.setContentID("<" + cid + ">"); messageBodyPart.setDisposition(MimeBodyPart.INLINE); multipart.addBodyPart(messageBodyPart); // Send the complete message parts emailMessage.setContent(multipart); return emailMessage; } private void sendEmail() throws AddressException, MessagingException, IOException { /** * Sender's credentials * */ String fromUser = "sender@dd.com"; String fromUserEmailPassword = "(XXXXXX1@)"; String emailHost = "smtp.office365.com"; Transport transport = mailSession.getTransport("smtp"); transport.connect(emailHost, fromUser, fromUserEmailPassword); /* * Draft the message * */ MimeMessage emailMessage = draftEmailMessage(); /** * Send the mail * */ transport.sendMessage(emailMessage, emailMessage.getAllRecipients()); transport.close(); System.out.println("Email sent successfully."); } }