Eclipse,Javamail,Tomcat和socket无法访问/网络无法访问?

时间:2016-10-26 15:25:39

标签: java tomcat javamail vaadin

我有一个适用于Javamail的常规Java应用程序。就像在,如果我只是在Main(String [] args)中运行它,它会工作,但如果我从webapp运行它,特别是VAADIN与Tomcat(AND Jetty),我总是得到java.net.SocketException:网络无法访问:connect

我可以ping MSExchange服务器。正常计划有效。

在eclipse中,我尝试通过更改server.xml和web.xml设置来关注this guide,但在添加了所有更改之后,我仍然会收到相同的错误。

这是在Eclipse中运行的Java应用程序,它将使用我们拥有的MSExchange服务器发送电子邮件。我需要添加特定端口吗?我试图强制Tomcat使用IPV4 by adding 0.0.0.0 to all my connectors但是没有做任何事情。

import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Session;

import java.text.SimpleDateFormat;

public class SendEmail {
    public static void main(String[] args) {
          //Creates a connection with the Exchange Server.
        String smtpHostServer = "MSExchangeServerName";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHostServer);
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.socketFactory.port", "25");
        props.put("java.net.preferIPv4Stack","True");
        Session session = Session.getInstance(props, null);

        String todayStr = new SimpleDateFormat("MM-dd-yyyy").format(new Date());

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 14);
        Date d = c.getTime();
        String dateStr = new SimpleDateFormat("MM/dd/yyyy").format(d);

        SendEmailUtility.sendEmail(session, "email@host.com", "Test <b>Email</b>");

这是SendEmailUtility:

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmailUtility {
    public static void sendEmail(Session session, String toEmail, String subject, String body){   
        try        
        {          
            //Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress("blah@test.com"));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));

         // Set Subject: header field
         message.setSubject(subject);

         // This mail has 2 part, the BODY and the embedded image
         MimeMultipart multipart = new MimeMultipart("related");

         // first part (the html)
         BodyPart messageBodyPart = new MimeBodyPart();
         String htmlText = "<img src=\"cid:image\"><p>"+body;
         messageBodyPart.setContent(htmlText, "text/html");
         // add it
         multipart.addBodyPart(messageBodyPart);

         // second part (the image)
         messageBodyPart = new MimeBodyPart();
         String fdsImg;
        fdsImg = "c:\download.jpg";

         DataSource fds = new FileDataSource(fdsImg);

         messageBodyPart.setDataHandler(new DataHandler(fds));
         messageBodyPart.setHeader("Content-ID", "<image>");

         // add image to the multipart
         multipart.addBodyPart(messageBodyPart);

         // put everything together
         message.setContent(multipart);
         // Send message
         Transport.send(message); //ERROR HAPPENS HERE ON TOMCAT
        }
        catch (Exception e) {          
            e.printStackTrace();        
        }    
    }
}

这与webapp中完全相同的复制粘贴代码与上面完全相同的EmailUtils,只是这个版本不起作用。

btnSendEmail.addClickListener(new ClickListener(){

        @Override
        public void buttonClick(ClickEvent event) {
            try {
                String smtpHostServer = "MSExchangeServerName";

                Properties props = System.getProperties();
                props.put("mail.smtp.host", smtpHostServer);
                props.put("mail.smtp.auth", "false");
                props.put("mail.smtp.socketFactory.port", "25");
                props.put("java.net.preferIPv4Stack","True");
                Session session = Session.getInstance(props, null);

                String todayStr = new SimpleDateFormat("MM-dd-yyyy").format(new Date());

                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH, 14);
                Date d = c.getTime();
                String dateStr = new SimpleDateFormat("MM/dd/yyyy").format(d);

                SendEmailUtility.sendEmail(session, "blah@test.com", "test <b>email");


            } catch (Exception e) {
                e.printStackTrace();
                Notification.show("Error sending the email", Notification.Type.ERROR_MESSAGE);
    }
        }

       });


       layout.addComponent(btnSendEmail);

我的Stacktrace:

javax.mail.MessagingException: Could not connect to SMTP host: MSExchangeName, port: 25;
  nested exception is:
    java.net.SocketException: Network is unreachable: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.test.EmailUtils.sendEmail(EmailUtils.java:57)

我还有其他选择或者我可能没有正确完成吗?在黑暗中,我尝试查找eclipse,javamail,tomcat和我got this question,并将Javamail jar添加到我的Tomcat Lib文件夹以及我的类路径中。我仍然得到无法连接错误。

当我右键单击&gt;运行为&gt;在服务器上运行,我试图查看Tomcat是否在系统帐户上运行,但是当我在任务管理器中检查时,它的用户名在此处:

enter image description here

这是否意味着它可以访问网络?或者某些东西仍被封锁?或者我需要专门为Tomcat添加代理设置?

1 个答案:

答案 0 :(得分:0)

听起来像防火墙或防病毒问题。 JavaMail FAQ有connection debugging tips