C#使用自带的email组件发送邮件的方法
本文实例讲述了C#使用自带的email组件发送邮件的方法。分享给大家供大家参考。具体实现方法如下:
#region邮件发送 ///<summary> ///发送邮件支持HTML格式先引用System.Net.Mail; ///</summary> ///<paramname="MailFrom">发送人Email如demo@126.com</param> ///<paramname="MailServer">邮件服务器如mail.126.com</param> ///<paramname="MailUserName">登陆用户名如demo@126.com</param> ///<paramname="MailPassword">登陆密码如123456</param> ///<paramname="mailto">收件人邮箱</param> ///<paramname="title">邮件标题</param> ///<paramname="msg">邮件正文</param> ///<returns>成功true;失败false</returns> publicstaticboolSendSmtpEmail(stringMailFrom,stringMailServer,stringMailUserName,stringMailPassword,stringmailto,stringtitle,stringmsg) { varclient=newSmtpClient(); client.Host=MailServer; client.UseDefaultCredentials=false; client.Credentials=newSystem.Net.NetworkCredential(MailUserName,MailPassword); client.DeliveryMethod=SmtpDeliveryMethod.Network; System.Net.Mail.MailMessagemessage=newMailMessage(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); returntrue; } catch { returnfalse; } } #endregion
希望本文所述对大家的C#程序设计有所帮助。