2
var msg = new AE.Net.Mail.MailMessage
              {
                  Subject = subject,
                  Body = bodyhtml,
                  From = new System.Net.Mail.MailAddress("myemail")

              };
            foreach (string add in vendorEmailList.Split(','))
            {
                if (string.IsNullOrEmpty(add))
                    continue;

                msg.To.Add(new System.Net.Mail.MailAddress(add));
            }

            msg.ReplyTo.Add(msg.From); // Bounces without this!!
            msg.ContentType = "text/html";

            ////attachment code

            foreach (string path in attachments)
            {
                var bytes = File.ReadAllBytes(path);
                string mimeType = MimeMapping.GetMimeMapping(path);
                AE.Net.Mail.Attachment attachment = new AE.Net.Mail.Attachment(bytes, mimeType, Path.GetFileName(path), true);
                msg.Attachments.Add(attachment);
            }
            ////end attachment code

            var msgStr = new StringWriter();
            msg.Save(msgStr);

            Message message = new Message();
            message.Raw = Base64UrlEncode(msgStr.ToString());
            var result = gmailService.Users.Messages.Send(message, "me").Execute();

This code is working without attachment but with attachment instead of attachment directly byte[] is appearing in inbox.

If i remove msg.ContentType = "text/html" this line then it is working but html not rendering in email, appearing as plain text.

I want to send both HTML body and attachment, Please help.

user2764660
  • 93
  • 1
  • 7

2 Answers2

5
 MailMessage mail = new MailMessage();
            mail.Subject = subject;
            mail.Body = bodyhtml;
            mail.From = new MailAddress("myemail");
            mail.IsBodyHtml = true;

            foreach (string add in vendorEmailList.Split(','))
            {
                if (string.IsNullOrEmpty(add))
                    continue;

                mail.To.Add(new MailAddress(add));
            }

            foreach (string add in userEmailList.Split(','))
            {
                if (string.IsNullOrEmpty(add))
                    continue;

                mail.CC.Add(new MailAddress(add));
            }

            foreach (string path in attachments)
            {
                //var bytes = File.ReadAllBytes(path);
                //string mimeType = MimeMapping.GetMimeMapping(path);
                Attachment attachment = new Attachment(path);//bytes, mimeType, Path.GetFileName(path), true);
                mail.Attachments.Add(attachment);
            }
            MimeKit.MimeMessage mimeMessage = MimeMessage.CreateFromMailMessage(mail);

            Message message = new Message();
            message.Raw = Base64UrlEncode(mimeMessage.ToString());
            var result = gmailService.Users.Messages.Send(message, "me").Execute();

I found solution after lot of efforts. Instead of AE.Net.Mail.MailMessage used System.Net.Mail.MailMessage and MimeKit to convert it to raw string. And now html body with attachment is working fine.

user2764660
  • 93
  • 1
  • 7
  • Curious if you've had any issues with this sending out correctly with multiple attachments. It has worked fine for me with a single attachment, then crashes with multiple. – Eissa Mar 12 '16 at 04:50
  • Sorry for late reply..my code is working for multiple attachments without any issue. – user2764660 Mar 16 '16 at 09:06
  • Works well for me and after days of banging my head on my desk this answer saved my sanity – Steven Pfeifer May 06 '22 at 07:07
0

try adding IsBodyHtml = true

 new AE.Net.Mail.MailMessage
          {
              Subject = subject,
              Body = bodyhtml,
              From = new System.Net.Mail.MailAddress("myemail")
              IsBodyHtml = true
          };
Gabriel Llorico
  • 1,783
  • 1
  • 13
  • 23