编程语言
首页 > 编程语言> > c# – 带有数字签名和嵌入图像的电子邮件

c# – 带有数字签名和嵌入图像的电子邮件

作者:互联网

我有以下工作代码来发送经过数字签名的电子邮件.我必须插入一个带有gif徽标的基于html的签名,该徽标应该从程序集资源中提取.我google了一下,发现Convert.ToBase64String()作为可能的解决方案,但Outlook不显示图像.
第二种方法是使用AlternateView的LinkedResource来嵌入我的图像,但实际上我没有使用下面的代码.我已经有一个AlternateView来发送带有数字签名的电子邮件.是否有可能以某种方式添加图像?

邮件(to,from,from_name,relay,subject,body,cc1,cc2);.

private void mailer(string toaddress, string fromaddress, string fromaddress_disp, string relays, string mailsubject, string bodytext, string ccman, string cccct)
{
    string certname = "";

    MailAddress from = new MailAddress(fromaddress, fromaddress_disp);
    MailAddress to = new MailAddress(toaddress);
    MailAddress cc_man = new MailAddress(ccman);
    MailAddress cc_cct = new MailAddress(cccct);
    MailMessage message = new MailMessage(from, to);
    message.CC.Add(cc_man);
    message.CC.Add(cc_cct);
    message.Subject = mailsubject;
    message.IsBodyHtml = true;
    string body = "Content-Type: text/html; charset=iso-8859-1 \r\nContent-Transfer-Encoding: 8bit\r\n\r\n" + bodytext;
    byte[] messageData = Encoding.ASCII.GetBytes(body);
    ContentInfo content = new ContentInfo(messageData);

    SignedCms Cms = new SignedCms(new ContentInfo(messageData));
    X509Store store = new X509Store(StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadOnly);

    RSACryptoServiceProvider csp = null;
    X509Certificate2Collection certCollection = store.Certificates;
    X509Certificate2 cert = null;
    foreach (X509Certificate2 c in certCollection)
    {
        if ((c.Subject.Contains("myEmailAddress")) && (c.FriendlyName.Contains("CompanyEmailDigSig")))
        {
            cert = c;
            break;
        }
    }

    if (cert != null)
    {
            csp = (RSACryptoServiceProvider)cert.PrivateKey;
    }
    else
    {
        throw new Exception("Valid certificate was not found");
    }

    CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, cert);
    Cms.ComputeSignature(Signer);
    byte[] SignedBytes = Cms.Encode();
    MemoryStream signedStream = new MemoryStream(SignedBytes);
    AlternateView signedView = new AlternateView(signedStream, "application/pkcs7-mime; smime-type=signed-data; name=sig.p7m");
    message.AlternateViews.Add(signedView);
    SmtpClient client = new SmtpClient(relays);
    store.Close();

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
    //exception
    }
}

编辑:我不得不重新打开这个帖子,因为之前接受的答案还不够好.

解决方法:

我确实认为不可能使用System.Net.Mail对整个邮件进行签名.但这也可以帮助你: – Send Email in C# Tutorial – SSL, HTML, Embedded Image, S/MIME

另请查看:-( Send Email with Embedded Images – ImportHtml – Example): –

using System;
using System.Collections.Generic;
using System.Text;
using EASendMail; //add EASendMail namespace

namespace mysendemail
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            // Set sender email address, please change it to yours
            oMail.From = "test@emailarchitect.net";

            // Set recipient email address, please change it to yours
            oMail.To = "support@emailarchitect.net";

            // Set email subject
            oMail.Subject = "test html email with attachment";

            // Your SMTP server address
            SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

            // User and password for ESMTP authentication, if your server doesn't require
            // User authentication, please remove the following codes.            
            oServer.User = "test@emailarchitect.net";
            oServer.Password = "testpassword";

            // If your SMTP server requires SSL connection, please add this line
            // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            try
            {
               // Import html body and also import linked image as embedded images.
               oMail.ImportHtml( "<html><body>test <img src=\"test.gif\"> importhtml</body></html>",
                    "c:\\my picture", //test.gif is in c:\\my picture
                    ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCss );

                Console.WriteLine("start to send email with embedded image...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            }
        }
    }
}

标签:c,embedded-resource,digital-signature,email
来源: https://codeday.me/bug/20190709/1410852.html