C# 带有数字签名和嵌入图像的电子邮件
我有以下发送数字签名电子邮件的工作代码。我必须插入一个带有gif徽标的基于html的签名,该签名应该从程序集资源中提取。我在谷歌上搜索了一下,找到了C# 带有数字签名和嵌入图像的电子邮件,c#,email,embedded-resource,digital-signature,C#,Email,Embedded Resource,Digital Signature,我有以下发送数字签名电子邮件的工作代码。我必须插入一个带有gif徽标的基于html的签名,该签名应该从程序集资源中提取。我在谷歌上搜索了一下,找到了Convert.ToBase64String()作为可能的解决方案,但Outlook没有显示图像。 第二种方法是使用LinkedResource和AlternateView来嵌入我的图像,但我实际上无法使用下面的代码。我已经有了一个AlternateView来发送带有数字签名的电子邮件。是否也可以以某种方式添加图像 Somailer(to,from,
Convert.ToBase64String()
作为可能的解决方案,但Outlook没有显示图像。
第二种方法是使用LinkedResource
和AlternateView
来嵌入我的图像,但我实际上无法使用下面的代码。我已经有了一个AlternateView
来发送带有数字签名的电子邮件。是否也可以以某种方式添加图像
Somailer(to,from,from_name,relay,subject,body,cc1,cc2)代码>
编辑:我必须重新打开此线程,因为以前接受的答案不够好。我确实认为不可能使用System.Net.Mail
对整个邮件进行签名。但这也可能对您有所帮助:-
同时检查以下内容:-():-
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用电子邮件//添加EASendMail命名空间
命名空间mysendemail
{
班级计划
{
静态void Main(字符串[]参数)
{
SmtpMail-oMail=新的SmtpMail(“TryIt”);
SmtpClient oSmtp=新的SmtpClient();
//设置发件人电子邮件地址,请将其更改为您的
oMail.From=”test@emailarchitect.net";
//设置收件人电子邮件地址,请将其更改为您的
oMail.To=”support@emailarchitect.net";
//设置电子邮件主题
oMail.Subject=“测试带有附件的html电子邮件”;
//您的SMTP服务器地址
SmtpServer oServer=新的SmtpServer(“smtp.emailarchitect.net”);
//ESMTP身份验证的用户和密码(如果服务器不需要)
//用户身份验证,请删除以下代码。
oServer.User=”test@emailarchitect.net";
oServer.Password=“testpassword”;
//如果您的SMTP服务器需要SSL连接,请添加此行
//oServer.ConnectType=SmtpConnectType.ConnectSSLAuto;
尝试
{
//导入html正文,并将链接图像作为嵌入图像导入。
oMail.ImportHtml(“测试导入TML”,
“c:\\my picture”//test.gif位于c:\\my picture中
ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCS);
Console.WriteLine(“开始发送带有嵌入图像的电子邮件…”);
SendMail(oServer,oMail);
Console.WriteLine(“电子邮件已成功发送!”);
}
捕获(异常ep)
{
Console.WriteLine(“发送电子邮件失败,错误如下:”);
控制台写入线(ep消息);
}
}
}
}
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
}
}
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);
}
}
}
}