代码之家  ›  专栏  ›  技术社区  ›  RobertJRu

如何以编程方式单击。电子邮件正文中的zip链接

  •  0
  • RobertJRu  · 技术社区  · 7 年前

    目前,我有一个按钮4,可以在收件箱中阅读具有特定主题的邮件,如果满足条件,它会在列表视图中显示邮件的第一类属性,但我希望它也能下载在每封电子邮件中找到的链接。

    我将展示我的button4代码,然后是电子邮件的示例。

    按钮4代码:

    private void button4_Click(object sender, EventArgs e)
    {
        EmailConnect();
        TimeSpan ts = new TimeSpan(0, -2, 0, 0);
        DateTime date = DateTime.Now.Add(ts);
        SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
    
        if (service != null)
        {
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
    
            foreach (Item item in findResults)
            {
    
                EmailMessage message = EmailMessage.Bind(service, item.Id);
                string subject = message.Subject.ToString();
    
                if (subject.Contains("NFIRS File Validation"))
                {
                    ListViewItem listitem = new ListViewItem(new[]
                    {message.DateTimeReceived.ToString(), message.From.Name.ToString() + "(" + message.From.Address.ToString() + ")", message.Subject, ((message.HasAttachments) ? "Yes" : "No")});
    
                    lstMsg.Items.Add(listitem);
                }
            }
            
            if (findResults.Items.Count <= 0)
            {
                lstMsg.Items.Add("No Messages found!!");
            }
        }
    }
    

    电子邮件示例:

    NFIRS文件验证

    https://www.nfirs.fema.gov/biarchive/xxxxxxxxx_xxxxxxxxx.zip

    如果您有任何问题,请不要回复此电子邮件。请联系NFIRS支持中心。

    1 回复  |  直到 5 年前
        1
  •  0
  •   RobertJRu    7 年前

    这基本上是一个重复的链接@DonBoitnott评论说,我所采取的唯一额外步骤是将每封电子邮件的正文放入属性列表中,对其进行解析,并确保其保存为与原始电子邮件中的URL相同的文件名

        private void handleLinks(List<EmailProperties> properties)
        {
            using (WebClient client = new WebClient())
            {
                foreach (var prop in properties)
                {
                    string link = searchForLink(prop.Body);
                    string fileName = MyExtensions.Between(link, "https://www.nfirs.fema.gov/biarchive/", ".zip");
                    string saveTo = string.Format((@"C:\Users\Foo\Downloads\{0}.zip"), fileName);
                    prop.Name = fileName;
    
                    client.DownloadFile(link, saveTo);
                }
            }
        }
    
        private string searchForLink(string body)
        {
            return MyExtensions.Between(body, "results.\r\n\r\n", "\r\n\r\nThis file will");
        }