Does anyone have an example of being able to send an email with an attachment where the attachment is saved as utf8 encoding. I have tried but when i open it in notepad it says the encoding is ascii. Note: that i dont want to save the file first.
// Init the smtp client and set the network credentials
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = getParameters("MailBoxHost");
// Create MailMessage
MailMessage message = new MailMessage("team@ccccc.co.nz",toAddress,subject, body);
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] contentAsBytes = Encoding.UTF8.GetBytes(attachment);
memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
// Create attachment
ContentType contentType = new ContentType();
contentType.Name = attachementname;
contentType.CharSet = "UTF-8";
System.Text.Encoding inputEnc = System.Text.Encoding.UTF8;
Attachment attFile = new Attachment(memoryStream, contentType);
// Add the attachment
message.Attachments.Add(attFile);
// Send Mail via SmtpClient
smtpClient.Send(message);
}
Add the BOM (byte order mark) for UTF-8 at the beginning of the stream:
Code: