0

这是我到目前为止所拥有的。我正在尝试在 Outlook 中发送电子邮件并将该功能嵌入到我的应用程序中。

String to = "me@mine.com";
String from = "you@yours";
String host = "casarray.ex2.specificallygeneral.net";
final String username = "user@doamin.com";
final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.user", "user@domain.com");
    props.put("mail.smtp.password", "password");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "true");

    Authenticator authenticator = new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(username, password);
      }
    };

    Session session = Session.getDefaultInstance(props, authenticator);      

    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject("Test");
        msg.setSentDate(new Date());
        msg.setText("This API works");
        Transport transport = session.getTransport();
        transport.connect(username, password);
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
        Transport.send(msg);
        transport.sendMessage(msg, address);
        if (transport == null) {

            JOptionPane.showMessageDialog(rootPane, "Mail carrier was shot. Sorry.");
        }

        }   
    catch (MessagingException ex) {
            Logger.getLogger(AAIApplication.class.getName()).log(Level.SEVERE, null, ex);
        }

这是我每次得到的:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: casarray.ex2.secureserver.net, 25; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:345) at javax.mail.Service.connect(Service.java:226) at javax.mail.Service.connect(Service.java:246) at AAIworkordertracker.AAIApplication.addToTrackerbtnActionPerformed(AAIApplication.java:412) at AAIworkordertracker.AAIApplication.access$1200(AAIApplication.java:27) at AAIworkordertracker.AAIApplication$13.actionPerformed(AAIApplication.java:171) at java.awt.Button.processActionEvent(Button.java:409) at java.awt.Button.processEvent(Button.java:377) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927) ... 29 more

如果有人可以帮我解决这个问题,让我知道我做错了什么。它对我来说看起来很完美,所以我假设问题出在 Exchange 服务器上。你怎么看?

谢谢!

4

1 回答 1

0

“无法连接到主机、端口”可能是由错误的主机名、错误的端口、阻塞的防火墙(在服务器上、网关上,甚至在您自己的机器上)、网络故障、服务器停机等引起的。您可能想要找出是否可以从命令行使用 telnet 连接/“交谈”到邮件服务器。另请参阅http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug

注意:在 Windows 7 上,您可能需要先安装 telnet。请参阅http://support.microsoft.com/kb/978779

于 2013-09-29T06:36:50.270 回答