在C#windows服务中为映像生成操作

在C#windows服务中为映像生成操作,c#,windows-services,C#,Windows Services,我正在使用一个包含许多不同项目的解决方案。我的项目特别负责发送电子邮件通知。我已经创建了一个EmailTemplate.cs,用于生成电子邮件。在这个EmailTemplate.cs中,我在发送电子邮件时使用了两个图像 此Windows服务可部署到不同的环境、PROD、STG和DEV。 我们的CI/CD也使用Jenkins 我遇到了一个场景,即图像没有正确加载,因为部署服务时,图像不是Jenkins文件的一部分,无法将图像包含到输出目录中 我知道我可以更新Jenkins文件以将图像复制到输出目录

我正在使用一个包含许多不同项目的解决方案。我的项目特别负责发送电子邮件通知。我已经创建了一个EmailTemplate.cs,用于生成电子邮件。在这个EmailTemplate.cs中,我在发送电子邮件时使用了两个图像

此Windows服务可部署到不同的环境、PROD、STG和DEV。 我们的CI/CD也使用Jenkins

我遇到了一个场景,即图像没有正确加载,因为部署服务时,图像不是Jenkins文件的一部分,无法将图像包含到输出目录中

我知道我可以更新Jenkins文件以将图像复制到输出目录,并将图像的构建操作更改为Content并复制到输出目录:始终复制。但我对这样做犹豫不决

我的问题是,在解决方案中包含图像的理想/最佳实践是什么

我读过关于图像嵌入资源的文章,但是所有的文章都过时了,我想知道什么最适合这个时代

我读过关于图像嵌入资源的文章,但是所有的文章都过时了,我想知道什么最适合这个时代

我已经将构建操作更改为“内容”,并将“复制到输出目录”更改为“始终”。这是可行的,但我不喜欢将图像复制到输出目录的想法

public struct EmailTemplate
    {
        public const string Html =
            @"
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset=""utf-8"" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>

</head>
<body>
    <table width = ""650px"" height=""550px"" style=""border-color: #3399ff; border-width:4px"" cellspacing=""0"" border=""0"">
        <tr style = ""border-style: hidden"" >
            <td align=""center"" style=""background-color:white"">
               <span style = ""font-size:25px;"" >
                    <span>
                        <img id=""mediaShuttleLogo""src=""mediashuttleLogo"">
                    </span>
                </span>
            </td>
        </tr>
        <tr align=""center"" style=""border-style: hidden"">
            <td>
                <br />
                <span style=""color: #3399ff; font-family:Calibri"" > RequesterEmail has sent you content via Media Shuttle.</span>
                <br />
                <span>
                    <img id = ""downloadIcon""
                         src=""downloadImage""
                         style='border-bottom-width:0in;border-left-width:0in;border-right-width:0in;border-top-width:0in;padding-bottom:5px;padding-left:6px'>
                </span>
                <div class=""top-rectangle""></div>
                <div class=""arrow-down""></div>
                <br />
                <a href = ""EmailDownloadLink"" id=""EmailDownloadLink"" >
                    Click here to download
                </a>
                <br />
                <br />
                <span style = ""font-size:15px; font-family:Calibri"" > Download link will expire after first download. </span>
            </td>
        </tr>
    </table>
</body>
</html>
";

    }
public结构电子邮件模板
{
公共常量字符串Html=
@"
表,th,td{
边框:1px纯黑;
}

RequesterEmail已通过Media Shuttle向您发送内容。



下载链接将在首次下载后过期。 "; }
对于电子邮件中的图像,它们实际上应该是指向internet上图像的链接。许多电子邮件客户端不显示图片,您必须“下载图片”

通过在线使用图像,您还可以通过跟踪图像的点击量来跟踪阅读电子邮件的人数

如果您正在构建Winforms/Service应用程序,我建议将副本作为输出或嵌入式资源

如果它是一个网站,你会将“压缩为web”的图像包含在网站中,或者将它们放在CDN上


对于移动应用程序,如果它是web上的HTML应用程序主机图像。如果是本机移动应用程序(不是照片共享),则将图像嵌入exe。

解决了这个问题,就能够将两个图像嵌入exe

<EmbeddedResource Include="MediaShuttleEmail\Images\email-download-button.png" />
<EmbeddedResource Include="MediaShuttleEmail\Images\mediashuttle-logo.png" />
然后我需要使用LinkedResource从映像路径创建映像资源:

string htmlBody = MailText;
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");

            // create image resource from image path using LinkedResource 
            // LinkedResource class represents an embedded external resource in email, ex: a image
            LinkedResource msLogoImageResource = new LinkedResource(GetMSLogoAssembly(), contentType)
            {
                ContentId = "msLogoImage",
                TransferEncoding = System.Net.Mime.TransferEncoding.Base64
            };
            LinkedResource downloadArrowImageResource = new LinkedResource(GetDownloadArrowAssembly(), contentType)
            {
                ContentId = "downloadArrowImage",
                TransferEncoding = System.Net.Mime.TransferEncoding.Base64
            };

            // adding the images linked to htmlView...
            htmlView.LinkedResources.Add(msLogoImageResource);
            htmlView.LinkedResources.Add(downloadArrowImageResource);

            string subjectMessage = $"Content available for download at MediaShuttle. {subject}";
            MailMessage emailMessage = new MailMessage(From, To)
            {
                Subject = subjectMessage,
                Body = MailText,
                Priority = MailPriority.Normal,
                IsBodyHtml = true
            };

            emailMessage.AlternateViews.Add(htmlView);

            SmtpClient smtpClient = new SmtpClient(emailServer);
            smtpClient.Send(emailMessage);

完成所有这些之后,我不再需要担心在部署时是否将映像复制到任何目录

谢谢Jeremy,是的,这是一项部署到不同环境的服务。我意识到我可以复制到输出或嵌入式资源,但我想知道什么是最好的,这样当Jenkins构建所有DLL/exe时,我就不必担心映像是否被复制了,而只需要担心DLL/exe。或者这种情况下的最佳做法是什么。如果您要复制到输出,请在Jenkins作业中复制bin文件夹中的所有内容,或者如果您只想复制DLL/exe,请嵌入资源。这取决于你,但作为一个经验法则,不要用巨大的图像膨胀你的exe,在这种情况下,不要把它们作为嵌入式资源。
string htmlBody = MailText;
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");

            // create image resource from image path using LinkedResource 
            // LinkedResource class represents an embedded external resource in email, ex: a image
            LinkedResource msLogoImageResource = new LinkedResource(GetMSLogoAssembly(), contentType)
            {
                ContentId = "msLogoImage",
                TransferEncoding = System.Net.Mime.TransferEncoding.Base64
            };
            LinkedResource downloadArrowImageResource = new LinkedResource(GetDownloadArrowAssembly(), contentType)
            {
                ContentId = "downloadArrowImage",
                TransferEncoding = System.Net.Mime.TransferEncoding.Base64
            };

            // adding the images linked to htmlView...
            htmlView.LinkedResources.Add(msLogoImageResource);
            htmlView.LinkedResources.Add(downloadArrowImageResource);

            string subjectMessage = $"Content available for download at MediaShuttle. {subject}";
            MailMessage emailMessage = new MailMessage(From, To)
            {
                Subject = subjectMessage,
                Body = MailText,
                Priority = MailPriority.Normal,
                IsBodyHtml = true
            };

            emailMessage.AlternateViews.Add(htmlView);

            SmtpClient smtpClient = new SmtpClient(emailServer);
            smtpClient.Send(emailMessage);