![](/img/trans.png)
[英]C# asp.net core v. 2 self hosted how to upload image to subfolder under wwwroot?
[英]How can i get the path to an image file in wwwroot folder of an azure hosted asp .net core app inside a webjob
我正在使用aspcore控制台應用程序作為Webjob發送計划的電子郵件。 在這些電子郵件中,我需要在電子郵件標題中包含來自wwwroot文件夾的圖像。 如何獲取該URL的URL,以傳遞到電子郵件HTML中img標簽的src屬性?
我測試了txt文件,以獲取可以在HOME
環境下使用的文件路徑。 它指向Kudu上的D:\\home
。 因此,如果文件位於wwwroot中,則可以使用下面的代碼來獲取它。
Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\test.jpg"
下面是我的示例代碼。
string rootpath = null;
rootpath = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\test.jpg";
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("xxxxxxxxxx");
mailMessage.To.Add(new MailAddress("xxxxxxxxxxxx"));
mailMessage.Subject = "message image test";
mailMessage.IsBodyHtml = true;
string content = "If your mail client does not support HTML format, please switch to the 'Normal Text' view and you will see this content.";
mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(content, null, "text/plain"));
mailMessage.Body += "<br /><img src=\"cid:weblogo\">";
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, "text/html");
LinkedResource lrImage = new LinkedResource(rootpath, "image/jpg");
lrImage.ContentId = "weblogo";
htmlBody.LinkedResources.Add(lrImage);
mailMessage.AlternateViews.Add(htmlBody);
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("xxxxxxxxxx", "xxxxxxxxxx");
client.Send(mailMessage);
以下是我的郵件,我們可以檢查源代碼並找到src="cid:weblogo"
,這意味着圖片文件不是本地文件。
您可以使用我的代碼進行測試,希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.