[英]Send email with attachment from a specific url in C#
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,然后可以基于ID从特定的URL下载该文档: 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. 例如,如果id为10,则用于下载文档的URL将为: http : //test.com/a.ashx?format=pdf& id=10,当用户单击它时,他们便可以下载该文档。
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. 下面是我对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?
我的问题是:如何将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;
}
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 : 如果PDF文件是在外部站点上生成的,则需要以某种方式下载它,为此,您可以使用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上使用它:
attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)
Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. 考虑使用邮政它是ASP.Net MVC的开源库,使您可以非常轻松地发送电子邮件。 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 另外,您可能想使用HangFire在后台发送电子邮件,请查看使用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. 更新:为了获取PDF文件路径,可以使用Server.MapPath方法将虚拟路径转换为服务器上相应的物理目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.