Send email with attachment from a specific url in C#

烂漫一生 提交于 2019-12-02 09:28:52

问题


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.com/a.ashx?format=pdf&id={0}

For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.

This is how it looks like in my view:

 foreach (var item in Model)
        {

                <td>
                    <a href=@string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
                        @Html.DisplayFor(modelItem => item.id)
                    </a>
                </td>
        }

And below is my controller action for SendEmail.

I am able to send email to the user. But i am having problem with sending attachments. My question is: how can i attach the document that comes with the url to the email?

 public static bool SendEmail(string SentTo, string Text, string cc)
    {

        MailMessage msg = new MailMessage();

        msg.From = new MailAddress("test@test.com");
        msg.To.Add(SentTo);
        msg.CC.Add(cc);
        msg.Subject = "test";
        msg.Body = Text;
        msg.IsBodyHtml = true;

        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment(???);
        msg.Attachments.Add(attachment);

        SmtpClient client = new SmtpClient("mysmtp.test.com", 25);

        client.UseDefaultCredentials = false;
        client.EnableSsl = false;
        client.Credentials = new NetworkCredential("test", "test");
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //client.EnableSsl = true;

        try
        {
            client.Send(msg);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/26357987/send-email-with-attachment-from-a-specific-url-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!