C# 在电子邮件中发送长行文本

C# 在电子邮件中发送长行文本,c#,email,C#,Email,我必须通过电子邮件发送一串文本。。。但是接收器对其格式要求很高:它必须是纯ASCII文本 我尝试过以内容类型发送:text/plain;charset=“us ascii”内容传输编码:引用可打印。不幸的是,我的字符串很长(255个字符),带引号的printable将78个字符后的字符串剪切到新行: 浮标ID,55073,UTCDATE,2015年6月24日,UTCTIME,22:42.00,状态,E0000,ZEROCRS,201,A= VGHGT,0.92,TZ,6.4,MAXWAVE,3.

我必须通过电子邮件发送一串文本。。。但是接收器对其格式要求很高:它必须是纯ASCII文本

我尝试过以
内容类型发送:text/plain;charset=“us ascii”内容传输编码:引用可打印
。不幸的是,我的字符串很长(255个字符),带引号的printable将78个字符后的字符串剪切到新行:

浮标ID,55073,UTCDATE,2015年6月24日,UTCTIME,22:42.00,状态,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:指定的路径、文件名或两者都太长。完全限定文件名必须少于260个字符,目录名必须少于248个字符。

我还尝试更改到本地磁盘的路径(
c:\temp\bucket.txt
),但文件仍然作为附件发送,而不是内联发送


编辑:请让我明确一点:只有当我以附件形式直接发送变量时,而不是从文件发送变量时,我才会收到
pathTooLongException

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

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邮件客户端将附件显示为附件,GMail(web和Android客户端)将其内联显示(作为单行文本)。但是在我的手机较小的屏幕上,很明显,文字被分成了几行


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

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

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邮件客户端将附件显示为附件,GMail(web和Android客户端)将其内联显示(作为单行文本)。但是在我的手机较小的屏幕上,很明显,文字被分成了几行


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

好的,我解决了它:正如@Dan Field所指出的,部分问题在于Ms Outlook顽固地将内联附件显示为。。好的,附件。但这并没有解决我断线的问题,这最终是由于的错误,这确实限制了线的大小。 但是,至少就我的目的而言,7bit编码是可以接受的,我能够在一行中发送多达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);

好的,我解决了:正如@Dan Field指出的,部分问题在于Ms Outlook顽固地将内联附件显示为。。好的,附件。但这并没有解决我断线的问题,这最终是由于的错误,这确实限制了线的大小。 但是,至少就我的目的而言,7bit编码是可以接受的,我能够在一行中发送多达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);

好啊请尝试将
消息
保存到非共享文件中,好吗?在
C:\files\bugger.txt
中说。我已将路径更改为C:\temp\bugger.txt-文件仍作为附件发送。“路径过长”仅在我试图直接从变量
附件数据=新附件(字符串消息,…)发送时显示。
通过电子邮件发送格式化数据总是充满问题。除了格式问题,它也不能保证交付,而且不安全(很容易被欺骗或拦截)。我建议您使用web服务或SFTP/FTPS传输数据。您可能是对的,但电子邮件是收件人收集数据的唯一方式。好的。请尝试将
消息
保存到非共享文件中,好吗?在
C:\files\bugger.txt
中说。我已将路径更改为C:\temp\bugger.txt-文件仍作为附件发送。“路径过长”仅在我试图直接从变量
附件数据=新附件(字符串消息,…)发送时显示。
通过电子邮件发送格式化数据总是充满问题。除了格式问题,它也不能保证交付,而且不安全(很容易被欺骗或拦截)。我建议您使用web服务或SFTP/FTPS传输数据。您可能是对的,但电子邮件是收件人收集数据的唯一方式。在提供的格式中,它将
附件数据作为
无效初始值设定项成员声明
加下划线。如果我使用
删除
,然后简单地声明
ms
并将其用作附件,我会收到带有“文本/普通”附件的空白电子邮件-我已在Outlook和Gmail中对其进行了检查。很抱歉,我在其中(使用行)删除了一个结束部分-编辑以修复。这就是问题所在的原因。正如我所指出的,即使您指定它是内联的,一些客户机也会将其显示为附件。由客户端决定如何呈现内联附件。嗯,这一次我的SMTP服务器似乎拒绝处理此消息:/-我收到
发送电子邮件失败
:(在提供的格式中,它将
附件数据作为
无效初始值设定项成员声明
加下划线。