简体   繁体   English

从Java应用程序发送电子邮件的最佳方式

[英]Best way to send an E-Mail from a java Application

I've been searching for the best way to send an e-mail with an attachment by my java application. 我一直在寻找通过我的java应用程序发送附件的电子邮件的最佳方式。 I want to use this as an users bug report with logger files. 我想将此用作带有记录器文件的用户错误报告。 The recipient should be my own e-mail address. 收件人应该是我自己的电子邮件地址。 I'd prefer the use of an e-mail client. 我更喜欢使用电子邮件客户端。

I tried the following: 我尝试了以下方法:

  • Send an e-mail with user authentification like this . 发送电子邮件与用户认证喜欢这样 I don't want to use this, because the user would need to reveal his e-mail account and password. 我不想使用它,因为用户需要透露他的电子邮件帐户和密码。 Furthermore, I'd have to set the properties for every e-mail adress, which is impossible. 此外,我必须为每个电子邮件地址设置属性,这是不可能的。

  • Send an e-mail directly to my own e-mail address like this in Listing 16.16 (didnt found an english example) . 直接发送电子邮件到我自己的电子邮件地址,如清单16.16所示(没有找到英文示例) The problem is every e-mail server is using POP authentification nowadays, that means the recipient e-mail server won't accept my e-mail. 问题是现在每个电子邮件服务器都使用POP身份验证,这意味着收件人电子邮件服务器将不接受我的电子邮件。

  • Using the mailto URL syntax like this . 使用像这样的mailto URL语法。 Doesn't work aswell, because the attachment function isn't working properly in every e-mail client. 也不起作用,因为附件功能在每个电子邮件客户端中都无法正常工作。 Best solution so far is to brief the user to add the attachment by himself, after I would put it to his desktop. 到目前为止,最好的解决方案是向用户简要介绍在我将其放入桌面后自己添加附件。 Or upload the data and add a link to the e-mail body. 或者上传数据并添加指向电子邮件正文的链接。

  • The last way I've found is this one . 我发现的最后一种方式就是这个 As you could assume this won't work either, because the localhost needs to be connected to the internet and capable enough to send an e-mail. 您可以认为这也不起作用,因为localhost需要连接到Internet并且足够能够发送电子邮件。

Hopefully I explained my problem well enough. 希望我能很好地解释我的问题。 Is there a different way to send bug reports? 是否有不同的方式发送错误报告?

The generally accepted way to get around the problems you describe is to keep all the email logic server side, and then have your application call a web service with the appropriate parameters. 解决您描述的问题的普遍接受的方法是保留所有电子邮件逻辑服务器端,然后让您的应用程序使用适当的参数调用Web服务。 It's pretty easy to knock a PHP script / servlet up that will do the job and then send the results on via email, put them in a mysql database or so on. 敲一下将完成工作的PHP脚本/ servlet很容易,然后通过电子邮件发送结果,将它们放在mysql数据库中等等。

However, if you can't / don't want to / won't keep this server side, I'd recommend using JavaMail to create a MimeMessage , then using writeTo() to write this to an EML file. 但是,如果您不能/不希望保留此服务器端,我建议使用JavaMail创建MimeMessage ,然后使用writeTo()将其写入EML文件。

You can then do the usual: 然后你可以做通常的事情:

Desktop.getDesktop().open(emlFile);

...which will open the EML file with the default application for handing those files, which is almost always the mail client. ...将使用默认应用程序打开EML文件以处理这些文件,这几乎总是邮件客户端。 Still not foolproof, but if you're determined on sending the email from the client directly I think that's as good as you'll get. 仍然不是万无一失,但如果您决定直接从客户端发送电子邮件,我认为这就像您将获得的一样好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM