本文主要是介绍C# 简单发送邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//Email 实体类public class MyEmail{/// <summary>/// 发送方发送方服务器地址/// </summary>public string strHost { get; set; }/// <summary>/// 发送方帐号/// </summary>public string strAccount { get; set; }/// <summary>/// 发送方密码/// </summary>public string strPwd { get; set; }/// <summary>/// 发送方邮件地址/// </summary>public string strFrom { get; set; }/// <summary>/// 接收方邮件地址/// </summary>public string to { get; set; }/// <summary>/// 邮件标题/// </summary>public string title { get; set; }/// <summary>/// 邮件正文内容/// </summary>public string content { get; set; }/// <summary>/// 附件/// </summary>public string sfile { get; set; }}
实现方法:
//发送Email公共方法public class SendEmailHelp{/// <summary>/// 发送邮件/// </summary>/// <param name="strHost">发送方发送方服务器地址</param>/// <param name="strAccount">发送方帐号</param>/// <param name="strPwd">发送方密码</param>/// <param name="strFrom">发送方邮件地址</param>/// <param name="to">接收方邮件地址</param>/// <param name="title">邮件标题</param>/// <param name="content">邮件正文内容</param>/// <param name="sfile">邮件附件</param>/// <returns></returns>public bool sendMail(MyEmail e)//string strHost, string strAccount, string strPwd, string strFrom, string to, string title, string content{SmtpClient smtpClient = new SmtpClient();if (string.IsNullOrEmpty(e.strHost)){e.strHost = "smtp.sina.cn";//新浪测试}if (string.IsNullOrEmpty(e.strAccount)){e.strAccount = "xxx";}if (string.IsNullOrEmpty(e.strPwd)){e.strPwd = "xxx";}if (string.IsNullOrEmpty(e.strFrom)){e.strFrom = "xxx";}smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式smtpClient.Host = e.strHost; ;//指定SMTP服务器smtpClient.Credentials = new System.Net.NetworkCredential(e.strAccount, e.strPwd);//用户名和密码MailMessage mailMessage = new MailMessage(e.strFrom, e.to);mailMessage.Subject = e.title;//主题mailMessage.Body = e.content;//内容mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码mailMessage.IsBodyHtml = true;//设置为HTML格式mailMessage.Priority = MailPriority.High;//优先级//// 添加附件if (e.sfile != ""){mailMessage.Attachments.Add(new Attachment(e.sfile));}try{smtpClient.Send(mailMessage);mailMessage.Dispose();//释放资源return true;}catch{return false;}}}
这篇关于C# 简单发送邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!