Attaching a file to an iCalendar - c#

I have iCalendar meeting requests sending correctly via SMTP (using the code below), but when I attempt to attach a file, the file does not appear as part of the iCalendar. When saving out the .ics after opening it in outlook, the whole file data has been stripped out.
Here's the code I'm using:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new System.Net.Mail.MailAddress("test1#test.com", "test1");
msg.To.Add(new System.Net.Mail.MailAddress("test2#test.com", "test2"));
msg.Subject = "Subject1";
msg.Body = "Body line 1\r\nBody line 2\r\nBody line 3";
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("name", "meeting.ics");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("PRODID:-/Microsoft Corporation//Outlook 15.0 MIMEDIR//EN");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("METHOD:REQUEST");
sb.AppendLine("X-MS-OLK-FORCEINSPECTOROPEN:TRUE");
sb.AppendLine("BEGIN:VEVENT");
string file = "D:\\LoadedDate.xlsx";
string filename = Path.GetFileName(file);
sb.Append("ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=");
sb.Append(filename).Append(":").AppendLine(Convert.ToBase64String(File.ReadAllBytes(file), Base64FormattingOptions.InsertLineBreaks));
foreach (System.Net.Mail.MailAddress to in msg.To)
{
sb.AppendLine(String.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", String.IsNullOrEmpty(to.DisplayName) ? to.Address : to.DisplayName, to.Address));
}
sb.AppendLine("CLASS:PUBLIC");
sb.Append("CREATED:").AppendLine(DateTime.Now.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sb.Append("DESCRIPTION:").Append(msg.Body.Replace("\r\n", "\\n")).Append("\\n <<").Append(filename).AppendLine(">> \\n");
string dt = DateTime.Now.AddHours(1).ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z");
sb.AppendLine("DTSTART:" + dt);
sb.AppendLine("DTSTAMP:" + dt);
sb.AppendLine("DTEND:" + DateTime.Now.AddHours(5).ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sb.AppendLine("LAST-MODIFIED:");
sb.Append("LOCATION:").AppendLine("Location1");
sb.AppendLine(String.Format("ORGANIZER;CN=\"{0}\":mailto:{0}", msg.From.Address));
sb.AppendLine("PRIORITY:5");
sb.AppendLine("SEQUENCE:0");
sb.Append("SUMMARY;LANGUAGE=en-gb:").AppendLine(msg.Subject);
sb.AppendLine("TRANSP:OPAQUE");
// UID should be unique.
sb.Append("UID:").AppendLine(Guid.NewGuid().ToString());
sb.Append("X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\\n");
sb.Append("<HTML>\\n").Append("<HEAD>\\n").Append("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html\\; charset=iso-8859-1\">\\n").Append("<META NAME=\"Generator\" CONTENT=\"MS Exchange Server version 14.03.0162.000\">\\n");
sb.Append("<TITLE>").Append(msg.Subject).Append("</TITLE>\\n");
sb.Append("</HEAD>\\n").Append("<BODY>\\n").Append("<!--Converted from text/rtf format -->\\n\\n");
sb.Append("<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Calibri\">").Append(msg.Body.Replace("\r\n", "</FONT></SPAN></P>\\n\\n<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Calibri\">")).Append("</FONT></SPAN></P>\\n\\n");
sb.Append("<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Arial\" SIZE=2 COLOR=\"#000000\"> &lt\\;&lt\\;").Append(filename).Append("&gt\\;&gt\\; </FONT></SPAN></P>\\n\\n");
sb.Append("</BODY>\\n").AppendLine("</HTML>");
sb.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
sb.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1");
sb.AppendLine("X-MICROSOFT-DISALLOW-COUNTER:FALSE");
sb.AppendLine("X-MS-OLK-AUTOFILLLOCATION:FALSE");
sb.AppendLine("X-MS-OLK-AUTOSTARTCHECK:FALSE");
sb.AppendLine("X-MS-OLK-CONFTYPE:0");
sb.AppendFormat("X-MS-OLK-SENDER;CN=\"{0}\":mailto:{0}", msg.From.Address).AppendLine();
sb.AppendLine("STATUS:TENTATIVE");
sb.AppendLine("BEGIN:VALARM");
sb.AppendLine("TRIGGER:-PT15M");
sb.AppendLine("ACTION:DISPLAY");
sb.AppendLine("DESCRIPTION:Reminder");
sb.AppendLine("END:VALARM");
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
System.Net.Mail.AlternateView av = System.Net.Mail.AlternateView.CreateAlternateViewFromString(sb.ToString(), ct);
msg.AlternateViews.Add(av);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mailserver");
client.Send(msg);
I've had a look at the RFC for iCalendars (https://www.rfc-editor.org/rfc/rfc5545), and I think I've done everything according to what the spec says. I'm guessing that there is either a problem with the way the file is read in (the Convert.ToBase64String bit), or I'm missing something with the alternate view (I've seen other people adding multiple views).
Things I have tried:
Replacing the Convert.ToBase64String(File.ReadAllBytes(file),
Base64FormattingOptions.InsertLineBreaks) with
Convert.ToBase64String(File.ReadAllBytes(file),
Base64FormattingOptions.None).
Using System.Text.Encoding
to convert the file to BASE64 (without success).
Attaching files to the email directly (using the
MailMessage.Attachments), but that just makes the email appear as a
normal email.
I've also had a look at the DDay.iCal project on sourceforge (http://sourceforge.net/projects/dday-ical/), but I couldn't figure out how that worked when it came to attaching a file.
One requirement I have for this is that the file has to be embedded / attached to the email, I cannot add it as a URI unfortunately.
Can anyone help?
Update: Following arnaudq's advice, I have implemented wrapping the lines at 75 characters as mentioned in the RFC. The resulting MIME message looks like the following:
BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 15.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=test.txt:U0ZMb2dObwlTRkxvYWR
lZERhdGUNCjkxNzY3NC8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjIwNS8xCTI3LzExLzIwMTIgM
Tg6MzANCjkxMjI0Ni8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjI1Mi8xCTI3LzExLzIwMTIgMTg
6MzANCjkxMjQyMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjQyMi8xCTI3LzExLzIwMTIgMTg6M
zANCjkxNTMyMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTQzNS8xCTI3LzExLzIwMTIgMTg6MzA
NCjkxNTU5OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNjc3NC8xCTI3LzExLzIwMTIgMTg6MzANC
jkxNjk1OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNjk2MC8xCTI3LzExLzIwMTIgMTg6MzANCjk
xNzM2Ny8xCTI3LzExLzIwMTIgMTg6MzANCjkxNzQzNC8xCTI3LzExLzIwMTIgMTg6MzANCjkxN
DczMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDczMi8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc
0My8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0NC8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0N
S8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0Ni8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2MS8
xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2Mi8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2My8xC
TI3LzExLzIwMTIgMTg6MzANCjkxNTYzNS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTYzOC8xCTI
3LzExLzIwMTIgMTg6MzANCjkxNTY0MC8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTY0MS8xCTI3L
zExLzIwMTIgMTg6MzANCjkxNTY1OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTc3Ni8xCTI3LzE
xLzIwMTIgMTg6MzANCjkxNTc3Ny8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTc3OC8xCTI3LzExL
zIwMTIgMTg6MzANCg==
ATTENDEE;CN="Test 1";RSVP=TRUE:mailto:test1#test.com
CLASS:PUBLIC
CREATED:20150318T095735Z
DESCRIPTION:Body line 1
Body line 2
Body line 3
<<test.txt>>
DTSTART:20150318T105735Z
DTSTAMP:20150318T105735Z
DTEND:20150318T145735Z
LAST-MODIFIED:
LOCATION:Location1
ORGANIZER;CN="test2#test.com":mailto:test2#test.com
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=en-gb:Subject1
TRANSP:OPAQUE
UID:40306717-c29a-42d1-b03e-0240a93c2ea2
X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//E
N"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html\; charse
t=iso-8859-1"><META NAME="Generator" CONTENT="MS Exchange Server version 1
4.03.0162.000"><TITLE>Subject1</TITLE></HEAD><BODY><!--Converted from text
/rtf format --><P DIR=LTR><SPAN LANG="en-gb"><FONT FACE="Calibri">Body lin
e 1</FONT></SPAN></P><P DIR=LTR><SPAN LANG="en-gb"><FONT FACE="Calibri"></
FONT></SPAN></P>Body line 2</FONT></SPAN></P><P DIR=LTR><SPAN LANG="en-gb"
><FONT FACE="Calibri"></FONT></SPAN></P>Body line 3<P DIR=LTR><SPAN LANG="
en-gb"><FONT FACE="Arial" SIZE=2 COLOR="#000000"> &lt\;&lt\;test.txt&gt\;&
gt\; </FONT></SPAN></P></BODY></HTML>
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-DISALLOW-COUNTER:FALSE
X-MS-OLK-AUTOFILLLOCATION:FALSE
X-MS-OLK-AUTOSTARTCHECK:FALSE
X-MS-OLK-CONFTYPE:0
STATUS:TENTATIVE
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
Unfortunately, this still doesn't work and the file (in this case a simple plain text file) does not come through with the calendar entry in Outlook.
What's really interesting is that saving the above MIME message to a file manually and renaming to a .ics then opening it does display the attached file correctly. This makes me think that there is something wrong with the way I'm sending the message, instead of the iCalendar markup.
Any ideas what is wrong?

#paul, I made the following changes and its working fine for me. I need to verify this fix on all email clients. I tested on MS Outlook 2013, ios, MS Outlook 2010 and its working fine.
MailMessage msg = new MailMessage();
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
Stream stream = new MemoryStream(attachment.Bytes);// Bytes of file
LinkedResource resource = new LinkedResource(stream);
resource.ContentId = attachment.Name.Replace(".", "") + DateTime.Now.Ticks.ToString();
resource.ContentType.Name = attachment.Name;//Name of file
resource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
alternate.LinkedResources.Add(resource);
msg.AlternateViews.Add(alternate);
I am not modifying .ics file to add ATTACH property(ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=)

String iCall = CreateICal();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("charset", #"utf-8");
ct.Parameters.Add("method", "REQUEST");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(iCall, ct);
System.Net.Mime.ContentType cthtml = new System.Net.Mime.ContentType("text/html");
cthtml.Parameters.Add("charset", #"utf-8");
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(this.mHTML, cthtml);
mail.AlternateViews.Add(avHtml);
mail.AlternateViews.Add(avCal);
foreach (LinkedResource resource in arrattach)
{
avHtml.LinkedResources.Add(resource);
}
client.Send(mail);

First, while you do make use of line breaks, it looks like you are not using the kind of line breaks that iCalendar expects. In short, each line after the first one should be prefixed with a space character and the lines should be less than 75 octets in length. See https://www.rfc-editor.org/rfc/rfc5545#section-3.1
(In general, for this type of interop issue, showing us the end result MIME message is more useful than the code that was used to generate it)
Then, as far as I remember, Outlook prefers attachments to be transmitted in a multipart/related containing the iCalendar stream and the attachment in different mime parts. See https://www.rfc-editor.org/rfc/rfc6047#section-4.6 for an example.
Finally, you might want to try sending an invitation with attachment from Outlook and see how the MIME message that it does send is structured.

Related

c# How to implement disposition type (Inline //or Attachment) for PostMark provider

SMTP Provider C# Code:
MailMessage Mail;
Mail.Attachments.Clear();
Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strICSData);
var ms = new MemoryStream(bytes);
var a = new Attachment(ms, "meeting111.ics", "text/calendar");
a.ContentDisposition.Inline = true;
Mail.Attachments.Add(a);
Here
a.ContentDisposition.Inline
Gets or sets a System.Boolean value that determines the disposition type (Inline or Attachment) for an e-mail attachment.
Above code is working fine and mapping my meeting to outlook calendar as shown below.
Find smtp screen shot after sending mail :
Postmark Provider C# Code:
I am also using postmark provider for mail but i did not find any a.ContentDisposition.Inline = true; functionality.
Please find the below Postmark code :
PostmarkMessage message;
message.Attachments.Clear();
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strICSData);
var ms = new MemoryStream(bytes);
message.AddAttachment(ms, "meeting111.ics", "text/calendar");
Below line, Adds a file stream with inline support:
message.AddAttachment(ms, "meeting.ics", "text/calendar");
Can anybody provide me solution so that postmark attachment is going to map to outlook calendar.
In order to get better outlook compatibility through our API, you'll need to set the ContentID for the attachment like the following:
attachment.ContentID = "cid:meeting.ics"
So we have to change
message.AddAttachment(ms, "meeting.ics", "text/calendar");
TO
message.AddAttachment(ms, "meeting.ics", "text/calendar","cid:meeting.ics");

How can I send image in subject line in Gmail?

I'm trying to send an email and it should display subject line like below.
I tried to put image in subject line but it won't work.
I also googling but unable to find any solution.
Is anyone know how to do it ?
Thank you.
That's an Emoji.
With the coolness of emoji, a new markting boom is adding emojis to the subject lines of email.
It´s not possible to add your own custom images to the subject line.
https://www.campaignmonitor.com/resources/guides/using-emojis-and-symbols-in-email-marketing/
Creating an email message using UTF-8
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bodyencoding%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
List of unicode Emojis
http://unicode.org/emoji/charts/full-emoji-list.html
As you know by now, you're referencing an emoji. But I just want to share that my own experience using emojis in C# for sending gmail does not require any conversion to UTF8.
In the link provided by Webbanditten, this is presented for the sake of producing some arrows:
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test email message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
I don't know if its because I'm working on a .cs file that's saved as UTF-8 already, but I can just cut and paste emojis right in and don't have do deal with SubjectEncoding or with character codes.
A modified version of the code below runs successfully and both notepad and visual studio display the actual emoji in the code so it's 'readable'.
var hasIssues = true;
var emoji = hasIssues ? "😡" : "👍";
using (var client = new SmtpClient("host"))
using (var mail = new MailMessage()) {
mail.From = new MailAddress("name#from.com");
mail.To.Add("name#to.com);
mail.Subject = $"{emoji} Emoji Test";
mail.Body = "Did it go well? Check the emoji on the subject line";
client.Send(mail);
}

When when sending an email of zip file size 19mb its not sending the email?

This is in my new class top:
MailMessage photosmessage;
This is the method i have in my new class:
public void SendPhotos(string fileNameToSend)
{
try
{
MailAddress from = new MailAddress("chocolade#gmail.com", "User " + (char)0xD8 + " Name",
System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("MyEimalOfMyInternet");
photosmessage = new MailMessage(from, to);
photosmessage.Body = "Please check the log file attachment i have some bugs.";
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
photosmessage.Body += Environment.NewLine + someArrows;
photosmessage.BodyEncoding = System.Text.Encoding.UTF8;
photosmessage.Subject = "Log File For Checking Bugs" + someArrows;
photosmessage.SubjectEncoding = System.Text.Encoding.UTF8;
Attachment myAttachment = new Attachment(fileNameToSend, MediaTypeNames.Application.Octet);
photosmessage.Attachments.Add(myAttachment);
SmtpClient docsend = new SmtpClient("smtp.gmail.com", 587);
docsend.SendCompleted += new SendCompletedEventHandler(docsend_SendCompleted);
docsend.EnableSsl = true;
docsend.Timeout = 10000;
docsend.DeliveryMethod = SmtpDeliveryMethod.Network;
docsend.UseDefaultCredentials = false;
docsend.Credentials = new NetworkCredential("gmailusername", "gmailpassword");
string userState = "test message1";
docsend.SendAsync(photosmessage, userState);
SendLogFile.Enabled = false;
}
catch (Exception errors)
{
Logger.Write("Error sending message :" + errors);
}
}
Im using this method in Form1 like this:
se.SendPhotos(outputtext+"\\"+"textfiles.zip");
se.SendPhotos(outputphotos + "\\" + "photofiles.zip");
Firsrt time its sending zipped file of some text files inside the zip file is about 5kb
Sending the zip file no problems.
Then its sending a zip file of 19mb that inside there are some images/photos each photos about 7.55mb
This time the zip file never get to my email.
The first zip file of the text files i get it but the second one i never get it.
Im using my gmail email account to send this files to my regular isp email account.
I know in gmail you cant send more then 25mb but the zip file of the photos is 19mb
What else could be the reason i never get the second zip file ?
Edit:
I think i know what is the problem.
When getting and creating the zip of text file i did a filter ".txt" but when doing it with the photos zip file i did ".*" all the files:
string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
The result is i had a file with .ini in the zip file.
How can i filter for all images types ?
string[] photosfiles = Directory.GetFiles(s, "*.jpg", SearchOption.AllDirectories);
This will work for jpg files only but if i want also to get png or bmp ?
For the added question (in your Edit), you can use the following code to get all the files you want:
string[] extensions = {"*.bmp","*.jpg","*.png", "*.gif" };//add extensions you want to filter first
var filenames = extensions.SelectMany(x => Directory.GetFiles(s, x));
Hope it helps.
Try sending a simple text file to vet your solution even works. If that comes through, then your ISP may be filtering. Some ISP's have 5mb limits. Also make sure you only have pictures in those zips. If you have any exes etcs those can be blocked. Check your spam folder.
Really though, just make sure a text attachment goes through.
Do the attachments only contain photos and text files, Google will open up zip files and check to see if you're not attaching anything that could be potentially dangerous (.exe, .bat, etc.)
Or, you could be sending a zip file within of your zip files. (see here for a full list of what can't be sent over Gmail)
I would imagine in any case where you are violating a sending condition, the SMTP request would be implicitly rejected.
Is the photo zip file just photos? I tried looking online to see if Gmail does implement further restrictions on SMTP requests but couldn't find anything...

File are corrupted when Attaching them to MailMessage C#

I have created an application at work that generates exel files from some database data. After generating the files they are sent automatically to the customers in question. My problem is that it works fine when i run the published application. But some users when they run the application the files are generated perfectly as they are saved on the HDD and i can see them. But when they are attached to the MailMessage object they get corrupted. This is an image of the corrupted files. These files should be Excel files.
This is my code for sending a mail with attached files:
public void SendMailedFilesDK()
{
string[] sentFiles = Directory.GetFiles(sentFilesDK);
if (sentFiles.Count() > 0)
{
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("ares"))
{
using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
{
msg.From = new MailAddress("system#mail.dk");
msg.To.Add(new MailAddress("operation#mail.dk"));
msg.To.Add(new MailAddress("bl#mail.dk"));
msg.CC.Add("lmy#mail.dk");
msg.CC.Add("ltr#mail.dk");
msg.Subject = "IBM PUDO";
msg.Body = sentFiles.Count() + " attached file(s) has been sent to the customer(s) in question ";
msg.IsBodyHtml = true;
foreach (string file in sentFiles)
{
Attachment attachment = new Attachment(file);
msg.Attachments.Add(attachment);
}
client.Send(msg);
}
}
}
}
Why are the files getting corrupted when others run the application? We are all using office 2010.
You should make sure to set the content type of the attachement to the appropriate value.
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for xlsx files, or
application/vnd.ms-excel for xls files.
For example, your loop should look something like this.
ContentType xlsxContent = new ContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
foreach (string file in sentFiles)
{
Attachment attachment = new Attachment(file, xlsxContent);
msg.Attachments.Add(attachment);
}
We use this in our Attachment constructor and have no issues attaching Excel and PDF.
Attachment data = new Attachment(sFileName, MediaTypeNames.Application.Octet);
Also check that the users running this have permissions to access the files in whatever location is specified by sentFilesDK.
You might want to specify the mimetype which is part of one of the constructors on Attachment class.
public Attachment(string fileName, ContentType contentType);
You can also read the file in memorystream and pass it as part of the following constructor.
public Attachment(Stream contentStream, string name, string mediaType);

Encoding - issue displaying dutch characters

I want to dynamically create an email and send it. So far so good, the problem is that it's in dutch language and it doesn't display correctly.
I am doing this:
// Body Html
if (!string.IsNullOrEmpty(emailMessage.BodyHtml))
{
var encoding = Encoding.UTF32;
byte[] byteArray = Encoding.GetEncoding("iso-8859-1").GetBytes(emailMessage.BodyHtml);
byte[] unicodeArray = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), encoding, byteArray);
emailMessage.BodyHtml = encoding.GetString(unicodeArray);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType();
ct.MediaType = MediaTypeNames.Text.Html;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(emailMessage.BodyHtml, ct);
mailMessage.AlternateViews.Add(htmlView);
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
}
try
{
smtpSender.Send(mailMessage);
}
The mail should contain Financiële details but when i open the mail with outlook i see Financiele details
How to fix it?
Please have a look at the InternetCodepage property of the Outlook mailitem. I had a similar issue with a new e-Mail in which I inserted text from an existiing e-mail, in which German Umlauts didn't displaying correctly. This was solved after I set the InternetCodepage in the new e-mail to the appropriate value of the original e-mail.
See http://msdn.microsoft.com/en-us/library/office/ff860730.aspx for more information about this property and a list of possible values.

Categories