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#发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

C#中发送邮件,包含Html代码

从c#代码发送电子邮件[重复]

c#代码怎么通过outlook发邮件

ASP .Net C# 代码向多个用户发送邮件,这些用户的邮件 ID 应隐藏在组名中

使用C#模拟Outlook发送邮件,代码编译报错

C#发送邮件