我已經通過其他許多帖子搜索與類似問題的用戶,但我似乎無法讓它工作。用JavaMail發送電子郵件到我的作品的電子郵件服務器
package testingstuff;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import javax.activation.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
final String user = "my work ID";
final String pw = "my work Password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "server I got from our outlook app");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pw);
}
});
session.setDebug(true);
try {
Message msg = new MimeMessage(session);
String from = "[email protected]";
String to = "[email protected]";
msg.setFrom(new InternetAddress(from));
msg.setRecipient(RecipientType.TO, new InternetAddress(to));
msg.setSubject("TEST");
msg.setText("TESTING");
//Transport trans = session.getTransport("smtp");
//trans.connect("smtp-mail.outlook.com", 25, user, pw);
Transport.send(msg);
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("done");
}
}
我用java 1.8,JavaMail的報告說,它是版本1.4.7
這裏從會話調試輸出:
Hello World
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "[my work email server]", port 587, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: [my work email server], port: 587;
嵌套的例外是:
java.net.ConnectException: Connection timed out: connect
完成
我使用的電子郵件服務器來自Outlook應用程序。我進入了帳戶設置,選擇了我的帳戶並選擇了更改。這將打開一個包含電子郵件配置的窗口,其中包含服務器地址。
我試過之前,它也沒有工作。我從工作網絡發送它,所以它被阻止。我建議通過電子郵件發送到私人Gmail或其他東西,看看是否可行 –
也許端口號可能會導致問題。例如嘗試「80」。 – pilkington