【问题标题】:sending long line of text in email在电子邮件中发送长行文本
【发布时间】: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 来传输数据。
  • 您可能是对的,但电子邮件是收件人收集数据的唯一方式。

标签: c# email


【解决方案1】:

所以我不知道这是否真的解决了您的实际问题,但是为了摆脱您现在正在处理的错误,这应该可以:

string message ="my255CharText";

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;

using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(message)))
{
    Attachment data = new Attachment(ms, new ContentType("text/plain; charset=us-ascii"));

    emailMsg.Attachments.Add(data);
    ContentDisposition dispositon = data.ContentDisposition;
    dispositon.Inline=true;
    SmtpClient smtp = new SmtpClient("SMTP_SERVER");
    smtp.Send(emailMsg);
}

当我对此进行测试时,Outlook 和 Android Mail 客户端将附件显示为附件,GMail(Web 和 Android 客户端)将其显示为内联(作为单行文本)。但是在我手机的小屏幕上,显然文字被分成几行。

我感觉您正在处理特定于电子邮件客户端的呈现问题,您的工作可能更多地处理该特定客户端,而不是提出通用解决方案(可能不存在)。

【讨论】:

  • 在提供的格式中,它将Attachment data 下划线为Invalid initializer member declaration。如果我删除 using 并简单地声明 ms 并将其用作附件,我会收到带有“text/plain”附件的空白电子邮件 - 我已经在 Outlook 和 Gmail 中检查过。
  • 抱歉,在其中删除了一个结束括号(在 using 行上) - 已编辑以修复。这就是问题出现的原因。正如我所指出的,即使您指定它是内联的,一些客户端也会将其显示为附件。由客户端决定如何呈现内联附件。
  • 嗯,看来这次我的 SMTP 服务器拒绝处理此消息:/ - 我得到 Failure to send the email :(
【解决方案2】:

好的,我解决了:正如@Dan Field 指出的那样,部分问题在于 Outlook 女士顽固地将内联附件显示为......以及附件。但这并没有解决我的断线问题,这最终是Quoted-printable standard 的错,它确实限制了线的大小。 但是,至少就我的目的而言,7 位编码是可以接受的,并且我能够在一行中发送多达 262 个字符:

string message="255CharsLongMessage";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"\\shareddata\temp\buoy.txt", false, System.Text.Encoding.ASCII))
        {
            file.WriteLine(message);
        }


MailMessage emailMsg = new MailMessage();
emailMsg.To.Add("destination@server.com");
emailMsg.Subject = "My_Subject";
emailMsg.From = new MailAddress("source@email.coom");
emailMsg.BodyEncoding = System.Text.Encoding.ASCII;
emailMsg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("SMTP_SERVER");
Attachment data = new Attachment(@"\\shareddata\temp\buoy.txt", new ContentType("text/plain; charset=us-ascii"));

emailMsg.Attachments.Add(data);
data.ContentDisposition.Inline = true;
data.TransferEncoding = TransferEncoding.SevenBit;
smtp.Send(emailMsg);

【讨论】:

    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 2011-09-16
    • 2012-02-04
    • 2012-06-15
    • 1970-01-01
    • 2017-11-01
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多