使用来自公司网络的{j}邮件发送邮件(拒绝连接)的问题

时间:2017-01-29 06:46:27

标签: java swing javamail

我已经写了一段用于发送邮件的代码(smtp,tls enabled)。当我的m / c(运行此代码)连接到任何商业网络时,它工作正常。但是,当我在办公室(公司网络)时,我在执行相同代码时收到以下错误:

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

提前致谢!!

2 个答案:

答案 0 :(得分:0)

您可能需要配置Java邮件以通过代理服务器进行连接。

请注意,Java邮件不支持HTTP代理,因此您必须将其配置为使用SOCKS代理。

如果您的公司网络没有启用SOCKS代理,那么可以选择其他方式:

  • 请网络管理员从您的服务器打开传出端口。
  • 通过在公司网络上通过HTTP代理进行代理的服务器上本地运行SOCKS代理来打败HTTP代理。 'cntlm'有这种能力。请注意,这可能会违反某些网络政策。

另见这个答案: https://stackoverflow.com/a/12097764/3216618

答案 1 :(得分:0)

就我而言,Avast Anti-Virus不允许我连接。这些类型的软件可能是导致这些类型错误的原因。