• 自动抽取邮件内容


    公司最近在做电子发票的归集,电子发票最终流转的末节点就是邮箱。所以我们要自动抽取邮箱中的内容,并根据标题下载附件到本地。

    这个用到了一个插件openpop插件,所有内容,包括标题都是强类型的,所以我们可以很方便的操作邮件中的东西。

    直接上代码看看吧

     public static void ReadPop3(string email, string uid, string psd)
            {
                string service = string.Empty;
                int portnub = 110;
                
                using (Pop3Client client = new Pop3Client())
                {
                    if (client.Connected)
                    {
                        client.Disconnect();
                    }
                    if (email.Contains("qq") || email.Contains("QQ"))
                    {
                        service = "pop.qq.com";
                        portnub = 995;
                        client.Connect(service, portnub, true);
    
                    }
                    else if (email.Contains("sina"))
                    {
                        service = "pop.sina.com";
                        portnub = 110;
                    }
                    // Connect to the server, false means don't use ssl
    
                    if (!client.Connected)
                    {
                        client.Connect(service, portnub, false);
                    }
                    
                    client.Authenticate(uid, psd);
                    //email count
                    int messageCount = client.GetMessageCount();
                    //i = 1 is the first email; 1 is the oldest email
                    for (int i = 1; i<= messageCount; i++)
                    {
                        Message message = client.GetMessage(i);
                        string sender = message.Headers.From.DisplayName;
                        string from = message.Headers.From.Address;
                        string subject = message.Headers.Subject;
                        DateTime Datesent = message.Headers.DateSent;
                        MessagePart messagePart = message.MessagePart;
                        string body = " ";
                        if (messagePart.IsText)
                        {
                            body = messagePart.GetBodyAsText();
                        }
                        
                        else if (messagePart.IsMultiPart)
                        {
                            MessagePart plainTextPart = message.FindFirstPlainTextVersion();
                            if (plainTextPart != null)
                            {
                                // The message had a text/plain version - show that one
                                body = plainTextPart.GetBodyAsText();
                               
                            }
                            else
                            {
                                // Try to find a body to show in some of the other text versions
                                List<MessagePart> textVersions = message.FindAllTextVersions();
                                if (textVersions.Count >= 1)
                                    body = textVersions[0].GetBodyAsText();
                                else
                                    body = "<<OpenPop>> Cannot find a text version body in this message.";
                            }
                        }
                        List<MessagePart> list = message.FindAllAttachments();
                        if (list.Count > 0)
                        {
                            for (int j = 0; j < list.Count; j++)
                            {
                                Random rnd = new Random();
                            int iRandNumber = rnd.Next(0, 1000000);
                            string name = DateTime.Now.ToString("yyyyMMddHHMMss") + iRandNumber.ToString();
    
                            string str3 = Directory.GetCurrentDirectory();
                            string sPath = str3 + "\" + DateTime.Now.ToString("yyyyMM");
    
                            if (!Directory.Exists(sPath))
                            {
                                Directory.CreateDirectory(sPath);
                            }
                            string SaveAsFileImagePath = sPath + "\" + name + ".pdf";
                            FileStream fs = new FileStream(SaveAsFileImagePath, FileMode.CreateNew);
                                list[j].Save(fs);
                            }
                        }
                        
                    }
                }
            }

    看起来确实很方便,插件从nuget中直接找到,openpop就可以了。

    邮件采集主要用到了第三方插件,我们可以从nuget中下载到,openpop这个插件,他可以通过pop3协议把远程服务器上的邮件下载到本地,还能分析里面的内容,并且是强对象类型,方便操作。

    目前除了qq邮箱出于安全考虑(qq邮箱密码和qq号密码一样,所以出于安全考虑,腾讯默认不允许开启pop服务协议,在邮箱中设置开启之后,他会生成一个随机key作为qq邮箱密码),默认没有打开pop服务协议,其他邮箱默认开启。这样我们就能抽取邮件中的带发票字样标题的附件了。

  • 相关阅读:
    jboss项目迁移至WebLogic12
    数据库字段关联更新
    清理ms sql server 大日志文件数据
    tool class
    pwn学习日记Day17 《程序员的自我修养》读书笔记
    pwn学习日记Day16 pwn原理理解
    pwn学习日记Day15 《程序员的自我修养》读书笔记
    pwn学习日记Day14 《程序员的自我修养》读书笔记
    pwn学习日记Day13 《程序员的自我修养》读书笔记
    pwn学习日记Day12 《程序员的自我修养》读书笔记
  • 原文地址:https://www.cnblogs.com/albertzhangyu/p/6065836.html
Copyright © 2020-2023  润新知