在我看来,用户可以搜索文档,一旦他们获得结果,他们就可以点击其ID,他们可以根据id 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的控制器操作。
我可以向用户发送电子邮件。但我发送附件有问题。 我的问题是:如何将网址附带的文档附加到电子邮件中?
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;
}
答案 0 :(得分:2)
如果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)
答案 1 :(得分:1)
考虑使用Postal ASP.Net MVC的开源库,允许您轻松发送电子邮件。例如,要附加文件,您可以使用以下代码:
dynamic email = new Email("Example");
email.Attach(new Attachment("c:\\attachment.txt"));
email.Send();
另外,您可能希望使用HangFire在后台发送电子邮件,请查看Sending Mail in Background with ASP.NET MVC
更新:为了获取PDF文件路径,您可以使用Server.MapPath方法将虚拟路径转换为服务器上相应的物理目录。