问题在C#与SmtpClient发送电子邮件 [英] Problem sending email with SmtpClient in C#

查看:383
本文介绍了问题在C#与SmtpClient发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net/MVC应用程序,我试图发送HTML电子邮件。我通过阅读与标记的HTML文件,然后更换令牌这样做。这部分是罚款和生成HTML这就是我想要的东西,但是当我发送电子邮件,有什么我收到类似的外观 -

I have an ASP.Net/MVC application and I'm trying to send HTML emails. I'm doing this by reading in an HTML file with tokens, then replacing the tokens. That part is fine and generates HTML that is exactly what I want, but when I send the email, what I'm receiving looks like -

<style type=3D"text/css">=
=0D=0A.styleTitles=0D=0A{=0D=0Afont-weight:=bold;=0D=0A}=0D=0A 
.style1=0D=0A        {=0D=0A

和应该看起来像

    <style type="text/css">
    .styleTitles
    {
        font-weight: bold;
    }
    .style1
    {
        height: 15px;
    }

我看了在网上和我似乎无法找到正确的语法来发送邮件。我见过一些解决方案,但没有似乎工作。

I've looked on the web and I can't seem to find the correct syntax to send the message. I've seen some solutions, but none seem to work.

我目前的测试code是 -

My current test code is -

SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();

MailAddress SendFrom = new MailAddress("xxxx@abc.com");
MailAddress SendTo = new MailAddress("zzzz@gmail.com");
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

var plainView = AlternateView.CreateAlternateViewFromString(msgBody,null,"text/html");
plainView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
MyMessage.AlternateViews.Add(plainView);
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;
smtpclient.Send(MyMessage);

有什么建议?

推荐答案

也许是这样的:

var plainView = AlternateView.CreateAlternateViewFromString(msgBody, new ContentType("text/plain; charset=UTF-8"));

MyMessage.AlternateViews.Add(plainView);
MyMessage.BodyEncoding = Encoding.UTF8;
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;

这篇关于问题在C#与SmtpClient发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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