代码之家  ›  专栏  ›  技术社区  ›  vondip

向gmail帐户发送包含html的smtp邮件

  •  5
  • vondip  · 技术社区  · 15 年前

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

    我正在使用C.NET 3.5。我在代码中使用了这些类:

    • 邮件消息
    • SMTP客户端

    怎么能做到?

    这是我的代码副本:

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

    谢谢!

    2 回复  |  直到 15 年前
        1
  •  8
  •   mnemosyn    15 年前

    这样的做法应该管用:

    注意 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而不是硬编码的更复杂的模板化解决方案,例如 EMailTemplateService 在mvccontrib中,您可以使用它作为指导。

        2
  •  3
  •   Lee    15 年前

    一种方法是创建电子邮件的备用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);
    
    推荐文章