私はメールを送信するためのコードを書いています(smtp、tls enabled)。私のm/c(このコードを実行している)が商用ネットワークに接続されているとうまく動作します。私はオフィス(企業ネットワーク)にいるときしかし、同じコードを実行しながら、私は次のエラーが表示されます企業ネットワークからのJavaメールを使用したメールの送信(接続拒否)
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 = { "[email protected]","[email protected]" };
Address addRess = new InternetAddress("[email protected]");
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 = "[email protected]";
String fromUserEmailPassword = "([email protected])";
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.");
}
}
事前に感謝します!
が正しいSMTPホストとポートのためのネットワーク管理者にお問い合わせください。 – mhshimul
ネットワーク管理者にヘルプを依頼してください。 Stack Overflowコミュニティに、あなたのネットワークがどのように設定されているかを推測することは、少し意味がありません。 –
TLSでSSLではないのですか? – DevilsHnd