一个C#发邮件模块,EmailHelper.cs 代码,是从一个C#工具包中拷贝出来的,在ASP.NET或C#项目中添加发送邮件功能时,这个类挺方便,使用时指定邮件标题、邮件正文、收件人、是否为HTML格式等参数即可,代码如下:
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05namespace CLB.Utility.Tools
06{
07 public class EmailHelper
08 {
09 ///<summary>
10 /// 发送邮件
11 ///<summary>
12 ///<param name="subject"> 邮件标题</param>
13 /// <param name="body">邮件正文</param>
14 /// <param name="to">收件人</param>
15 /// <param name="Ishtml">是否为html格式</param>
16 public static bool sendmail(string subject, string body, string to, bool Ishtml)
17 {
18 using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
19 {
20 msg.To.Add(to);
21 msg.From = new System.Net.Mail.MailAddress("邮箱地址", "标题", System.Text.Encoding.UTF8);
22 msg.Subject = subject;//邮件标题
23 msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
24 msg.Body = body;//邮件内容
25 msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
26 msg.IsBodyHtml = Ishtml;//是否是HTML邮件
27 msg.Priority = System.Net.Mail.MailPriority.High;//邮件优先级
28 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
29 client.Credentials = new System.Net.NetworkCredential("邮箱地址", "密码");
30 client.Host = "域名";
31 object userState = msg;
32 try
33 {
34 client.Send(msg);
35 return true;
36 }
37 catch
38 {
39 return false;
40 }
41 }
42 }
43 }
44}
中国足彩网信息请查看IT技术专栏
2025国考·省考课程试听报名