0

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.

Community
  • 1
  • 1
RobertJRu
  • 35
  • 6
  • So this "button4" is giving you a list of messages that meet the criteria, right? And you'd like it to also download the file...or what? I'm not quite following what all you want to get out of that one click. – DonBoitnott Oct 03 '17 at 17:54
  • @DonBoitnott yes it is putting it in the list for now, but the list is irrelevant and may be removed later, every single message that meets that criteria will have 1 zip link and I would like it to download them all automatically – RobertJRu Oct 03 '17 at 19:10
  • Unfortunately, this will not be trivial. The link appears to be in the body, so it will require parsing the full text of the body, identifying the link, and extracting the full web address. Feed those addresses into [something like this](https://stackoverflow.com/questions/525364/how-to-download-a-file-from-a-website-in-c-sharp#525372) to get the download and save it. – DonBoitnott Oct 03 '17 at 19:44
  • great thank you, I will try that – RobertJRu Oct 03 '17 at 20:05

1 Answers1

0

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");
    }
RobertJRu
  • 35
  • 6