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;
        }

  

原文地址:https://www.cnblogs.com/XuPengLB/p/6396822.html