Send email with attachment from a specific url in C#

后端 未结 2 1722
-上瘾入骨i
-上瘾入骨i 2021-01-23 05:26

In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.

相关标签:
2条回答
  • 2021-01-23 06:16

    Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:

    dynamic email = new Email("Example");
    email.Attach(new Attachment("c:\\attachment.txt"));
    email.Send(); 
    

    Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC

    Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.

    0 讨论(0)
  • 2021-01-23 06:23

    If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:

    var client = new WebClient();
    
    // Download the PDF file from external site (pdfUrl) 
    // to your local file system (pdfLocalFileName)
    client.DownloadFile(pdfUrl, pdfLocalFileName);  
    

    Then you can use it on Attachment:

    attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
    msg.Attachments.Add(attachment)
    
    0 讨论(0)
提交回复
热议问题