繁体   English   中英

我如何在Webjob内的Azure托管ASP .NET Core应用程序的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM