发送电子邮件时显示“noname”附件

时间:2018-03-07 02:32:14

标签: c# email-attachments

HELP!所以我有这个问题,当发送带有图片的电子邮件时,这是一个“noname”附件

enter image description here

现在,我希望将其删除,因为它不是我的电子邮件的一部分。

继承人是我的代码

 public static bool SendEmailNotification(List<string> toList, List<string> ccList, string subject, string body)
    {
        bool isSent = true;
        try
        {
            var mail = new MailMessage();
            var smtpClient = new SmtpClient(Constants.SMTP.SMTPClient);
            var alternateView = AlternateView.CreateAlternateViewFromString(body, null, Constants.SMTP.Format);
            mail.From = new MailAddress(Constants.SMTP.EmailAddress);
            if (toList != null && toList.Any())
            {
                foreach (var email in toList)
                {
                    mail.To.Add(email);
                }

                if (ccList != null && ccList.Any())
                {
                    foreach (var email in ccList)
                    {
                        mail.CC.Add(email);
                    }
                }
                mail.Subject = subject;
                mail.IsBodyHtml = true;
                mail.AlternateViews.Add(alternateView);

                smtpClient.Credentials = new NetworkCredential(Constants.SMTP.EmailAddress,
                    Constants.SMTP.EmailPassword, Constants.SMTP.Email);
                smtpClient.Send(mail);
            }
            else
            {
                isSent = false;
            }
        }
        catch
        {
            isSent = false;
        }
        return isSent;
    }

4 个答案:

答案 0 :(得分:0)

附件实际上是您的图像有效载荷(二进制)。设置为“ noname”。原因是某些参数设置不正确。在这种情况下,要正确执行此操作,请参考https://brutaldev.com/post/sending-html-e-mail-with-embedded-images-(the-correct-way)

答案 1 :(得分:0)

if( attachementFile!=null)
{
    Attachment attachment = new Attachment(attachementFile, MediaTypeNames.Application.Octet);
    ContentDisposition disposition = attachment.ContentDisposition;
    disposition.CreationDate = File.GetCreationTime(attachementFile);
    disposition.ModificationDate = File.GetLastWriteTime(attachementFile);
    disposition.ReadDate = File.GetLastAccessTime(attachementFile);
    disposition.FileName = Path.GetFileName(attachementFile);
    disposition.Size = new FileInfo(attachementFile).Length;
    disposition.DispositionType = DispositionTypeNames.Attachment;
    return attachment;
}

答案 2 :(得分:0)

以下是来自 link 中的 @kendolew's answer 的代码,其中包含一个关于如何正确包含附件的示例:

public static void SendMessageWithEmbeddedImages()
{
  string htmlMessage = @"<html>
                         <body>
                         <img src='cid:EmbeddedContent_1' />
                         </body>
                         </html>";
  SmtpClient client = new SmtpClient("mail.server.com");
  MailMessage msg = new MailMessage("noreply@deventerprise.net",
                                    "someone@deventerprise.net");
  // Create the HTML view
  AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
                                               htmlMessage,
                                               Encoding.UTF8,
                                               MediaTypeNames.Text.Html);
  // Create a plain text message for client that don't support HTML
  AlternateView plainView = AlternateView.CreateAlternateViewFromString(
                                              Regex.Replace(htmlMessage,
                                                            "<[^>]+?>",
                                                            string.Empty),
                                              Encoding.UTF8,
                                              MediaTypeNames.Text.Plain);
  string mediaType = MediaTypeNames.Image.Jpeg;
  LinkedResource img = new LinkedResource(@"C:\Images\MyImage.jpg", mediaType);
  // Make sure you set all these values!!!
  img.ContentId = "EmbeddedContent_1";
  img.ContentType.MediaType = mediaType;
  img.TransferEncoding = TransferEncoding.Base64;
  img.ContentType.Name = img.ContentId;
  img.ContentLink = new Uri("cid:" + img.ContentId);
  htmlView.LinkedResources.Add(img);
  //////////////////////////////////////////////////////////////
  msg.AlternateViews.Add(plainView);
  msg.AlternateViews.Add(htmlView);
  msg.IsBodyHtml = true;
  msg.Subject = "Some subject";
  client.Send(msg);
}

答案 3 :(得分:-1)

mail.From = new MailAddress(Constants.SMTP.EmailAddress, Constants.SMTP.EmailAddress)