【发布时间】:2015-06-25 02:05:43
【问题描述】:
我必须通过电子邮件发送一串文本……但接收者对其格式要求很高:它必须是纯 ASCII 文本。
我已尝试以Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 发送。不幸的是,我的字符串很长(255 个字符),并且引号可打印在 78 个字符后将此字符串剪切为新行:
BUOYID,55073,UTCDATE,24/06/2015,UTCTIME,22:42.00,STATUS,E0000,ZEROCRS,201,A= VGHGT,0.92,TZ,6.4,MAXWAVE,3.12,SIGWAVE,1.63,SIGPERIOD,9.3,H10,2.2,T10,9.5,M=
显然,解决方案在于将文本作为ContentDisposition.Inline 附件发送(正文中包含内容的附件),这将允许更长的文本字符串......但这并不容易。
如果我将字符串保存到文本文件中,如下所示:
string message ="my255CharText";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"\\shareddata\temp\buoy.txt"))
{
file.WriteLine(message);
}
MailMessage emailMsg = new MailMessage();
emailMsg.To.Add("email@destination.com);
emailMsg.Subject = "my Subject";
emailMsg.From = new MailAddress("email@source.com");
emailMsg.BodyEncoding = System.Text.Encoding.ASCII;
emailMsg.IsBodyHtml = false;
Attachment data = new Attachment(@"\\shareddata\temp\buoy.txt", MediaTypeNames.Text.Plain);
emailMsg.Attachments.Add(data);
ContentDisposition dispositon = data.ContentDisposition;
dispositon.Inline=true;
SmtpClient smtp = new SmtpClient("SMTP_SERVER");
smtp.Send(emailMsg);
我一直在发送带有附件而不是正文中文本文件内容的电子邮件。 如果我尝试将变量作为附件:
Attachment data = new Attachment(message, MediaTypeNames.Text.Plain);
我得到以下信息:
PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
我也尝试过更改本地磁盘的路径 (c:\temp\buoy.txt),但文件仍然作为附件发送,而不是内联。
编辑:请让我说清楚:只有当我将变量作为附件直接发送时,我才会收到 pathTooLongException,而不是当我从文件发送时。
【问题讨论】:
-
好的。您能否尝试将
message保存在不是共享文件的文件中?在C:\files\buoy.txt说。 -
我已将路径更改为 c:\temp\buoy.txt - 文件仍作为附件发送。 “路径太长”仅在我尝试从变量
Attachment data = new Attachment (string message,....)直接发送时显示 -
通过电子邮件发送格式化数据总是充满问题。除了格式问题,它也不能保证交付并且不安全(很容易被欺骗或拦截)。我建议使用 Web 服务或 SFTP/FTPS 来传输数据。
-
您可能是对的,但电子邮件是收件人收集数据的唯一方式。