[英]Unable to attach an eml file as an attachment when sending an email using c#?
我正在通过hmailserver
接收电子邮件,并将这些电子邮件作为.eml文件作为另一个报告电子邮件的附件发送。
我在阅读和发送这些电子邮件作为附件时遇到问题。
这就是我在做什么。
public void addAttachment(string pathname, bool retry)
{
string attachmentname = "";
do
{
try
{
attachmentname = Path.GetFileNameWithoutExtension(pathname);
Stream file = new MemoryStream(File.ReadAllBytes(pathname));
Log.WriteMessage("Size" + file.Length);
dtstreamAttach.Add(attachmentname+".eml", file);
retry = false;
}
catch (ArgumentException e)
{
string strCurrentTs = DateTime.Now.ToString(strDateFormat);
attachmentname = attachmentname + "-" + strCurrentTs+".eml";
}
} while (retry);
}
然后,
MailMessage message = new MailMessage();
.
.
message.Attachments.Add(new Attachment(kvp.Value, kvp.Key)); // I have an attachment dictionary
string contenttype = GetMimeType(".eml");
ContentType cnttype = new ContentType(contenttype);
message.Attachments[0].ContentType = cnttype;
如您所见,我打印了Stream大小 -打印出的内容类似于4790Bytes(4KB),但是当我收到电子邮件时,我只收到一个大小为1KB的eml文件,而eml文件为空 。
我检查了文件路径,并确保在发送报告邮件之前,该电子邮件一直存在。 我还验证了内容类型为message/rfc822
。
一切似乎都结帐了。 不知道是什么问题。
我能够解决它。 看起来MemoryStream
具有正确的流,并且消息对象的ContentStream
也具有正确的大小,但是Stream 位置已经移到流的末尾,因此,当实际上发送消息时,它实际上没有任何内容。
因此,在将流添加到AttachmentCollection
之前,请确保将流重新定位回原始位置 ,例如
Seek(0, SeekOrigin.Begin)
特别是当使用包裹在dll中的流时,可能是某些东西移动了它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.