简体   繁体   中英

How do I attach a remote file to an email in C#?

I am trying to send an email from C# (A WCF service to be more specific) that has an attachment. The attachment is not on the local file system, so it will have a path of "http://..." etc.

Currently, if I try to pass in the url, I get an error that the given path's format is not supported.

Attachment attachment;
            attachment = new Attachment("https://assets.conestogac.on.ca/wiki/gatewayprocess.png", MediaTypeNames.Application.Octet);
            message.Attachments.Add(attachment);

The server encountered an error processing the request. The exception message is 'The given path's format is not supported.'. See server logs for more details.

How would I go about attaching a remote file as an email attachment?

您需要使用HttpClient或HttpWebRequest to download the remote file to a Stream`,然后附加下载的数据。

Try something like that:

var tempFileName = @"c:\tempFolder\gatewayprocess.png";        
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile("https://assets.conestogac.on.ca/wiki/gatewayprocess.png", tempFileName);
message.Attachments.Add(new Attachment(tempFileName));

maybe you can try the following code:

it will add the attachment as an inline attachment


  string attachmentPath = Environment.CurrentDirectory + @"\test.png";
  Attachment inline = new Attachment(attachmentPath);
  inline.ContentDisposition.Inline = true;
  inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
  inline.ContentId = contentID;
  inline.ContentType.MediaType = "image/png";
  inline.ContentType.Name = Path.GetFileName(attachmentPath);

  message.Attachments.Add(inline);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM