ASP.NET C# 电子邮件附件在发送到多个电子邮件地址时最终变为 0 字节
ASP.NET C# Email Attachments Eventually go to 0 Bytes When Sending to Multiple Email Addresses
我有这个例程,可以按如下方式将电子邮件发送到列表,并且我已经对其进行了测试。我收到的第一封电子邮件没问题,但第二封电子邮件的字节数比实际文件呈现的字节数少,无法打开。我怎样才能使每个电子邮件附件都能正确发送而不发送任何 0 字节附件?我通过 ASP.NET C# webform 将附件直接发送给收件人,但大多数 PDF 附件都是由于错误消息解码不当而发送的。这是页面的主要电子邮件代码:
if (emailIsValid(EmailToSend))
{
string TheSubject = SubjectTxt.Text;
string TheBody = "Dear " + FirstName + ",<br/><br/>" + MessageTxt.Text;
TheBody = TheBody + " EMail Body "
string BodyTxt = TheBody.Replace(Environment.NewLine, "<br />");
MailMessage mailObj = new MailMessage(
"noreply@company.com", EmailToSend, TheSubject, BodyTxt);
SmtpClient SMTPServer = new SmtpClient("unknown.company.com");
string RPT = FromTxt.Text;
mailObj.ReplyToList.Add(RPT);
mailObj.BodyEncoding = System.Text.Encoding.UTF8;
mailObj.IsBodyHtml = true;
string filePath = txtAttachment.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".ppt":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".pptx":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".csv":
contenttype = "text/csv";
break;
case ".txt":
contenttype = "text/csv";
break;
default:
contenttype = "Unknown Content Type";
break;
}
if (txtAttachment.PostedFile != null && contenttype != "Unknown Content Type")
{
try
{
string strFileName =
System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
mailObj.Attachments.Add(attachFile);
}
catch
{
}
}
try
{
SMTPServer.Send(mailObj);
SqlConnection con2 = new SqlConnection(CS);
con2.Open();
DateTime now = DateTime.Now;
}
catch
{
}
之前
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
尝试重置流光标,如下所示:
txtAttachment.PostedFile.InputStream.Position = 0;
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
我有这个例程,可以按如下方式将电子邮件发送到列表,并且我已经对其进行了测试。我收到的第一封电子邮件没问题,但第二封电子邮件的字节数比实际文件呈现的字节数少,无法打开。我怎样才能使每个电子邮件附件都能正确发送而不发送任何 0 字节附件?我通过 ASP.NET C# webform 将附件直接发送给收件人,但大多数 PDF 附件都是由于错误消息解码不当而发送的。这是页面的主要电子邮件代码:
if (emailIsValid(EmailToSend))
{
string TheSubject = SubjectTxt.Text;
string TheBody = "Dear " + FirstName + ",<br/><br/>" + MessageTxt.Text;
TheBody = TheBody + " EMail Body "
string BodyTxt = TheBody.Replace(Environment.NewLine, "<br />");
MailMessage mailObj = new MailMessage(
"noreply@company.com", EmailToSend, TheSubject, BodyTxt);
SmtpClient SMTPServer = new SmtpClient("unknown.company.com");
string RPT = FromTxt.Text;
mailObj.ReplyToList.Add(RPT);
mailObj.BodyEncoding = System.Text.Encoding.UTF8;
mailObj.IsBodyHtml = true;
string filePath = txtAttachment.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".ppt":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".pptx":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".csv":
contenttype = "text/csv";
break;
case ".txt":
contenttype = "text/csv";
break;
default:
contenttype = "Unknown Content Type";
break;
}
if (txtAttachment.PostedFile != null && contenttype != "Unknown Content Type")
{
try
{
string strFileName =
System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
mailObj.Attachments.Add(attachFile);
}
catch
{
}
}
try
{
SMTPServer.Send(mailObj);
SqlConnection con2 = new SqlConnection(CS);
con2.Open();
DateTime now = DateTime.Now;
}
catch
{
}
之前
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
尝试重置流光标,如下所示:
txtAttachment.PostedFile.InputStream.Position = 0;
Attachment attachFile =
new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);