Ammar
Ammar

Reputation: 693

How to attach file to a email through online URL in asp.net

i am using asp.net web forms. i have an file save on on line server. i want to send that file in attachment. i have URL of the file how i attach the file through URL as attachment in email. please guide me.

Thank you

Upvotes: 0

Views: 821

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37530

You're going to have to download it before attaching. Try something like this...

var request = (HttpWebRequest)WebRequest.Create(fileUrl);

using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        var message = new MailMessage("[email protected]", "[email protected]", "subject", "body");
        message.Attachments.Add(new Attachment(responseStream, "myfile.pdf"));
        var smtpClient = new SmtpClient();
        smtpClient.Send(message);
    }
}

Upvotes: 2

Related Questions