错误:
异常无法连接到主机,端口www.example.com,587超时-1嵌套异常是java. net。smtp.gmail.com连接 Connection refused: connect
我的源代码是:
package poidemo;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Test_MailTrigger {
public static void main(String [] args) {
String to = "xxxx@gmail.com";//change accordingly
String from = "yyyy@gmail.com";//change accordingly
final String username = "yyyy@gmail.com";//change accordingly
final String password = "*****";//change accordingly
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", 587);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Testing Subject");
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
System.out.println(e);
}
}
}
1条答案
按热度按时间0kjbasz61#
尝试其他端口:465并添加更多 prop