Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 要使用UTF-8编码保存的电子邮件中的附件_Asp.net_Email_Memory_Encoding_Utf 8 - Fatal编程技术网

Asp.net 要使用UTF-8编码保存的电子邮件中的附件

Asp.net 要使用UTF-8编码保存的电子邮件中的附件,asp.net,email,memory,encoding,utf-8,Asp.net,Email,Memory,Encoding,Utf 8,是否有人能够发送带有附件的电子邮件,其中附件保存为utf8编码。我试过了,但当我在记事本中打开它时,它说编码是ascii码。注意:我不想先保存文件 // Init the smtp client and set the network credentials SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = getParameters("MailBoxHost");

是否有人能够发送带有附件的电子邮件,其中附件保存为utf8编码。我试过了,但当我在记事本中打开它时,它说编码是ascii码。注意:我不想先保存文件

// 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);


            }
在流的开头添加UTF-8的代码:

0xEF,0xBB,0xBF
代码:


假设您的附件是文本,
ContentType
类的默认构造函数将附件的
Content-Type
头设置为
application/octet-stream
,但需要改为设置为
text/plain
,例如:

ContentType contentType = new ContentType(MediaTypeNames.Text.Plain); 
或:

此外,您应该为附件指定一个
transferncode
,因为UTF-8不是7比特干净的(许多电子邮件系统仍然需要),例如:

或:

ContentType contentType = new ContentType(MediaTypeNames.Text.Plain); 
ContentType contentType = new ContentType(); 
contentType.MediaType = MediaTypeNames.Text.Plain;
attFile.TransferEncoding = TransferEncoding.QuotedPrintable;  
attFile.TransferEncoding = TransferEncoding.Base64;