我怎样才能在 Azure 托管的 wwwroot 文件夹中获取图像文件的路径 asp .net 核心应用程序在 webjob 中

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 来发送预定的电子邮件。在这些电子邮件中,我需要在电子邮件 headers 中包含来自 wwwroot 文件夹的图像。我怎样才能将 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",说明图片文件不是本地文件

你可以用我的代码测试一下,希望对你有帮助。