所以我有这段代码可以发送电子邮件:
public static void main(String[] args) {
String to = "mycoolreceiveremail@gmail.com";//change accordingly
String from = "mycoolsenderemail@gmail.com";//change accordingly
String host = "localhost";//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mycoolsenderemail@gmail.com", "mycoolsenderpassword");
}
});
//compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
问题是我设法发送了它(我想),因为打印了消息“消息发送成功...”。但是问题是我没有收到电子邮件。我检查了邮箱。原因可能是什么?