Rashid
Rashid

Reputation: 1750

"noname" attachment shown when email sent has image

HELP! So i have this issue wherein theres a "noname" attachment when sending emails with pictures as seen below

enter image description here

Now, i want this to remove since it is not part of my emailer.

heres is my code below

 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;
    }

Upvotes: 3

Views: 8370

Answers (3)

Akaisteph7
Akaisteph7

Reputation: 6554

Here's the code from the link in @kendolew's answer with an example on how to properly include an attachment:

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("[email protected]",
                                    "[email protected]");
  // 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);
}

Upvotes: 0

Vikas Chaturvedi
Vikas Chaturvedi

Reputation: 507

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;
}

Upvotes: 0

kendolew
kendolew

Reputation: 506

The attachment is actually your image payload in binary. It's set to "noname". Reason being is certain parameters aren't set properly. In this case, to do it properly refer to https://brutaldev.com/post/sending-html-e-mail-with-embedded-images-(the-correct-way)

Upvotes: 6

Related Questions