以编程方式从POP3打开电子邮件并提取附件

时间:2021-03-18 13:59:54

We have a vendor that sends CSV files as email attachments. These CSV files contain statuses that are imported into our application. I'm trying to automate the process end-to-end, but it currently depends on someone opening an email, saving the attachment to a server share, so the application can use the file.

我们有一家供应商将CSV文件作为电子邮件附件发送。这些CSV文件包含导入我们的应用程序的状态。我正在尝试端到端地自动化流程,但它目前依赖于有人打开电子邮件,将附件保存到服务器共享,因此应用程序可以使用该文件。

Since I cannot convince the vendor to change their process, such as offering an FTP location or a Web Service, I'm stuck with trying to automate the existing process.

由于我无法说服供应商改变他们的流程,例如提供FTP位置或Web服务,因此我不得不尝试自动化现有流程。

Does anyone know of a way to programmatically open an email from a POP3 account and extract an attachment? The preferred solution would reside on a Windows 2003 server, be written VB.NET and secure. The application can reside on the same server as the POP3 server, for example, we could setup the free POP3 server that comes with Windows Server and pull against the mail file stored on the file system.

有没有人知道从POP3帐户以编程方式打开电子邮件并提取附件的方法?首选解决方案将驻留在Windows 2003服务器上,编写VB.NET并且安全。应用程序可以驻留在与POP3服务器相同的服务器上,例如,我们可以设置Windows Server附带的免费POP3服务器,并对存储在文件系统上的邮件文件进行拉取。

BTW, we are willing to pay for an off-the-shelf solution, if one exists.

顺便说一句,如果存在,我们愿意支付现成的解决方案。

Note: I did look at this question but the answer points to a CodeProject solution that doesn't deal with attachments.

注意:我确实看过这个问题,但答案指向一个不处理附件的CodeProject解决方案。

3 个解决方案

#1


possible duplication of Reading Email using Pop3 in C#

在C#中使用Pop3可能重复阅读电子邮件

Atleast, there's a shed load of suggestions there that you may find useful

至少,那里有大量的建议你可能觉得有用

#2


Try Mail.dll email component, it's very affordable, supports attachments national characters and is easy to use, it also supports SSL:

尝试Mail.dll电子邮件组件,它非常实惠,支持附件国家字符并且易于使用,它还支持SSL:

Using pop3 As New Pop3()
    pop3.Connect("mail.server.com") 
    pop3.Login("user", "password")                            

    Dim builder As New MailBuilder()
    For Each uid As String In pop3.GetAll()           
        ' Receive email message'
        Dim mail As IMail = builder.CreateFromEml(pop3.GetMessageByUID(uid))

        'Write out received message'
        Console.WriteLine(mail.Subject)

        'Here you can use mail.Attachmets collection'
        For Each attachment As MimeData In mail.Attachments
            Console.WriteLine(attachment.FileName)
            attachment.Save("c:\" + attachment.FileName)
            ' you can also use attachment.Data here'
        Next attachment

    Next

    pop3.Close(true)   
End Using

You can download it here: http://www.lesnikowski.com/mail.

你可以在这里下载:http://www.lesnikowski.com/mail。

#3


I'll throw in a late suggestion for a more generalized "download POP3 messages and extract attachments" solution using existing software and minimal programming. I needed to do this for a client who switched to receiving faxes via email and was not pleased with manually saving the attachments to a location where they could be imported into an application.

我将推迟使用现有软件和最少编程的更广泛的“下载POP3消息和提取附件”解决方案的建议。我需要为通过电子邮件切换到接收传真的客户端执行此操作,并且不满意手动将附件保存到可以将其导入应用程序的位置。

For downloading messages on *nix systems fetchmail seems to be the standard and is very capable, but I chose mpop for both simplicity and Windows compatibility (but it is cross-platform). If mpop hadn't done the trick for me, I probably would have ended up doing something with the Python-based getmail, which was created when fetchmail's development stalled for a time (it's since resumed).

为了在* nix系统上下载消息,fetchmail似乎是标准的并且非常强大,但我选择了mpop以实现简单性和Windows兼容性(但它是跨平台的)。如果mpop没有为我完成这个技巧,我可能最终会使用基于Python的getmail做一些事情,这是在fetchmail的开发停滞一段时间后创建的(它已经恢复)。

Mpop is controlled either via command line or configuration file, so I simply created multiple configuration files and specify via command line which file to load. I'm using it in "Exchange pickup directory" mode, which means it simply downloads the messages and drops them as text (.eml) files in a specified directory.

Mpop是通过命令行或配置文件控制的,所以我只创建了多个配置文件,并通过命令行指定要加载的文件。我在“Exchange代理目录”模式下使用它,这意味着它只是下载消息并将它们作为文本(.eml)文件放在指定的目录中。

For extraction of the message attachments, UUDeview appears to be the standard (I'm using the Windows port of UUDeview) across just about any system you could want with just about any features you could want. My main alternative to this was a much-less-capable Python script that I'd developed for a different client back in 2007, but I'm happy to go with a precompiled executable over either installing Python or packaging with any of the Python-to-exe options.

为了提取消息附件,UUDeview似乎是几乎所有你想要的系统的标准(我使用的是UUDeview的Windows端口)。我对此的主要替代方案是我在2007年为不同的客户端开发的功能要弱得多的Python脚本,但我很高兴能够使用预编译的可执行文件来安装Python或使用任何Python包装 - to-exe选项。

Finally there's the configuration - along with the two mpop configuration files mentioned above (which I could do away with by using command-line options), I also have two 2-line .cmd files launched every 10 minutes by scheduled task - the first line to launch mpop to download into a working directory and the second line to launch UUDeview and extract attachments of specified types (.pdf or .tif) then delete each file from which it extracted attachments. Output is sent to another directory from which staff can directly attach files as needed.

最后是配置 - 以及上面提到的两个mpop配置文件(我可以通过使用命令行选项取消),我还有两个2行.cmd文件按计划任务每​​10分钟启动一次 - 第一行启动mpop以下载到工作目录,第二行启动UUDeview并提取指定类型的附件(.pdf或.tif),然后删除从中提取附件的每个文件。输出发送到另一个目录,员工可以根据需要直接附加文件。

This is overall not the most elegant way to reach these ends, but it was quick, simple, functional and reasonably robust - at each stage if something goes wrong it fails such that no data is lost. The only places where data could be lost are any non-attachment messages being sent to the dedicated fax email addresses, and even those will sit in the processing directory and be caught eventually.

这总体上不是达到这些目的的最优雅的方式,但它快速,简单,功能和相当健壮 - 在每个阶段,如果出现问题,它失败,没有数据丢失。唯一可能丢失数据的地方是发送到专用传真电子邮件地址的任何非附件消息,甚至那些将位于处理目录中并最终被捕获。

#1


possible duplication of Reading Email using Pop3 in C#

在C#中使用Pop3可能重复阅读电子邮件

Atleast, there's a shed load of suggestions there that you may find useful

至少,那里有大量的建议你可能觉得有用

#2


Try Mail.dll email component, it's very affordable, supports attachments national characters and is easy to use, it also supports SSL:

尝试Mail.dll电子邮件组件,它非常实惠,支持附件国家字符并且易于使用,它还支持SSL:

Using pop3 As New Pop3()
    pop3.Connect("mail.server.com") 
    pop3.Login("user", "password")                            

    Dim builder As New MailBuilder()
    For Each uid As String In pop3.GetAll()           
        ' Receive email message'
        Dim mail As IMail = builder.CreateFromEml(pop3.GetMessageByUID(uid))

        'Write out received message'
        Console.WriteLine(mail.Subject)

        'Here you can use mail.Attachmets collection'
        For Each attachment As MimeData In mail.Attachments
            Console.WriteLine(attachment.FileName)
            attachment.Save("c:\" + attachment.FileName)
            ' you can also use attachment.Data here'
        Next attachment

    Next

    pop3.Close(true)   
End Using

You can download it here: http://www.lesnikowski.com/mail.

你可以在这里下载:http://www.lesnikowski.com/mail。

#3


I'll throw in a late suggestion for a more generalized "download POP3 messages and extract attachments" solution using existing software and minimal programming. I needed to do this for a client who switched to receiving faxes via email and was not pleased with manually saving the attachments to a location where they could be imported into an application.

我将推迟使用现有软件和最少编程的更广泛的“下载POP3消息和提取附件”解决方案的建议。我需要为通过电子邮件切换到接收传真的客户端执行此操作,并且不满意手动将附件保存到可以将其导入应用程序的位置。

For downloading messages on *nix systems fetchmail seems to be the standard and is very capable, but I chose mpop for both simplicity and Windows compatibility (but it is cross-platform). If mpop hadn't done the trick for me, I probably would have ended up doing something with the Python-based getmail, which was created when fetchmail's development stalled for a time (it's since resumed).

为了在* nix系统上下载消息,fetchmail似乎是标准的并且非常强大,但我选择了mpop以实现简单性和Windows兼容性(但它是跨平台的)。如果mpop没有为我完成这个技巧,我可能最终会使用基于Python的getmail做一些事情,这是在fetchmail的开发停滞一段时间后创建的(它已经恢复)。

Mpop is controlled either via command line or configuration file, so I simply created multiple configuration files and specify via command line which file to load. I'm using it in "Exchange pickup directory" mode, which means it simply downloads the messages and drops them as text (.eml) files in a specified directory.

Mpop是通过命令行或配置文件控制的,所以我只创建了多个配置文件,并通过命令行指定要加载的文件。我在“Exchange代理目录”模式下使用它,这意味着它只是下载消息并将它们作为文本(.eml)文件放在指定的目录中。

For extraction of the message attachments, UUDeview appears to be the standard (I'm using the Windows port of UUDeview) across just about any system you could want with just about any features you could want. My main alternative to this was a much-less-capable Python script that I'd developed for a different client back in 2007, but I'm happy to go with a precompiled executable over either installing Python or packaging with any of the Python-to-exe options.

为了提取消息附件,UUDeview似乎是几乎所有你想要的系统的标准(我使用的是UUDeview的Windows端口)。我对此的主要替代方案是我在2007年为不同的客户端开发的功能要弱得多的Python脚本,但我很高兴能够使用预编译的可执行文件来安装Python或使用任何Python包装 - to-exe选项。

Finally there's the configuration - along with the two mpop configuration files mentioned above (which I could do away with by using command-line options), I also have two 2-line .cmd files launched every 10 minutes by scheduled task - the first line to launch mpop to download into a working directory and the second line to launch UUDeview and extract attachments of specified types (.pdf or .tif) then delete each file from which it extracted attachments. Output is sent to another directory from which staff can directly attach files as needed.

最后是配置 - 以及上面提到的两个mpop配置文件(我可以通过使用命令行选项取消),我还有两个2行.cmd文件按计划任务每​​10分钟启动一次 - 第一行启动mpop以下载到工作目录,第二行启动UUDeview并提取指定类型的附件(.pdf或.tif),然后删除从中提取附件的每个文件。输出发送到另一个目录,员工可以根据需要直接附加文件。

This is overall not the most elegant way to reach these ends, but it was quick, simple, functional and reasonably robust - at each stage if something goes wrong it fails such that no data is lost. The only places where data could be lost are any non-attachment messages being sent to the dedicated fax email addresses, and even those will sit in the processing directory and be caught eventually.

这总体上不是达到这些目的的最优雅的方式,但它快速,简单,功能和相当健壮 - 在每个阶段,如果出现问题,它失败,没有数据丢失。唯一可能丢失数据的地方是发送到专用传真电子邮件地址的任何非附件消息,甚至那些将位于处理目录中并最终被捕获。