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

我的未来我决定 提交于 2019-12-10 10:39:40

问题


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.


回答1:


 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.




回答2:


try adding IsBodyHtml = true

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


来源:https://stackoverflow.com/questions/35763607/how-to-send-email-using-gmail-api-having-html-body-attachment-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!