私はJavamailで動作する通常のJavaアプリケーションを持っています。私がWebアプリケーション()から実行している場合、特にTomcat(AND Jetty)のVAADINを使用すると、私はいつもjava.netを取得します。 SocketException:ネットワークに到達できません:connectEclipse、Javamail、Tomcat、およびソケット到達不能/ネットワーク到達不能?
私はMSExchangeサーバーにpingできます。そして、通常のプログラムが動作します。
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 protected]", "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("[email protected]"));
// 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とWebアプリケーションのコードを貼り付けまったく同じコピーです。
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, "[email protected]", "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を探して、Tomcat Libフォルダに、そして私のクラスパスにJavamail jarを追加しました。私はまだ接続できませんエラーを取得します。サーバー上で実行>として実行
私は右クリック>、私は、Tomcatは、システムアカウントで実行されていたかどうかを確認しようとしたが、私はタスクマネージャで確認したとき、それはここの下で私のユーザー名を持っていた:
これは、ネットワークにアクセスできるという意味ですか?または何かがまだブロックされていますか?あるいは、Tomcat専用のプロキシ設定を追加する必要がありますか?
交換サーバーのポート25からtelnetで接続できますか? –
私はそれを行う方法がわかりませんが、おそらくネットワーク管理者の許可がありません。私は自分のコンピュータから交換サーバーにアクセスできることを知っています。最初の例では、ホスト名とポートは完全に機能します。もう1つは接続できません。私は、Eclipse内の同じコンピュータから両方のプログラムを実行しています。 – arsarc
明らかにするには、両方のプログラムがデスクトップ/ラップトップコンピュータ、Eclipseで実行されていて、1つは動作しますが、もう1つは失敗しますか?そして同じコンピュータから[telnetを使ってExchangeサーバーに接続する](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug)できますか? telnetが動作し、スタンドアロンプログラムが動作し、Tomcatだけが失敗する場合は、ファイアウォールとアンチウィルスの設定を確認してください。 –