2

I have an API which gives me path to a file http://bts.myurl.com/ThisIsMyPdf.pdf. Now I have a button which onto clicking share this file to users via mail. This is the code which i have used for sending the report:

var filePath = "http://bts.myurl.com/ThisIsMyPdf.pdf";  
Utilities.SendEmail("MyId@gmail.com", "subject", "To@gmail.com", "", "", "body", filePath);

But this is giving exception as URI Formats are not supported.

Some other approaches include the file to be download first before sending as attachment but again i don't want it to be downloaded.

I believe there are other ways to achieve this, if any please share.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
vndpal
  • 697
  • 6
  • 20
  • If you don't want to download it before you send it, your best option is to include the link in the mail body and have recipient users download it themselves. of course this makes sense only if the resource is reasonably long-lived, and they can be expected to have access to this URL. – Cee McSharpface Jul 26 '18 at 07:34
  • Yes, this was my first response but as you correctly mentioned the link is only for internal use and others don't have access to it. that's why i'm looking for attachment. – vndpal Jul 26 '18 at 07:38
  • you won't get around downloading it then, but it should at least be possible to do that on the fly, without persisting to a temporary file on disk (streaming, combine [this answer](https://stackoverflow.com/a/5336414/1132334) with [this one](https://stackoverflow.com/a/30599168/1132334)) – Cee McSharpface Jul 26 '18 at 08:23
  • The above approach has solved my problem after some minor modifications. There should be an option to mark the comment as accepted answers. :) – vndpal Jul 26 '18 at 11:20

1 Answers1

2

As suggested by @dlatikay, hereby sharing a working code for the above problem.

//This  is the code get byte stream from the URL    
WebClient myClient = new WebClient();
        byte[] bytes = myClient.DownloadData("http://www.examle.com/mypdf.pdf");
        System.IO.MemoryStream webPdf = new MemoryStream(bytes);

//To Create the Attachment for sending mail.
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
            Attachment attach = new Attachment(webPdf, ct);
            attach.ContentDisposition.FileName = "myFile.pdf";

            var smtp = new SmtpClient
            {
                Host = "email.domainName.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
 //Here webpdf is the bytestream which is going to attach in the mail
                message.Attachments.Add(new Attachment(webPdf, "sample.pdf"));
                smtp.Send(message);
            }
            webPdf.Dispose();
            webPdf.Close();
vndpal
  • 697
  • 6
  • 20