是否可以使用Gmail帐户从我的Java应用程序发送电子邮件?我已经将公司邮件服务器配置为Java应用程序以发送电子邮件,但是当我分发应用程序时,这不会将其删除。任何使用Hotmail,Yahoo或Gmail的答案都是可以接受的。
Is it possible to send an email from my Java application using a GMail account? I have configured my company mail server with Java app to send email, but that's not going to cut it when I distribute the application. Answers with any of using Hotmail, Yahoo or GMail are acceptable.
发布评论
评论(14)
首先下载 JavaMail API 并确保相关的 jar 文件位于您的类路径中。
这是使用 GMail 的完整工作示例。
当然,您需要在
catch
块中执行更多操作,而不是像我在上面的示例代码中那样打印堆栈跟踪。 (删除catch
块以查看 JavaMail API 中的哪些方法调用会引发异常,以便您可以更好地了解如何正确处理它们。)感谢 @jodonnel 以及其他所有回答的人。我给他悬赏是因为他的回答让我得到了大约 95% 的完整答案。
First download the JavaMail API and make sure the relevant jar files are in your classpath.
Here's a full working example using GMail.
Naturally, you'll want to do more in the
catch
blocks than print the stack trace as I did in the example code above. (Remove thecatch
blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)Thanks to @jodonnel and everyone else who answered. I'm giving him a bounty because his answer led me about 95% of the way to a complete answer.
像这样的事情(听起来你只需要更改你的 SMTP 服务器):
Something like this (sounds like you just need to change your SMTP server):
上面其他人有很好的答案,但我想在这里补充一下我的经验。我发现,当使用 Gmail 作为我的网络应用程序的出站 SMTP 服务器时,Gmail 只允许我发送大约 10 封左右的邮件,然后再进行反垃圾邮件响应,我必须手动逐步执行该响应才能重新启用 SMTP 访问。我发送的电子邮件不是垃圾邮件,而是用户在我的系统注册时收到的网站“欢迎”电子邮件。所以,YMMV,我不会依赖 Gmail 来构建网络应用程序。如果您代表用户发送电子邮件,例如安装的桌面应用程序(用户输入自己的 Gmail 凭据),则可能没问题。
另外,如果您使用 Spring,这里有一个使用 Gmail 进行出站 SMTP 的工作配置:
Other people have good answers above, but I wanted to add a note on my experience here. I've found that when using Gmail as an outbound SMTP server for my webapp, Gmail only lets me send ~10 or so messages before responding with an anti-spam response that I have to manually step through to re-enable SMTP access. The emails I was sending were not spam, but were website "welcome" emails when users registered with my system. So, YMMV, and I wouldn't rely on Gmail for a production webapp. If you're sending email on a user's behalf, like an installed desktop app (where the user enters their own Gmail credentials), you may be okay.
Also, if you're using Spring, here's a working config to use Gmail for outbound SMTP:
即使这个问题已经结束,我想发布一个计数器解决方案,但现在使用 Simple Java Mail (开源 JavaMail smtp 包装器):
Even though this question is closed, I'd like to post a counter solution, but now using Simple Java Mail (Open Source JavaMail smtp wrapper):
我的完整代码如下运行良好:
My complete code as below is working well:
最低要求:
The minimum required:
当您需要在同一JVM中的任何地方设置多个SMTP会话时,已发布的代码解决方案可能会导致问题。
Javamail常见问题解答建议使用
而不是
使用,因为GetDefault仅使用第一次调用属性。默认实例的所有以后使用将忽略属性更改。
参见 http:///wwwww.oracle.com/technetwork.com/technetwork/technetwork/java/java/java/java/java/java/java/java/java/java/java/java-java/java/java-java/java/java/java-faq--- 135477.html#getDefaultInstance
The posted code solutions may cause problems when you need to set up multiple SMTP sessions anywhere within the same JVM.
The JavaMail FAQ recommends using
instead of
because the getDefault will only use the properties given the first time it is invoked. All later uses of the default instance will ignore property changes.
See http://www.oracle.com/technetwork/java/faq-135477.html#getdefaultinstance
增值:
Session.getInstance()
推荐使用Session.getDefaultInstance()
Value added:
Session.getInstance()
recommended overSession.getDefaultInstance()
这是我想发送带有附件的电子邮件时要做的,工作正常。 :)
This is what I do when i want to send email with attachment, work fine. :)
一个简单的方法是配置/启用 POP3 访问的 Gmail 帐户。这将允许您通过 gmail 服务器通过普通 SMTP 发送出去。
然后您只需通过 smtp.gmail.com(端口 587)发送
An easy route would be to have the gmail account configured/enabled for POP3 access. This would allow you to send out via normal SMTP through the gmail servers.
Then you'd just send through smtp.gmail.com (on port 587)
嗨,尝试此代码。
Hi try this code....
这是一个易于使用的类,用于使用
Gmail
发送电子邮件。您需要有JavaMail
库 添加到您的构建路径 或仅使用Maven
。用法示例:
Here's an easy-to-use class for sending emails with
Gmail
. You need to have theJavaMail
library added to your build path or just useMaven
.Example usage:
如果您想将 Outlook 与
Javamail API
一起使用,请用作主机以获取更多完整的工作代码查看此答案。
If you want to use outlook with
Javamail API
then useas a host for more and complete working code Check out this answer.