C#简单发送email的方法
程序员文章站
2024-01-05 12:06:22
本文实例讲述了c#简单发送email的方法。分享给大家供大家参考。具体实现方法如下:
#region 邮件发送
///
///...
本文实例讲述了c#简单发送email的方法。分享给大家供大家参考。具体实现方法如下:
#region 邮件发送 /// <summary> /// 发送邮件支持html格式 先引用system.net.mail; /// </summary> /// <param name="mailfrom">发送人email 如demo@126.com</param> /// <param name="mailserver">邮件服务器 如mail.126.com</param> /// <param name="mailusername">登陆用户名 如demo@126.com</param> /// <param name="mailpassword">登陆密码 如123456</param> /// <param name="mailto">收件人邮箱</param> /// <param name="title">邮件标题</param> /// <param name="msg">邮件正文</param> /// <returns>成功 true; 失败 false</returns> public static bool sendsmtpemail(string mailfrom,string mailserver,string mailusername,string mailpassword,string mailto,string title,string msg) { var client = new smtpclient(); client.host = mailserver; client.usedefaultcredentials = false; client.credentials = new system.net.networkcredential(mailusername, mailpassword); client.deliverymethod = smtpdeliverymethod.network; system.net.mail.mailmessage message = new mailmessage(mailfrom, mailto); message.subject = title; message.body = msg; message.subjectencoding = system.text.encoding.utf8; message.bodyencoding = system.text.encoding.utf8; message.isbodyhtml = true; message.priority = mailpriority.high; message.isbodyhtml = true; try { client.send(message); return true; } catch { return false; } } #endregion
希望本文所述对大家的c#程序设计有所帮助。