如何在ASP.NET中使用具有HTML正文和附件的GMAIL API发送电子邮件 [英] How to send email using GMAIL API having HTML body + attachment in ASP.NET

查看:34
本文介绍了如何在ASP.NET中使用具有HTML正文和附件的GMAIL API发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

如果我删除msg.ContentType ="text/html"这行,则它可以正常工作,但html无法在电子邮件中呈现,显示为纯文本.

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

我想同时发送HTML正文和附件,请帮助.

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

推荐答案

 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();

经过大量的努力,我找到了解决方案.而不是AE.Net.Mail.MailMessage使用System.Net.Mail.MailMessage和MimeKit将其转换为原始字符串.现在带有附件的html主体可以正常工作.

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.

这篇关于如何在ASP.NET中使用具有HTML正文和附件的GMAIL API发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆