[SOLVED] Send attachments with MailKIt which comes from a website

Issue

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?

Solution

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

Answered By – Nitin S

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *