我试图通过Outlook发送邮件,但收到如下错误。
package test.first.javamail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 SendAttachmentInEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
// String to = "sagapawar1234@gmail.com";
// Sender's email ID needs to be mentioned
String from = "abcd@outlook.com";
final String password = "*****";// change accordingly
// Check how many arguments were passed in
if (args.length == 0) {
System.out.println("Please run Sendmail jar as: java -jar SendMail.jar <recivers@gmail.com> <path of attachment file>");
System.exit(0);
}
String to = args[0];
String attachment_path = args[1];
final String username = from;
;// change accordingly
System.out.println("Trying to send mail to : " + to + "\n");
// final String username = "abcd";// change accordingly
// final String password = "******";// change accordingly
System.out.println("Please wait for a moment, we are checking creadentials...! \n");
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.smtp.socketFactory.fallback", "true");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
System.out.println("Seat tight your identity has been proved and mail is just on the way...! ");
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = attachment_path;// "C:/Users/swapnil.kotwal/Desktop/buildsuccess.txt";Azure.png
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
错误是
Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:104)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:99)
Caused by: java.net.ConnectException: Connection timed out: 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:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
我相信代码是正确的,但不确定究竟是什么阻止邮件传递?任何帮助将不胜感激。
答案 0 :(得分:0)
基于
r = new BufferedReader(new FileReader(System.getProperty("user.home") + "/Desktop/ip.txt"));
client = new Socket(r.readLine(),8880);
我认为你需要检查一些事情
client = new Socket("192.168.1.12",8880);
端口号,请与您的IT团队核实该端口是否已打开以与给定的SMTP通信
服务器如果您获得了适当的开放端口,则该问题可能会得到解决。
基本上处理此属性
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
如果您的组织有任何与代理相关的设置,您也可以参考此答案https://stackoverflow.com/a/12097764/1665592