如何在 Linux 环境下使用 Java 发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14433908/
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 using Java in a Linux environment?
提问by Hasif Subair
I have successfully sent mail using Java using apache james server in Windows. I have been trying to do the same in Linux, but couldn't start the james server. I am a novice in Linux.
我已经在 Windows 中使用 apache james 服务器使用 Java 成功发送了邮件。我一直在尝试在 Linux 中做同样的事情,但无法启动 james 服务器。我是 Linux 新手。
Is james server really necessary for sending mails? If so how to start james server in Linux?
发送邮件真的需要詹姆斯服务器吗?如果是这样,如何在 Linux 中启动 james 服务器?
采纳答案by raffael
For sending emails in java you can use the java mail api: http://www.oracle.com/technetwork/java/javamail/index.html
See this tutorial for infos about using it: http://www.tutorialspoint.com/java/java_sending_email.htm
要在 Java 中发送电子邮件,您可以使用 Java 邮件 API:http: //www.oracle.com/technetwork/java/javamail/index.html
有关使用它的信息,请参阅本教程:http: //www.tutorialspoint.com /java/java_sending_email.htm
They are using a mail server on localhost, but i wouldn't recommend that. It is perfectly alright for testing or if your localhost is a server with proper DNS records. If you send emails from a IP where DNS reverse lookup does not work, chances are that your mail will be stopped by a spam filter.
他们在本地主机上使用邮件服务器,但我不建议这样做。如果您的本地主机是具有正确 DNS 记录的服务器,则完全可以进行测试。如果您从 DNS 反向查找不起作用的 IP 发送电子邮件,则您的邮件很可能会被垃圾邮件过滤器阻止。
If you want a mailserver on localhost you could install exim, postfix, ssmtp or any other MTA from your linux distribution. It's possible that one of them is already installed and running.
如果你想在本地主机上安装邮件服务器,你可以从你的 Linux 发行版中安装 exim、postfix、ssmtp 或任何其他 MTA。其中之一可能已经安装并正在运行。
I would use some other mail server for example the one you are using to send emails. This line of code from the tutorial configures the host:
我会使用其他一些邮件服务器,例如您用来发送电子邮件的服务器。教程中的这行代码配置了主机:
// Setup mail server
properties.setProperty("mail.smtp.host", host);