I want to send email using System.Web.Mail.MailMessage along with System.Web.Mail.SmtpMail class.
I know that this is a deprecated class but using System.Net.Mail is not an option for me.
So far I am able to send html formatted mail but I am not able to embed image to my email while sending.
This is what I've tried so far;
private static void SendMailMethod(string mailServer, int mailServerPort, string userName, string password)
{
const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
const string SMTP_SERVER_PORT =
"http://schemas.microsoft.com/cdo/configuration/smtpserverport";
const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";
const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";
const string SMTP_AUTHENTICATE =
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";
const string SEND_USERNAME =
"http://schemas.microsoft.com/cdo/configuration/sendusername";
const string SEND_PASSWORD =
"http://schemas.microsoft.com/cdo/configuration/sendpassword";
var mailMessage = new System.Web.Mail.MailMessage();
mailMessage.Fields[SMTP_SERVER] = mailServer;
mailMessage.Fields[SMTP_SERVER_PORT] = mailServerPort;
mailMessage.Fields[SEND_USING] = 2;
mailMessage.Fields[SMTP_USE_SSL] = false;
mailMessage.Fields[SMTP_AUTHENTICATE] = 0;
mailMessage.Fields[SEND_USERNAME] = userName;
mailMessage.Fields[SEND_PASSWORD] = password;
mailMessage.From = "abc#xyz.com";
mailMessage.To = "abc#xyz.com";
mailMessage.Subject = "Test mail:";
mailMessage.BodyFormat = MailFormat.Html;
var mailAttachment = new MailAttachment("E:\\imageToEmbed.jpg");
mailMessage.Attachments.Add(mailAttachment);
mailMessage.Attachments.Add(new MailAttachment("E:\\TestAttachmentFile.txt"));
var htmlBody =
"<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
"<html xmlns = \"http://www.w3.org/1999/xhtml\" >" +
" <head >" +
"<meta http - equiv = \"content-type\" content = \"text/html; charset=UTF-8\" />" +
"</head >" +
"<body style = \"font-family: Segoe UI; text-align:left;\" >" +
"Following is an embedded image:" +
"<br />" +
"<img alt = \"\" src = \"imageToEmbed.jpg\" />" +
"</body >" +
"</html >";
mailMessage.Body = htmlBody;
try
{
SmtpMail.Send(mailMessage);
Console.WriteLine("Mail sent");
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.ToString());
throw ex;
}
}
This code sends email but instead of showing image, the mail shows small square with a cross in it.
How can I embed this "imageToEmbed.jpg" to my email.
You can't achieve that with the classes System.Web.Mail because that implementation abstracts the access to a lot of features of the underlying Collaboration Data Objects component.
Instead of using the wrapper offered by System.Web.Mail.Message you better switch to using the CDO COM object directly. Add a reference to Microsoft CDO for Windows 2000 Library from the COM tab in the reference dialog.
After that you can use the following code to create and send an html email with embedded images:
var cdo = new CDO.Message();
// configuration
var cfg = cdo.Configuration;
cfg.Fields[SMTP_SERVER].Value = "smtp.server.com";
cfg.Fields[SMTP_SERVER_PORT].Value = 22;
cfg.Fields[SEND_USING].Value = 2;
cfg.Fields[SMTP_USE_SSL].Value = true;
cfg.Fields[SMTP_AUTHENTICATE].Value = 1;
cfg.Fields[SEND_USERNAME].Value = "user#example.com";
cfg.Fields[SEND_PASSWORD].Value = "password";
cfg.Fields.Update();
cdo.To = "awesome#example.com";
cdo.Sender = "me#example.com";
// attachment
var cdoatt = cdo.AddAttachment("file:///E:/imageToEmbed.jpg");
//this is why System.Web.Mail can't embed images
cdoatt.Fields["urn:schemas:mailheader:content-id"].Value = Guid.NewGuid().ToString("N");
cdoatt.Fields.Update();
// get a reference to out content-id field
var cid = cdoatt.Fields["urn:schemas:mailheader:content-id"];
// notice the special layout of SRC on the image tag,
//it will be somethong like CID:123456789abcdef
cdo.HTMLBody = #"<HTML><BODY><B>CDO</B><BR /> <IMG SRC=""cid:" + cid.Value + #"""/></BODY></HTML>";
cdo.Send();
Notice how you can set the Content-ID header here on the Attachment. That is needed so you can use the Content-ID in the SRC attribute of your image.
A typical mailmessage will look
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0001_01D1AF72.C8AE8C70"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384
This is a multi-part message in MIME format.
------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0002_01D1AF72.C8AE8C70"
------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
plain cdo
------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<HTML><BODY><B>plain cdo</B> <IMG SRC="cid:42"/></BODY></HTML>
------=_NextPart_001_0002_01D1AF72.C8AE8C70--
------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: image/png;
name="file.png"
Content-Transfer-Encoding: base64
Content-ID: <42>
Content-Disposition: attachment;
filename="file.png"
iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMA
Notice the Content-ID (here 42).
When using the plain System.Web.Mail classes your raw email will look like this:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0000_01D1AF70.59FC25F0"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384
This is a multi-part message in MIME format.
------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0001_01D1AF70.59FC25F0"
------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Following is an embedded image:
_____
test afterit
------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml" > <head ><meta http - equiv = "content-type" content = "text/html; charset=UTF-8" /></head ><body style = "font-family: Segoe UI; text-align:left;" ><i>Following is an embedded image:</i><br /><img alt = "" src ="<file:///file.png>"/><hr><b>test afterit</b></body ></html >
------=_NextPart_001_0001_01D1AF70.59FC25F0--
------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: image/png;
name="file.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="file.png"
iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMAAAAI0l
As you can see, there is no Content-ID header added and there is no code-path from the .Net implementation to add that header.
It might not find the image in the specified path or it might be a permission thing hence the broken image thingy. You need to check that first but if you would use base64 you will have the image in a string.
Try changing the image to a base64 link to a free online converter page.
This link might help as well c# embed base64 image for emailing.
Related
I'm trying to embed an image in an email that's send with Sendgrid C# 9.28.1. But somehow the image is not displayed. There is also not a function anymore that's called EmbedImage. Is there a way to do this?
var client = new SendGridClient(_settings.ApiKey);
var myMessage = new SendGridMessage();
myMessage.AddTo(entity.Email);
myMessage.From = new EmailAddress(_settings.FromAddress, _settings.FromName);
myMessage.Subject = $"{_settings.FromName} - Image TEST";
var body = "<div style=\"width:200px;\"><img src=\"cid::IMAGE01\"/></div>" +
#"<p>Test message?</p>";
var attachment = new SendGrid.Helpers.Mail.Attachment
{
ContentId = "IMAGE01",
Content = entity.Base64ImageString,
Type = "image/png",
Filename = "image.png"
};
myMessage.AddAttachment(attachment);
myMessage.HtmlContent = body;
await client.SendEmailAsync(myMessage);
I was able to replicate your issue. When running your code I see the image attached, but not inlined into the body of the email.
To resolve the issue, I added the Disposition property and set it to "inline".
message.AddAttachment(new Attachment
{
ContentId = "IMAGE01",
Content = Convert.ToBase64String(File.ReadAllBytes("image.png")),
Type = "image/png",
Filename = "image.png",
Disposition = "inline" // important!
});
Once I added the property, the image was displayed inline. (I also use a single : instead of two : in the HTML, so cid: instead of cid:: but not sure it matters.
Here's what part of my email source looked like without the Disposition property:
<html>
<body>
<img src=3D"cid:IMAGE01"/>
</body>
</html>
--d0e309773e8692891fcbf3a6dc18894582dea28d62adfe2bd8fd84a96b90
Content-Disposition: attachment; filename="image.png"
Content-Transfer-Encoding: base64
Content-Type: image/png; name="image.png"
Notice how IMAGE01 only appears in the HTML, but not in the attachment.
This is with the Disposition property:
<html>
<body>
<img src=3D"cid:IMAGE01"/>
</body>
</html>
--5bb4b7a345af921dc9094ee0d66495c2a390a8aa6e89c2a41d96f8af8af5
Content-Disposition: inline; filename="image.png"
Content-ID: <IMAGE01>
Content-Transfer-Encoding: base64
Content-Type: image/png; name="image.png"
Notice how there's now a Content-ID: <IMAGE01> parameter.
Having said that, there are also two alternatives, you can use a data URL to embed the base64 data into the HTML, or you can upload the image to the internet and reference it.
You can find the source code here: https://github.com/Swimburger/SendGridImages/blob/main/Program.cs
I'm using graph sdk from c# to send emails from a web app. The body is formatted as HTML. I'm attaching PDFs to the email. The recipients are receiving the email with winmail.dat and sometimes noname. It seems like the email is coming in as plain text. Gmail seems to able to unpack winmail.dat, but not all clients seem to do this.
Some research tells me to use MIME format, but having trouble finding a concise example.
How can I send emails from graph api / sdk formatted as HTML with MIME with attachments without creating winmail.dat
Here's a snip from my create message code:
private MimeMessage GetMimeMessage(string fromEmail, string subject, string htmlBody, IEnumerable<string> toRecipients, IEnumerable<string> ccRecipients, IEnumerable<string> attachments)
{
var message = new MimeMessage();
foreach (var recipient in toRecipients)
message.To.Add(MailboxAddress.Parse(recipient));
foreach (var recipient in ccRecipients)
message.Cc.Add(MailboxAddress.Parse(recipient));
message.From.Add(MailboxAddress.Parse(fromEmail));
message.Subject = subject;
var builder = new BodyBuilder();
builder.TextBody = htmlBody;
builder.HtmlBody = htmlBody;
foreach (var attachment in attachments)
builder.Attachments.Add(attachment);
message.Body = builder.ToMessageBody();
return message;
}
Send Code:
private async Task SendMimeMessageWithGraph(string sendFromEmail, MimeMessage mimeMessage)
{
var stream = new MemoryStream();
mimeMessage.WriteTo(stream);
stream.Position = 0;
StringContent MessagePost = new StringContent(Convert.ToBase64String(stream.ToArray()), Encoding.UTF8, "text/plain");
var message = new Message();
var mypost = await MessagePost.ReadAsStringAsync();
var sendMailRequest = _GraphClient.Users[sendFromEmail].SendMail(message, true).Request().GetHttpRequestMessage();
sendMailRequest.Content = MessagePost;
sendMailRequest.Method = HttpMethod.Post;
var sendResult = await _GraphClient.HttpProvider.SendAsync(sendMailRequest);
Debug.WriteLine(sendResult.StatusCode);
}
From: fromemail#fromemail.com
Date: Tue, 06 Sep 2022 21:39:47 -0400
Subject: Invoice #0000003
Message-Id: <2FWZXJIFTHU4.JPGM32IG50SI2#person1234>
To: Newperson#new.com
Cc: cc#cc.cc
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="=-9G8gCZS3S+XQhQ+1kLqUog=="
--=-9G8gCZS3S+XQhQ+1kLqUog==
Content-Type: text/plain; charset=utf-8
Please see attached invoice.<br><br>Thank you for your business,<br>Test<br>Person<br>222-999-1111<br>email#email.com<br><img height="50px" width="auto" src="data:image/svg+xml;base64,PD94b...>">
--=-9G8gCZS3S+XQhQ+1kLqUog==
Content-Type: text/html; charset=utf-8
Please see attached invoice.<br><br>Thank you for your business,<br>Test<br>Person<br>222-999-1111<br>email#email.com<br><img height="50px" width="auto" src="data:image/svg+xml;base64,PD94...">
--=-9G8gCZS3S+XQhQ+1kLqUog==--
I've followed this blog post:
https://gsexdev.blogspot.com/2021/08/sending-mimemessage-via-microsoft-graph.html
It partially works; emails received by clients without o365/outlook do not render the html email and include winmail.dat attachment. So, I still have the same issue.
In my case, I needed to disable TNEF formatting by disabling RTF. This is done through the online Exchange Admin Center:
Mail Flow
Remote domains
Choose 'Default'(or whatever you have set)
Edit text and character set
List item
Use rich-text format: Never.
Save.
Now both Graph SDK Messages and MIME messages send emails via graph as expected!
I have string data as CDATA Format. How can I convert this to Html or normal view text at C#? Should I use mimeKit or something else?
Received: from 172.19.76.148 (proxying for 85.105.234.193)
(InterKepWebMail authenticated user parkentegrasyon)
by kep.local with HTTP;
Mon, 29 Jan 2018 18:51:40 +0300
Content-Type: multipart/mixed;
boundary="------_=_NextPart_001_01F869E9.0A514C28"
Message-ID: <8ec68378-eca0-428d-a350-94427435a521.webmail#testkep.inter-kep.com.tr>
MIME-Version: 1.0
Date: Mon, 29 Jan 2018 18:51:40 +0300
From: "parkentegrasyon" <parkentegrasyon#testkep.inter-kep.com.tr>
To: <parkentegrasyon#testkep.inter-kep.com.tr>
Cc: <parkentegrasyon#testkep.inter-kep.com.tr>
Subject: =?utf-8?Q?=C3=96rnek_KEP_2018-01-29_18=3A51=3A41?=
User-Agent: InterKepWebMail/1.0.0
X-TR-REM-iletiTip: standart
X-TR-REM-iletiID:
--------_=_NextPart_001_01F869E9.0A514C28
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
<b>Merhaba D=C3=BCnya!</b>
--------_=_NextPart_001_01F869E9.0A514C28
Content-Type: application/octet-stream;
name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="test.txt"
dGVzdCBlaw==
--------_=_NextPart_001_01F869E9.0A514C28--
If you use MimeKit to parse the message, it will automatically decode the content (whether it be in base64 or quoted-printable).
In your example message, the text/html message body can be gotten like this:
var html = message.HtmlBody;
To get the decoded attachment content, you can do this:
foreach (var attachment in message.Attachments.OfType<MimePart> ()) {
using (var memory = new MemoryStream ()) {
attachment.Content.DecodeTo (memory);
var data = memory.ToArray ();
var text = Encoding.UTF8.GetString (data);
}
}
It's base64 encoded text. You can decode it like this
byte[] data = Convert.FromBase64String("dGVzdCBlaw==");
string decodedString = Encoding.UTF8.GetString(data);
Console.WriteLine(decodedString);
That prints 'Test ek'.
I am trying to send an email with embedded image(Not as attachment file). I am able to send mail.
I'm sending mail using following code:
internal static void Send(SmtpServerConfigurations configurations, EmailMessage emailMsg)
{
using (var mail = InitializeMailMessage(emailMsg))
using (var smtpClient = CreateSmtpClient(configurations))
smtpClient.Send(mail);
}
private static MailMessage InitializeMailMessage(EmailMessage emailMsg)
{
var mail = new MailMessage
{
From = new MailAddress(emailMsg.From),
Subject = emailMsg.Subject,
IsBodyHtml = emailMsg.IsBodyHtml
};
mail.To.Add(emailMsg.To);
AddMessageBody(emailMsg, mail);
return mail;
}
private static void AddMessageBody(EmailMessage emailMsg, MailMessage mail)
{
if (emailMsg.IsBodyHtml)
{
var body = GetHtmlBody(emailMsg.Body, emailMsg.EmbeddedImages);
mail.AlternateViews.Add(body);
}
else
mail.Body = emailMsg.Body;
}
private static AlternateView GetHtmlBody(string body, List<EmbeddedImage> embeddedImages)
{
var alternateView = AlternateView.CreateAlternateViewFromString(body, null,
MediaTypeNames.Text.Html);
if (embeddedImages == null) return alternateView;
foreach (var image in embeddedImages)
{
var imageToInline = new LinkedResource(image.Path, MediaTypeNames.Image.Jpeg);
imageToInline.ContentId = image.Id;
alternateView.LinkedResources.Add(imageToInline);
}
return alternateView;
}
private static SmtpClient CreateSmtpClient(SmtpServerConfigurations config)
{
var smtpClient = new SmtpClient(config.Host);
smtpClient.Port = config.PortNo;
if (config.IsAuthenticationRequired)
smtpClient.Credentials =
new NetworkCredential(config.Username, config.Password);
else
smtpClient.UseDefaultCredentials = true;
smtpClient.EnableSsl = false;
return smtpClient;
}
But the mail sent using above code is not in the format as I want.
What I want is;
MIME-Version: 1.0
From: x#y.com
To: a#b.com
Date: 11 Nov 2016 11:37:52 +0530
Subject: This is subject
Content-Type: multipart/related;
boundary=--boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d; type="text/html"
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "=
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xm=
lns =3D "http://www.w3.org/1999/xhtml" > <head ><meta http - equi=
v =3D "content-type" content =3D "text/html; charset=3DUTF-8" /><=
/head ><body style =3D"font-family: Segoe UI; text-align:left;" >=
This is body<br /><img alt =3D"" src =3D"cid:05393c56-15c1-4652-a=
31f-9cc513726bc0" height=3D"50" width=3D"50"/></body ></html >
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: image/jpeg name="filename.jpg" <<-----This is what I want.
Content-Transfer-Encoding: base64
Content-ID: <05393c56-15c1-4652-a31f-9cc513726bc0>
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMF
BwYHBwcGBwcI
.
.
.
/w20K7sPt8ul2st3/z0dd36Hj9K9I+HHwj8M6/rLaldaJp8l6y
kGRYgn6LgfpXve0pundwQmk9z//Z
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d--
What I am getting is;
MIME-Version: 1.0
From: x#y.com
To: a#b.com
Date: 11 Nov 2016 11:37:52 +0530
Subject: This is subject
Content-Type: multipart/related;
boundary=--boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d; type="text/html"
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "=
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xm=
lns =3D "http://www.w3.org/1999/xhtml" > <head ><meta http - equi=
v =3D "content-type" content =3D "text/html; charset=3DUTF-8" /><=
/head ><body style =3D"font-family: Segoe UI; text-align:left;" >=
This is body<br /><img alt =3D"" src =3D"cid:05393c56-15c1-4652-a=
31f-9cc513726bc0" height=3D"50" width=3D"50"/></body ></html >
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <05393c56-15c1-4652-a31f-9cc513726bc0>
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMF
BwYHBwcGBwcI
.
.
.
/w20K7sPt8ul2st3/z0dd36Hj9K9I+HHwj8M6/rLaldaJp8l6y
kGRYgn6LgfpXve0pundwQmk9z//Z
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d--
How can I achieve that custom "Name" header in Embedded image section of raw mail?
I want to add that header is because;
When I click on download button shown on image in Gmail inbox, I get "noname" file without extension. That downloaded file isn't useful unless user changes its extension to '.jpg/.jpeg'.
When I tried the same with another component(Which I don't have code for) strangely I was able to download that image with correct filename. Only difference between these two mails was "Name" header.
Please suggest me how to do this or any other way to achieve it.
This will do the trick for you
imageToInline.ContentType.Name = "ImageName.jpg";
I am trying to create a multipart/signed mime email following the RFC1847 protocol.
This is how it is supposed to look (part of the signature is removed):
Content-Type: multipart/signed; protocol="application/pkcs7-signature"
micalg=sha1; boundary="--PTBoundry=3"
----PTBoundry=3
Content-Type: multipart/mixed;
boundary="--PTBoundry=2"
----PTBoundry=2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
TEST AF signed
----PTBoundry=2
Content-Type: application/octet-stream;
name=test2.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=test2.txt
bWludGVzdGF0dGFjaG1lbnRzaWduZWQ=
----PTBoundry=2--
----PTBoundry=3
Content-Type: application/pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"
MIIKfAYJKoZIhvcNAQcCoIIKbTCCCmkCAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHAaCCCNow
ggPVMIICvaADAgECAgMCNtEwDQYJKoZIhvcNAQEFBQAwQjELMAkGA1UEBhMCVVMxFjAUBgNVBAoT
PrENekpgrYkz
----PTBoundry=3--
empty line
I cannot figure out how to actually send this as an email. I am using MailMessage and I have tried to add it as written below:
var stream = new MemoryStream(Encoding.ASCII.GetBytes(message));
var view = new AlternateView(stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
however it does not work. The MailMessage adds different headers and messes it all up.
How can I send this correctly?
I am no longer using the MailMessage class, because I could not get it working.
Instead I am using EWS managed api like this:
var mail = new EmailMessage(_service);
mail.Subject = filename;
mail.MimeContent = new MimeContent("us-ascii", Encoding.ASCII.GetBytes(messageContentWithHeaders));
mail.SendAndSaveCopy();