[英]How can i get the path to an image file in wwwroot folder of an azure hosted asp .net core app inside a webjob
I am using an aspcore console app as a Webjob to send scheduled emails. 我正在使用aspcore控制台应用程序作为Webjob发送计划的电子邮件。 In those emails i need to include an image from the wwwroot folder in the email headers.
在这些电子邮件中,我需要在电子邮件标题中包含来自wwwroot文件夹的图像。 How can i get the url to that to pass into the src attribute of my img tag in the email html?
如何获取该URL的URL,以传递到电子邮件HTML中img标签的src属性?
I test with txt file, to get the file path you could use HOME
environment. 我测试了txt文件,以获取可以在
HOME
环境下使用的文件路径。 It points to the D:\\home
on the Kudu. 它指向Kudu上的
D:\\home
。 So if your file is in wwwroot, you could use the below code to get it. 因此,如果文件位于wwwroot中,则可以使用下面的代码来获取它。
Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\test.jpg"
And the below is my sample code. 下面是我的示例代码。
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);
The below is my mail, and we could check the source code and find src="cid:weblogo"
, this means the picture file is not a local file. 以下是我的邮件,我们可以检查源代码并找到
src="cid:weblogo"
,这意味着图片文件不是本地文件。
You could have a test with my code, hope this could help you. 您可以使用我的代码进行测试,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.