简体   繁体   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

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.

相关问题 C#asp.net核心v。2自托管如何将图片上传到wwwroot下的子文件夹? - C# asp.net core v. 2 self hosted how to upload image to subfolder under wwwroot? 文件上传到ASP.NET Core中的wwwroot文件夹 - File upload to wwwroot folder in ASP.NET Core 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC 从 ASP.Net Core 中的 wwwroot/images 获取图像 - Get image from wwwroot/images in ASP.Net Core Azure webjob 如何访问托管站点文件夹 - Azure webjob how to access hosted site folder ASP.NET MVC 核心 Razor wwwroot 东西不工作/存在于托管在 Nginx 上的应用程序中 - ASP.NET MVC Core Razor wwwroot things not working/exists in app which is hosted on Nginx 如何通过浏览文件夹在 asp.net mvc 应用程序中获取文件夹路径 - How can i get folder path in asp.net mvc app by browsing folders 如何使用 .net 核心 2.x 在 azure 网络作业上获得应用程序洞察力? - How to get app insights working on an azure webjob with .net core 2.x? ASP.NET Core 托管在 Windows 服务中 - 未提供静态文件(即 /wwwroot 的内容) - ASP.NET Core hosted in a Windows Service - static files not being served (i.e. contents of /wwwroot)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM