如何使用在ASP.NET中具有HTML正文+附件的GMAIL API发送电子邮件

时间:2016-03-03 05:04:29

标签: c# asp.net .net 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();

此代码在没有附件的情况下工作,但是附件而不是附件,byte []出现在收件箱中。

如果我删除msg.ContentType =" text / html"这行然后它正在工作,但html没有在电子邮件中呈现,显示为纯文本。

我想发送HTML正文和附件,请帮忙。

2 个答案:

答案 0 :(得分:4)

 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机身工作正常。

答案 1 :(得分:0)

尝试添加IsBodyHtml = true

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