使用C#实现发送自定义的html格式邮件的代码案例


本文摘自PHP中文网,作者黄舟,侵删。

本篇文章主要介绍了通过C#实现发送自定义的html格式邮件,详细的介绍了发送HTML格式邮件的方法,有兴趣的可以了解一下。

要发送HTML格式邮件,需要设置MailMessage对象的IsBodyHtml属性,设置为true。

类MailMessage在命名空间System.Net.Mail下。

1

using System.Net.Mail;

发送HTML格式的邮件在HoverTreeTop项目中已经实现,并发送成功。

需依赖于HoverTreeFrame项目的HoverTreeEmail类。

方法为:

代码如下:

1

public static string HoverTreeSendEmail(string userName, string password, SmtpClient smtpClient, MailMessage mailMessage)

页面截图:

EmailSend.aspx页面:

1

2

3

4

5

6

7

8

9

<h2>发送邮件</h2>

  <br />收信人邮箱:<asp:TextBox runat="server" ID="textBox_mail" TextMode="Email" Columns="53" />

  <br />标题:<asp:TextBox runat="server" ID="textBox_title" Columns="60" />

  <br /><asp:CheckBox runat="server" ID="checkBox_isHtml" Text="是否HTML格式" />

  <br />内容:

  <br /><asp:TextBox runat="server" ID="textBox_content" TextMode="MultiLine" Rows="10" Columns="70" />

  <br /> <asp:Button runat="server" ID="button_send" Text="发送邮件" OnClick="button_send_Click" />

    <br />

    <asp:Literal runat="server" ID="literal_tips" />

EmailSend.aspx.cs代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

using System;

using System.Net.Mail;

using HoverTree.HoverTreeFrame.HtNet;

using HoverTreeTop.HtConfig.MyConfig;

 

namespace HoverTreeTop.HoverTree.HoverTreePanel.HTPanel.HEmail

{

  public partial class EmailSend : System.Web.UI.Page

  {

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

    protected void button_send_Click(object sender, EventArgs e)

    {

      //使用smtp来发送邮件

      //literal_tips.Text = HoverTreeEmail.HoverTreeSendEmail("smtp.hovertree.com", "hello@.mail.hovertree.com",

      "hewenqi", "hello@mail.hovertree.com", "ht@mail.hovertree.com", "祝你生日快乐!", "生日快乐!天天开心! -- 何问起");

      // literal_tips.Text = HoverTreeEmail.HoverTreeSendEmail(HtSmtpConfig.HtSmtpHost, HtSmtpConfig.HtSmtpUserName,

      HtSmtpConfig.HtSmtpPassword, HtSmtpConfig.HtSmtpFromEmail, textBox_mail.Text.Trim(), textBox_title.Text, textBox_content.Text);

 

      SmtpClient h_smtpClient = new SmtpClient();

      h_smtpClient.Host = HtSmtpConfig.HtSmtpHost;

      MailMessage h_mailMessage = new MailMessage();

      h_mailMessage.From = new MailAddress(HtSmtpConfig.HtSmtpFromEmail);

      h_mailMessage.To.Add(textBox_mail.Text.Trim());

      h_mailMessage.Subject = textBox_title.Text.Trim();

      h_mailMessage.Body = textBox_content.Text;

      h_mailMessage.IsBodyHtml = checkBox_isHtml.Checked;

 

      literal_tips.Text = HoverTreeEmail.HoverTreeSendEmail(HtSmtpConfig.HtSmtpUserName, HtSmtpConfig.HtSmtpPassword, h_smtpClient, h_mailMessage);

 

      if (literal_tips.Text == "")

      {

        literal_tips.Text = "发送成功!";

        textBox_content.Text = "";

        textBox_title.Text = "";

        textBox_mail.Text = "";

      }

    }

  }

}

用于发送的示例内容:

1

2

3

4

5

6

<html>

<body>

  <h2>C#发送html格式的邮件 </h2>

  <p style="background-color:green;width:200px;height:100px;color:white">HoverTreeTop</p>

</body>

</html>

以上就是使用C#实现发送自定义的html格式邮件的代码案例的详细内容!

相关阅读 >>

C#devexpress gridcontrol日期行的显示格式设置详解(图文)

详细介绍C#批量生成随机密码必须包含数字和字母并用加密算法加密的代码案例

C# 清除html标签标记

C#教程】C# 预处理器指令

详细介绍C#代码与javascript函数的相互调用

文件路径和文件夹路径在C#中使用浏览按钮获得的方法

C#模拟printscreen和alt+printscreen截取屏幕图片的示例代码分享(图)

C#中把image无损转换为icon的实例详解

C# winform程序上传图片到指定目录的示例代码

C#类的声明详解及实例

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...