在我看来,用户可以搜索一个文档,一旦他们得到结果,他们可以点击它的id,他们可以根据id从特定的url下载文档:http://test.com/a.ashx?format=pdf&id= {0}
例如,如果id是10,那么下载文件的url就是:http://test.com/a.ashx?format=pdf&id=10 ,当用户点击它时,他们可以下载文档。
在我看来是这样的:
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>
}
下面是我的 SendEmail Controller 操作。
我可以向用户发送电子邮件。但是我在发送附件时遇到问题。 我的问题是:如何将 url 附带的文档附加到电子邮件中?
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;
}
最佳答案
如果 PDF 文件是在外部站点上生成的,您将需要以某种方式下载它,为此您可以使用 WebClient :
var client = new WebClient();
// Download the PDF file from external site (pdfUrl)
// to your local file system (pdfLocalFileName)
client.DownloadFile(pdfUrl, pdfLocalFileName);
然后你就可以在Attachment上使用它了:
attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)
关于c# - 从 C# 中的特定 url 发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357987/