Search code examples
c#winformsexchangewebservicesemail-attachmentslistviewitem

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.

It is a .zip link that when the link is clicked from inside the email it will download the zip. I want it to automatically download all links found when button4 is clicked.

I will show my button4 code and then an example of what the email is.

button4 code:

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

The NFIRS File Validation service has completed processing your files. Please follow this link to retrieve the zip file containing your results.

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

This file will be deleted after 28 days.

If you have any questions, please do not reply to this email. Instead, please contact the NFIRS Support Center.


Solution

  • 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

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