gpt4 book ai didi

java - Eclipse、Javamail、Tomcat 和套接字无法访问/网络无法访问?

转载 作者:行者123 更新时间:2023-11-28 23:23:16 24 4
gpt4 key购买 nike

我有一个使用 Javamail 的常规 Java 应用程序。比如,如果我只是在 Main(String[] args) 中运行它,它会起作用,但如果我从 Web 应用程序运行它,特别是带有 Tomcat(和 Jetty)的 VAADIN,我总是得到java.net.SocketException:网络无法访问:连接

我可以 ping 通 MSExchange 服务器。常规程序有效。

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

这是在 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();
}
}
}

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

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);

我的堆栈跟踪:

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 文件夹以及我的类路径中。我仍然收到无法连接错误。

当我右键单击 > 运行方式 > 在服务器上运行时,我试图查看 Tomcat 是否在系统帐户上运行,但是当我检查任务管理器时,它在此处显示了我的用户名:

enter image description here

这是否意味着它可以访问网络?或者什么东西仍然被阻止?或者我需要专门为 Tomcat 添加代理设置?

最佳答案

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

关于java - Eclipse、Javamail、Tomcat 和套接字无法访问/网络无法访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com