Home > Blockchain >  Send attachments with MailKIt which comes from a website
Send attachments with MailKIt which comes from a website

Time:02-21

I´m using asp.net with c#

I send emails with MailKit including pdf attachments. These pdf files are at the moment on my local computer. This works so far without any problems. For the local files I specify the path like this.

builder.Attachments.Add(@"C:\files\06\attachment.pdf");

Now I have the problem that the pdf attachments have to be loaded directly from a website.

i.e.: https://website.com/files/06/atachment.pdf

Is this possible with Mailkit? How do I specify the path?

CodePudding user response:

Include this namespace

using System.Net;

Download your files to a path using WebClient class

using (WebClient wc = new WebClient())
{
    wc.DownloadFile (
        // Param1 = Link of file
        new System.Uri("https://website.com/files/06/atachment.pdf"),
        // Param2 = Path to save
        @"D:\Images\atachment.pdf"
    );
}

and then attach as usual

builder.Attachments.Add(@"D:\Images\atachment.pdf");
  • Related