中的特定网址发送带有附件的

中的特定网址发送带有附件的

本文介绍了通过C#中的特定网址发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,用户可以搜索文档,一旦获得结果,就可以单击其ID,然后可以基于ID从 http://test.com/a.ashx?format=pdf&id= {0}

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}

例如,如果id为10,则用于下载文档的URL将为: http://test.com/a.ashx?format=pdf&id=10 ,当用户单击它时,便可以下载该文档.

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.

这是我的看法:

 foreach (var item in Model)
        {

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

以下是我对SendEmail的控制器操作.

And below is my controller action for SendEmail.

我能够向用户发送电子邮件.但是我在发送附件时遇到问题.我的问题是:如何将URL随附的文档附加到电子邮件中?

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("[email protected]");
        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;
    }

推荐答案

如果PDF文件是在外部站点上生成的,则需要以某种方式下载它,为此,您可以使用 WebClient :

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);

然后您可以在附件:

attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)

这篇关于通过C#中的特定网址发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:03