当包含图像到MvCMailer邮件时,路径应该如何

本文关键字:路径 图像 包含 MvCMailer | 更新日期: 2023-09-27 18:18:27

我试图嵌入图像时发送电子邮件与MvCMailer像这样:

    Dictionary<string,string> resources = new Dictionary<string,string>();
    resources["logo"] = new Uri(new Uri(string.Format("{0}://{1}{2}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Authority, Url.Content("~"))), "/Content/images/logo.png").ToString();
    mailSender.Confirm(user.Username, user.Email, link,resources).Send();

它是如何产生这个的:

URI formats are not supported.

我不确定我应该发送什么样的路径,以及如何生成它?

堆栈跟踪:

     at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, ContentType contentType)
   at Mvc.Mailer.LinkedResourceProvider.Get(String contentId, String filePath)

当包含图像到MvCMailer邮件时,路径应该如何

资源路径应该是磁盘上的本地路径,而不是url。

resources["logo"] = Server.MapPath("~/Content/images/logo.png");

您必须添加代码来执行附件。如果这不是权限问题,请执行

public virtual MailMessage Welcome(string email, string name)
{     
   var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"};
   mailMessage.To.Add(email);
   ViewBag.Name = name;
   PopulateBody(mailMessage, viewName: "Welcome");
   return mailMessage; 
}