我正在使用下面的代碼從使用java的Outlook發送電子郵件。但是得到錯誤。如何使用java api使用Outlook發送電子郵件
CODE:
public static void mail(){
// TODO Auto-generated method stub
//String host="POKCPEX07.corp.absc.local";
String host="POKCPEX07.corp.absc.local";
final String user="[email protected]";
String to="[email protected]";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.port", "587");
Session session=Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","******");
}
});
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.saveChanges();
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Test mail");
message.setText("This is test mail.");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
}
catch (MessagingException e) {e.printStackTrace();}
}
}
錯誤:
javax.mail.MessagingException: Could not connect to SMTP host: POKCPEX07.corp.absc.local, port: 587; nested exception is: java.net.SocketException: Permission denied: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322) at javax.mail.Service.connect(Service.java:258) at javax.mail.Service.connect(Service.java:137) at javax.mail.Service.connect(Service.java:86) at javax.mail.Transport.send0(Transport.java:150) at javax.mail.Transport.send(Transport.java:80) at TestEmail.mail(TestEmail.java:50) at TestEmail.main(TestEmail.java:16)
您的電子郵件服務器是否配置爲通過SMTP協議連接到它? – prabodhprakash
我不認爲它已配置。要做到這一點,將需要做些什麼。 但即使使用Gmail,我也無法發送。獲取「權限被拒絕」的錯誤。 –