简体   繁体   English

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

[英]How to programmatically click a .zip link in an email message body

This is probably very simple but I am extremely new to coding anything, sorry in advance. 这可能很简单,但是对任何事情我都非常陌生,对不起。

Currently I have a button4 that will read through my inbox for messages with a certain subject, if condition is met it displays the messages first class properties in a listview but I want it to also download the link found in each email. 目前,我有一个button4,它将通过我的收件箱通读具有特定主题的邮件,如果满足条件,它将在列表视图中显示邮件的头等属性,但我希望它也下载在每封电子邮件中找到的链接。

It is a .zip link that when the link is clicked from inside the email it will download the zip. 这是一个.zip链接,当从电子邮件内部单击该链接时,它将下载该zip。 I want it to automatically download all links found when button4 is clicked. 我希望它自动下载单击button4时发现的所有链接。

I will show my button4 code and then an example of what the email is. 我将显示我的button4代码,然后显示该电子邮件的示例。

button4 code: button4代码:

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!!");
        }
    }
}

Example email: 电子邮件示例:

NFIRS File Validation NFIRS文件验证

The NFIRS File Validation service has completed processing your files. NFIRS文件验证服务已完成对文件的处理。 Please follow this link to retrieve the zip file containing your results. 请点击此链接以检索包含结果的zip文件。

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

This file will be deleted after 28 days. 该文件将在28天后删除。

If you have any questions, please do not reply to this email. 如有任何疑问,请不要回复此电子邮件。 Instead, please contact the NFIRS Support Center. 相反,请联系NFIRS支持中心。

This is basically a duplicate of the link @DonBoitnott commented the only extra steps I am taking is putting the body of each email into a property list parsing it and making sure it saves as the same filename as the URL had in the original email 这基本上是@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");
    }

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

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