Java发送电子邮件
我尝试以任何方式发送电子邮件,但无济于事。我得到这个例外。 还有其他方式发送电子邮件还是这是唯一的方式? 我可以通过 spring 发送,但我需要通过 Java 使用 Ant 发送电子邮件。
package javaapplication5;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmailTLS {
public static void main(String[] args) {
final String username = "[email protected]";
final String password = "otbbxgrztdzzinxb";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("[email protected], [email protected]")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear Mail Crawler,"
+ "\n\n Please do not spam my email!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
错误:
I am trying to send an email in any way, but to no avail. I get this exeption.
Is there any other way to send an email or is this the only one?
I can send via spring, but I need to send an email via Java with Ant.
package javaapplication5;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmailTLS {
public static void main(String[] args) {
final String username = "[email protected]";
final String password = "otbbxgrztdzzinxb";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("[email protected], [email protected]")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear Mail Crawler,"
+ "\n\n Please do not spam my email!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Error:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论