如果找到附件,我会收到带附件的电子邮件,然后将该电子邮件转发给某个用户。我使用以下代码来执行此操作。当我使用下面的代码时,它会发送带附件的电子邮件 但附件没有内容(空白附件)。你能告诉我我错在哪里吗?
public bool AddAttachment(System.IO.Stream sm, string fileName)
{
try
{
System.Net.Mail.Attachment atch = new System.Net.Mail.Attachment(sm, fileName);
msg.Attachments.Add(atch);
return true;
}
catch (Exception ex)
{
TraceService(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);
}
return false;
}
ObjMail.MsgData = strBuilder.ToString();
for (int i = 0; i < sMail.Attachments.Length; i++)
{
if (!string.IsNullOrEmpty(sMail.Attachments[i].Name))
{
if (!sMail.Attachments[i].Name.Contains(".dat"))
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
var sr = new StreamReader(ms);
writer.Write(sMail.Attachments[i]);
ObjMail.AddAttachment(ms, sMail.Attachments[i].Name);
}
}
}
ObjMail.SendMail();
答案 0 :(得分:0)
要附加使用以下代码我已使用该示例附加JPG图像
Attachment attach = new Attachment(id + ".jpg");
attach.Name = "WhateverName.jpg";
mail.Attachments.Add(attach);
答案 1 :(得分:0)
试试这个:
if (!string.IsNullOrEmpty(sMail.Attachments[i].Name))
{
if (!sMail.Attachments[i].Name.Contains(".dat"))
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
sMail.Attachments[i].ContentStream.CopyTo(ms);
ObjMail.AddAttachment(ms, sMail.Attachments[i].Name);
}
}
或者你可以使用简单的:
if (!string.IsNullOrEmpty(sMail.Attachments[i].Name))
{
if (!sMail.Attachments[i].Name.Contains(".dat"))
{
ObjMail.Add(sMail.Attachments[i]);
}
}
ObjMail.SendMail();