如何通过电子邮件发送Excel文件?
我有一个Excel文件(Excel 2003 / xls格式),我想通过电子邮件与C#发送它。 我的代码发送成功,但当我尝试打开响应文件,似乎编码错误。
例如这里是响应文件名:
= _utf-8_B_RWxzesOhbW9sw6FzXzIwMTJfMTBfMTZf.dat
这里是响应文件本身:
=?utf-8?B?VEdWdmJIWmhjMkZ1Wk1Pelh6UXlYekZmPz0NCiA9P3V0Zi04P0I / VGtW?= \?= utf-8?B?TlgwZFRXaTU0YkhNPT89?=“Content-Transfer-Encoding:base64 Content-Disposition:attachment
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7 / CQAGAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAAEAAAIwAAAAEAAAD + //// AAAAAAAAAAD ///////////////////// //////////////////// //////////////////////////////////////////// ////// //////////////////////// ….
这是我的代码片段:
... var attachment = new Attachment(WriteFileToMemory("fileFullPath"), "fileName.xls"); attachment.ContentType = new ContentType("application/vnd.ms-excel"); attachmentCollection.Add(attachment); ... private Stream WriteFileToMemory(string filePath) { var memoryStream = new MemoryStream(); _openedStreams.Add(memoryStream); using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var bytes = new byte[file.Length]; file.Read(bytes, 0, (int) file.Length); memoryStream.Write(bytes, 0, (int) file.Length); file.Close(); } memoryStream.Position = 0; return memoryStream; }
如何设置附件编码types,以及我应该在Excel文件中使用哪种编码?
请帮我解决这个问题。 提前致谢。
正如@Magnus所指出的那样,你使得它太复杂了, new Attachment()
可以处理FileStream
所以只需要传递一个新的文件stream到构造函数。
... var attachment = new Attachment(File.Open("fileFullPath", FileMode.Open), "fileName.xls"); attachment.ContentType = new ContentType("application/vnd.ms-excel"); attachmentCollection.Add(attachment); ...
一个警告的话,种类是脱离主题,你不能多次发送这样的邮件,因为stream不会总是正确地重置自己。
我find一个解决scheme:
... var attachment = CreateAttachment(WriteFileToMemory(Common.TempPath + excelName), excelName); attachmentCollection.Add(attachment); ... private Stream WriteFileToMemory(string filePath) { var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); _openedStreams.Add(fileStream); return fileStream; } public static Attachment CreateAttachment(Stream attachmentFile, string displayName) { var attachment = new Attachment(attachmentFile, displayName); attachment.ContentType = new ContentType("application/vnd.ms-excel"); attachment.TransferEncoding = TransferEncoding.Base64; attachment.NameEncoding = Encoding.UTF8; string encodedAttachmentName = Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName)); encodedAttachmentName = SplitEncodedAttachmentName(encodedAttachmentName); attachment.Name = encodedAttachmentName; return attachment; } private static string SplitEncodedAttachmentName(string encoded) { const string encodingtoken = "=?UTF-8?B?"; const string softbreak = "?="; const int maxChunkLength = 30; int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2); IEnumerable<string> parts = SplitByLength(encoded, splitLength); string encodedAttachmentName = encodingtoken; foreach (var part in parts) { encodedAttachmentName += part + softbreak + encodingtoken; } encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length); return encodedAttachmentName; } private static IEnumerable<string> SplitByLength(string stringToSplit, int length) { while (stringToSplit.Length > length) { yield return stringToSplit.Substring(0, length); stringToSplit = stringToSplit.Substring(length); } if (stringToSplit.Length > 0) { yield return stringToSplit; } }
基于此来源: http : //social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55
我通过更正MailMessage属性来解决同样的问题:
bool SendEmail(string subject, string message, Attachment attachment) { try { SmtpClient smtpClient = CreateProductionSMTPClient(); MailMessage msg = new MailMessage(); msg.Subject = subject; msg.Body = message; msg.To.Add(ConfigurationHelper.EmailTo); msg.From = new MailAddress(ConfigurationHelper.EmailFrom); msg.BodyEncoding = Encoding.UTF8; //commented line cause the problem // msg.Headers.Add("Content-Type", "text/html"); //instead of that use next line msg.IsBodyHtml = true; if (attachment != null) { msg.Attachments.Add(attachment); } smtpClient.Send(msg); return true; } catch (Exception ex) { return false; } } //Attachment building Attachment WrapExcelBytesInAttachment(byte[] excelContent) { try { Stream stream = new MemoryStream(excelContent); Attachment attachment = new Attachment(stream, "fileName.xls"); attachment.ContentType = new ContentType("application/vnd.ms-excel"); return attachment; } catch (Exception ex) { return null; } }
如果你想从DataTable生成Excel,并通过邮件发送,你可以附加Excel文件,如下所示:
Workbook theWorkbook = new Workbook(); theWorkbook.SetCurrentFormat(WorkbookFormat.Excel2007); Worksheet theWorkSheet = theWorkbook.Worksheets.Add("Sheet1"); int iRow = 0; int iColumn = 0; theWorkSheet.Rows[0].CellFormat.Font.Bold = ExcelDefaultableBoolean.True; //Titles foreach (DataColumn column in DataTable.Columns) { theWorkSheet.Rows[iRow].Cells[iColumn].Value = column.ColumnName; iColumn++; } //Values foreach (DataRow row in DataTable.Rows) { iColumn = 0; iRow++; foreach (var item in row.ItemArray) { theWorkSheet.Rows[iRow].Cells[iColumn].Value = item.ToString(); iColumn++; } } System.IO.MemoryStream theStream = new System.IO.MemoryStream(); theWorkbook.Save(theStream); byte[] byteArr = theStream.ToArray(); System.IO.MemoryStream stream1 = new System.IO.MemoryStream(byteArr, true); stream1.Write(byteArr, 0, byteArr.Length); stream1.Position = 0; message.Attachments.Add(new Attachment(stream1, "filename.xlsx"));
它至less在.NET框架4和Excel 2016的工作。
问候。
包含非ASCII字符且长度超过41个UTF-8编码字节的电子邮件附件名称在为.NET Framework 4编译的应用程序中传输之前进行了两次编码。出现此问题的原因在于。 NET Framework 4. SMTP编码被重写为包含正确折叠的每个RFC标准的行长限制。 当名称string太长时,此行为会插入其他回车换行符(CRLF)字符。 这些额外的控制字符导致附件名称被再次编码。 更多你可以在这里findhttp://support.microsoft.com/kb/2402064