【问题标题】:send smtp mail including html to gmail account将包含 html 的 smtp 邮件发送到 gmail 帐户
【发布时间】:2011-01-27 05:22:05
【问题描述】:

我发现了这个向 gmail 用户发送电子邮件的小代码。我希望邮件正文包含 html(例如,解码一个链接以使其包含与它指向的 url 不同的文本)。

我正在使用 c# .net 3.5。我在我的代码中使用了这些类:

  • 邮件消息
  • SmtpClient

如何做到这一点?

这是我的代码的副本:

            MailMessage message = new MailMessage("me@gmail.com", WebCommon.UserEmail, "Test", context.Server.HtmlEncode("<html> <body> <a href='www.cnn.com'> test </a> </body> </html> "));
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential("him@gmail.com", "myPwd");
            message.IsBodyHtml = true;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");

            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = true;
            smtp.Credentials = cred;
            smtp.Port = 587;

            smtp.Send(message);

谢谢!

【问题讨论】:

  • 什么语言/平台/框架至少会有帮助,没有任何细节是不可能回答的。
  • 请发布您已完成的代码以及您尝试完成的示例。一般来说,您应该只需要传递 HTML,由接收方决定如何处理它。您可能需要设置标题,但我不能说没有看到您的代码。

标签: c# .net gmail


【解决方案1】:

一种方法是创建电子邮件的备用 html 视图:

MailMessage message = new MailMessage();
message.Body = //plain-text version of message
//set up message...

//create html view
string htmlBody = "<html>...</html>";
htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
message.AlternateViews.Add(htmlView);

//send message
smtpClient.Send(message);

【讨论】:

    【解决方案2】:

    这样的事情应该可以工作:

    请注意,MailMessage 指的是System.Net.MailMessage。还有System.Web.MailMessage,我从来没有用过,而且据我所知已经过时了。

    MailMessage message = new MailMessage();
    // Very basic html. HTML should always be valid, otherwise you go to spam
    message.Body = "<html><body><p>test</p></body></html>"; 
    // QuotedPrintable encoding is the default, but will often lead to trouble, 
    // so you should set something meaningful here. Could also be ASCII or some ISO
    message.BodyEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;
    // No Subject usually goes to spam, too
    message.Subject = "Some Subject";
    // Note that you can add multiple recipients, bcc, cc rec., etc. Using the 
    // address-only syntax, i.e. w/o a readable name saves you from some issues
    message.To.Add("someone@gmail.com");
    
    // SmtpHost, -Port, -User, -Password must be a valid account you can use to 
    // send messages. Note that it is very often required that the account you
    // use also has the specified sender address associated! 
    // If you configure the Smtp yourself, you can change that of course
    SmtpClient client = new SmtpClient(SmtpHost, SmtpPort) {
                Credentials = new NetworkCredential(SmtpUser, SmtpPassword),
                EnableSsl = enableSsl;
            };
    
            try {
                // It might be necessary to enforce a specific sender address, see above
                if (!string.IsNullOrEmpty(ForceSenderAddress)) {
                    message.From = new MailAddress(ForceSenderAddress);
                }
                client.Send(message);
            }
            catch (Exception ex) {
                return false;
            }
    

    对于呈现正文 html 而不是硬编码的更复杂的模板解决方案,例如,MvcContrib 中的 EMailTemplateService 可以用作指导。

    【讨论】:

    • 好的,我不知道为什么我的代码以前不能工作。我在这里复制了你的代码,效果很好,谢谢!
    • @AaronPatten:我可以建议您将其表述为一个新问题吗?恢复一个有 5 年历史的线程通常不太有希望,因为随着时间的推移情况会发生很大变化,只有极少数人会收到通知......此外,“不会在 Outlook 中呈现”不是很精确 - 什么它看起来究竟是什么,outlook 显示的标题是什么,你可以保存/上传 .eml 文件,你的代码在什么环境中运行,等等。将是你的问题应该解决的问题(不是在这里,在一个新问题中) )。
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 2022-11-12
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多