有沒有人有一個例子,能夠發送附件保存爲utf8編碼的附件的電子郵件。我試過,但是當我在記事本中打開它說編碼是ascii。注意:我不想先保存文件。電子郵件中的附件以UTF-8編碼保存
// Init the smtp client and set the network credentials
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = getParameters("MailBoxHost");
// Create MailMessage
MailMessage message = new MailMessage("[email protected]",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);
}