如何以编程方式单击电子邮件正文中的 .zip link
How to programmatically click a .zip link in an email message body
这可能非常简单,但我对编码非常陌生,提前抱歉。
目前我有一个按钮 4,它会通过我的收件箱读取具有特定主题的邮件,如果满足条件,它会首先在列表视图中显示邮件 class 属性,但我希望它也下载 link 在每封电子邮件中找到。
这是一个 .zip link,当从电子邮件中单击 link 时,它将下载 zip。我希望它自动下载单击 button4 时找到的所有 link。
我将展示我的 button4 代码,然后是电子邮件的示例。
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!!");
}
}
}
示例电子邮件:
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.
这基本上是 link @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");
}
这可能非常简单,但我对编码非常陌生,提前抱歉。
目前我有一个按钮 4,它会通过我的收件箱读取具有特定主题的邮件,如果满足条件,它会首先在列表视图中显示邮件 class 属性,但我希望它也下载 link 在每封电子邮件中找到。
这是一个 .zip link,当从电子邮件中单击 link 时,它将下载 zip。我希望它自动下载单击 button4 时找到的所有 link。
我将展示我的 button4 代码,然后是电子邮件的示例。
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!!");
}
}
}
示例电子邮件:
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.
这基本上是 link @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");
}