user2764660
user2764660

Reputation: 93

How to send email using GMAIL API having HTML body + attachment in ASP.NET

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.

Upvotes: 2

Views: 4194

Answers (2)

user2764660
user2764660

Reputation: 93

 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.

Upvotes: 5

Gabriel Llorico
Gabriel Llorico

Reputation: 1803

try adding IsBodyHtml = true

 new AE.Net.Mail.MailMessage
          {
              Subject = subject,
              Body = bodyhtml,
              From = new System.Net.Mail.MailAddress("myemail")
              IsBodyHtml = true
          };

Upvotes: 0

Related Questions