简体   繁体   English

用Java自动处理电子邮件

[英]Automated processing of an Email in Java

Just got a request from my boss for an application I'm working on. 刚收到老板要求我正在处理的申请。 Basically we're getting an email address setup for an external client to submit excel files to. 基本上我们正在为外部客户端设置电子邮件地址以提交excel文件。

What I need is a way to automatically pick up any email sent to this address, so I can take the attachment, process it and save it to a folder. 我需要的是一种自动获取发送到该地址的任何电子邮件的方法,因此我可以获取附件,处理它并将其保存到文件夹中。

Any information of even where to start would be helpful.\\ 即使从哪里开始的任何信息都会有所帮助。

Note: We're using a lotus notes server to do this, but a generic way would be more helpful (If possible). 注意:我们使用Lotus Notes服务器来执行此操作,但通用方法会更有帮助(如果可能)。

Email -> mailserver ->[something] -> file-on-disk. 电子邮件 - >邮件服务器 - > [东西] - >磁盘上的文件。

File on disk is pretty easy to parse, use JavaMail . 磁盘上的文件很容易解析,使用JavaMail

The [something] could be: [东西]可能是:

  • listener for smtp connections (overkill)! smtp连接的监听器(矫枉过正)!
  • Pop3 / imap client Pop3 / imap客户端
  • Maildir /Mailbox Maildir /邮箱

Edit: since I first wrote this answer, Wiser has moved and now claims to only be a unit testing tool, so take the answer below with a pinch of salt... 编辑:自从我第一次写这个答案以来,Wiser已经搬家了,现在声称只是一个单元测试工具,所以请用下面的答案拿一小撮...


Svrist's answer is good, but if you want to avoid his middle step (the mailserver that writes the mail to disk for later pickup by the Java system) you can use Wiser . Svrist的答案很好,但是如果你想避免他的中间步骤(将邮件写入磁盘以便以后由Java系统拾取的邮件服务器),你可以使用Wiser

Wiser lets you start an in-Java mailserver: Wiser允许您启动Java内邮件服务器:

Wiser wiser = new Wiser();
wiser.setPort(2500);
wiser.start();

Then you can just poll it periodically for mail: 然后你可以定期轮询邮件:

for (WiserMessage message : wiser.getMessages())
{
    String envelopeSender = message.getEnvelopeSender();
    String envelopeReceiver = message.getEnvelopeReceiver();

    MimeMessage mess = message.getMimeMessage();

    // mail processing goes here
}

I've done quite a bit lately with Java agents on Domino servers. 我最近在Domino服务器上使用Java代理做了很多。 The Domino 8.5 server supports Java 6 and its embedded so it won't take someone with a bit of Domino development experience long to put together an agent that runs when new mail arrives. Domino 8.5服务器支持Java 6及其嵌入式设备,因此不需要具有一点Domino开发经验的人将新邮件到达时运行的代理放在一起。 In LotusScript its even easier but that needs more specialised skills which you'd probably need to get a contractor in to provide. 在LotusScript中,它更容易,但需要更多的专业技能,您可能需要让承包商提供。

The limitation your likely to encounter concerns the extracted file, you can easily place it on the Domino server's file structure but you may be limited by the OS security from placing it on a different server. 您可能遇到的限制涉及提取的文件,您可以轻松地将其放在Domino服务器的文件结构上,但是您可能会受到操作系统安全性的限制而不能将其放在不同的服务器上。

Use a mail in database (your Domino administrator can set that up for you but it's in the help file as well). 在数据库中使用邮件(您的Domino管理员可以为您设置,但它也在帮助文件中)。

In that database, you can create an agent that runs periodically to process all new documents. 在该数据库中,您可以创建定期运行以处理所有新文档的代理。 That agent will use the EmbeddedObjects property of the NotesRichTextItem class and the ExtractFile method of the NotesEmbeddedObject class to get a handle on the file attachment and extract it to the location you specify. 该代理将使用NotesRichTextItem类的EmbeddedObjects属性和NotesEmbeddedObject类的ExtractFile方法来获取文件附件的句柄并将其提取到您指定的位置。

For example, this script goes through all the file attachments, object links, and embedded objects in the Body item of a document. 例如,此脚本遍历文档的Body项中的所有文件附件,对象链接和嵌入对象。 Each time it finds a file attachment, it detaches the file to the SAMPLES directory on the C drive and removes the attachment from the document 每次找到文件附件时,它都会将文件分离到C驱动器上的SAMPLES目录,并从文档中删除附件

Dim doc As NotesDocument
Dim rtitem As Variant
'...set value of doc...
Set rtitem = doc.GetFirstItem( "Body" )
If ( rtitem.Type = RICHTEXT ) Then
  Forall o In rtitem.EmbeddedObjects
    If ( o.Type = EMBED_ATTACHMENT ) Then
      Call o.ExtractFile( "c:\samples\" & o.Source )
      Call o.Remove
      Call doc.Save( False, True )
    End If
  End Forall
End If

No matter what you do, you'll need an understanding of the Lotus Notes data structures. 无论您做什么,您都需要了解Lotus Notes数据结构。 The good news is that a fully automated solution can be built in Notes very easily. 好消息是,可以非常轻松地在Notes中构建完全自动化的解决方案。

Your best bet is to have it built within Notes, and it can be set up to run automatically whenever new mail is received. 您最好的选择是在Notes中构建它,并且可以将其设置为在收到新邮件时自动运行。 Gary's answer is dead on, but without any experience, it would probably be hard to figure out how to implement it yourself. 加里的答案已经过时,但没有任何经验,可能很难弄清楚如何自己实施。 On the other hand, it really shouldn't take any competent Notes programmer more than an hour or two to set it up. 另一方面,它确实不应该让任何有能力的Notes程序员花费超过一两个小时进行设置。

Lotus Notes/Domino stores mail in a Notes database. Lotus Notes / Domino将邮件存储在Notes数据库中。 There are APIs available for getting documents (emails), reading field values (From, Subject), and detaching files. 有些API可用于获取文档(电子邮件),读取字段值(From,Subject)和分离文件。

APIs include API包括

-LotusScript (VB variant, available within the Notes database) -LotusScript(VB变体,在Notes数据库中可用)

-Java (from within or external to the database) -Java(来自数据库内部或外部)

-C API (external) -CAP(外部)

-Same API available through COM server -Same API可通过COM服务器获得

You can create a "scheduled agent" within the database (using LotusScript or Java) that can locate documents created since it last ran, locate the attachments, and extract them. 您可以在数据库中创建“预定代理”(使用LotusScript或Java),该代理可以查找自上次运行以来创建的文档,找到附件并提取它们。 The agent will need to be signed with an ID that has the appropriate permissions on the server, including those required to write to the file system and initiate any other processes. 代理程序需要使用在服务器上具有适当权限的ID进行签名,包括写入文件系统和启动任何其他进程所需的权限。

External to the database, you can use any API except LotusScript to log-in to the server/mail database, and follow a similar process, eg extracting the files locally on a client or separate server. 在数据库外部,您可以使用除LotusScript之外的任何API登录到服务器/邮件数据库,并遵循类似的过程,例如在客户端或单独的服务器上本地提取文件。 C API and COM require a notes client install, but Java applications can be set up to run via CORBA/DIIOP without a full install. C API和COM需要Notes客户端安装,但Java应用程序可以设置为在没有完整安装的情况下通过CORBA / DIIOP运行。

Consult the Domino Designer help (or IBM's website for C API) for more information. 有关更多信息,请参阅Domino Designer帮助(或IBM的C API网站)。

As to a "generic way" to do this, if you are accessing data in Notes and needing to extract attachments, I believe these APIs are your best option. 至于执行此操作的“通用方法”,如果您正在访问Notes中的数据并需要提取附件,我相信这些API是您的最佳选择。 If you envision porting the application to another mail system, consider decoupling the API routines via an "interface" so you only need to add a new implementation of that interface to support a new mail system. 如果您设想将应用程序移植到另一个邮件系统,请考虑通过“接口”解耦API例程,这样您只需添加该接口的新实现来支持新的邮件系统。

您可以使用DIIOP相对轻松地访问Notes文档,比沿着C Api道路更容易...

Try POP3Client in the Net Commons package ; Net Commons包中尝试POP3Client; it'll let your Java program check for new mail for a particular account at whatever interval you want (every few minutes? hourly?), and get/delete messages as desired. 它会让你的Java程序以你想要的任何间隔检查特定帐户的新邮件(每隔几分钟?每小时?),并根据需要获取/删除邮件。

SMTP/POP3 can be enabled on the Domino server. 可以在Domino服务器上启用SMTP / POP3。 Worked with this before and gotten Squirrel Mail running with it. 之前使用过这个并且让Squirrel Mail运行它。 SMTP is a bit resource intensive, but well worth the effort because then you don't have to descend into LotusLand to get things working. SMTP是一种资源密集型,但值得付出努力,因为这样您就不必下载到LotusLand中来实现工作。 Just write a small Java CLI program that will check a specific email box (POP3 or SMTP), and parse through the messages, pulling the attachments and placing them where needed. 只需编写一个小型Java CLI程序,它将检查特定的电子邮箱(POP3或SMTP),并解析消息,拉出附件并将它们放在需要的位置。

Plenty of documentation and examples here: http://java.sun.com/products/javamail/ 这里有大量文档和示例: http//java.sun.com/products/javamail/

The techniques that you develop taking this approach will be more widely applicable in your future career than anything Lotus/Domino specific. 使用此方法开发的技术将在未来的职业生涯中比Lotus / Domino特定的任何内容更广泛地应用。

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

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