java 如何通过本地主机从 JavaMail 发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Send Mail from JavaMail via Localhost
提问by jl.
I am creating a form, which will send out the details via email upon user completes his details and click submit.
我正在创建一个表单,该表单将在用户完成其详细信息并单击提交后通过电子邮件发送详细信息。
Mail Submission with JavaMail:
使用 JavaMail 提交邮件:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String host = "localhost";
String from = "[email protected]";
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Transport transport = session.getTransport("smtp");
MimeMessage message = new MimeMessage(session);
Address fromAddress = new InternetAddress("[email protected]");
message.setFrom(fromAddress);
InternetAddress to = new InternetAddress("[email protected]");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Email Details Sending");
message.setText("This is my testing content.");
transport.connect(host, from);
message.saveChanges();
Transport.send(message);
transport.close();
} finally {
out.close();
}
}
I am using Email aliases for [email protected] which means I could have 4 email aliases from sendToAliases. However, I am unable to reach any emails upon deploying and running the above mail file. Can anyone please advise me?
我正在使用 [email protected] 的电子邮件别名,这意味着我可以从 sendToAliases 获得 4 个电子邮件别名。但是,在部署和运行上述邮件文件后,我无法收到任何电子邮件。任何人都可以给我建议吗?
Thank you.
谢谢你。
回答by Juha Syrj?l?
- Have you checked the log files?
- Do you get any exceptions or errors when running the program?
- Do you have a SMTPserver running in localhost?
- Is the SMTP server accepting connections from localhost?
- Can you send emails via that server using normal email client and receive them somehow?
- Try to make your program a standalone commandline program and try to execute it
- 你检查过日志文件吗?
- 运行程序时是否有任何异常或错误?
- 您是否有在本地主机中运行的SMTP服务器?
- SMTP 服务器是否接受来自本地主机的连接?
- 您可以使用普通电子邮件客户端通过该服务器发送电子邮件并以某种方式接收它们吗?
- 尝试使您的程序成为独立的命令行程序并尝试执行它
You seem to have a missing quote in message.setSubject("Email Details Sending);. Are you sure that your servlet actually compiles?
您似乎在message.setSubject("Email Details Sending);. 你确定你的 servlet 真的能编译吗?

