C#发送邮件
Posted x_蜡笔小新
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#发送邮件相关的知识,希望对你有一定的参考价值。
MailSend("公司", "发送邮箱", "发送密码", "姓名", "接收邮箱", "说明", "详细内容").ToString(); public bool MailSend(string txtName, string txtEmail, string txtEmailPwd, string txtFormName, string txtFormEmail, string txtTitle, string txtNeiRong) { string smtpName = "smtp." + txtEmail.Substring(txtEmail.LastIndexOf("@") + 1);//服务器 return NetMailSend(smtpName, txtEmail, txtEmailPwd, txtName, txtFormEmail, txtFormName, txtTitle, txtNeiRong, ""); } /// <summary> /// 发送邮件可带附件,System.Net.Mail命名空间 /// </summary> /// <param name="smtpName">邮件服务器地址</param> /// <param name="mailFrom">发件人邮箱地址</param> /// <param name="mailFromPwd">发件人邮箱密码</param> /// <param name="fromUserName">发件人名字</param> /// <param name="mailTo">收件人邮箱地址</param> /// <param name="toUserName">收件人名字</param> /// <param name="mailSubject">邮件主题</param> /// <param name="mailBody">邮件内容</param> /// <param name="strFileName">附件名</param> /// <returns>成功返回true,否则返回false</returns> public bool NetMailSend(string smtpName, string mailFrom, string mailFromPwd, string fromUserName, string mailTo, string toUserName, string mailSubject, string mailBody, string strFileName) { bool isSucceed = false;//发送邮件是否成功 //邮件发送时请确认服务的杀毒软件因素 System.Net.Mail.SmtpClient client; client = new System.Net.Mail.SmtpClient(smtpName); client.Timeout = 60000; //client.UseDefaultCredentials = false; client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential(mailFrom, mailFromPwd); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.From = new System.Net.Mail.MailAddress(mailFrom, fromUserName, System.Text.Encoding.UTF8); message.To.Add(new System.Net.Mail.MailAddress(mailTo, toUserName, System.Text.Encoding.UTF8)); message.IsBodyhtml = true; message.Subject = mailSubject;//邮件主题 message.Body = mailBody;//邮件内容 message.Priority = MailPriority.High; //发送附件 if (!string.IsNullOrEmpty(strFileName)) { Attachment data = new Attachment(strFileName);//附件 message.Attachments.Add(data); } try { client.Send(message); isSucceed = true; } catch (Exception ex) { isSucceed = false; //throw ex; } return isSucceed; }
以上是关于C#发送邮件的主要内容,如果未能解决你的问题,请参考以下文章