c#发送邮件

如果使用公司内部服务器发送邮件,需要搭建服务器

https://www.landui.com/help/show-1063.html

 

从数据库读取相关信息

CREATE TABLE [dbo].[Config](
    [Category] [nvarchar](100) NOT NULL,
    [Type] [nvarchar](200) NOT NULL,
    [Value] [nvarchar](max) NOT NULL,
    [Memo] [nvarchar](50) NULL,
 CONSTRAINT [PK_Config_1] PRIMARY KEY CLUSTERED 
(
    [Category] ASC,
    [Type] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

        /// <summary>
        /// 发送邮件,附件方面代码未添加
        /// </summary>
        /// <param name="mailto">目标邮箱 分号隔开可填写多个邮箱</param>
        /// <param name="body">发送内容</param>
        /// <returns></returns>
        public static string SendMail(string mailto,string mailcc, string maildesc, string subject, string body)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                maildesc = maildesc.Trim() == "" ? Config.GetValue("Mail", "Desc") : maildesc.Trim();
                //发送使用的邮箱
                mailMsg.From = new MailAddress(Config.GetValue("Mail", "Mail"), maildesc);
                foreach (string s in mailto.Split(';'))
                {
                    if (s.Trim().Contains("@"))
                    {
                        mailMsg.To.Add(new MailAddress(s.Trim()));
                    }
                }
                foreach (string s in mailcc.Split(';'))
                {
                    if (s.Trim().Contains("@"))
                    {
                        mailMsg.CC.Add(new MailAddress(s.Trim()));
                    }
                }
                mailMsg.Subject = subject;
                mailMsg.Body = body;
                mailMsg.Priority = MailPriority.High;

                //smtp服务器
                SmtpClient spClient = new SmtpClient(Config.GetValue("Mail", "Server"));

                //验证ID密码
                spClient.Credentials = new System.Net.NetworkCredential(Config.GetValue("Mail", "UserName"), Config.GetValue("Mail", "Password"));
                spClient.Send(mailMsg);
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {

            }
        }


private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SendMail("1203765096@qq.com", "", "test", "c#邮件发送", content1.Text);
        }

        /// <summary>
        /// 发送邮件,附件方面代码未添加
        /// </summary>
        /// <param name="mailto">目标邮箱 分号隔开可填写多个邮箱</param>
        /// <param name="body">发送内容</param>
        /// <returns></returns>
        public static string SendMail(string mailto, string mailcc, string maildesc, string subject, string body)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                //maildesc = maildesc.Trim() == "" ? Config.GetValue("Mail", "Desc") : maildesc.Trim();
                //发送使用的邮箱
                mailMsg.From = new MailAddress("1203765096@qq.com", maildesc);
                foreach (string s in mailto.Split(';'))
                {
                    if (s.Trim().Contains("@"))
                    {
                        mailMsg.To.Add(new MailAddress(s.Trim()));
                    }
                }
                foreach (string s in mailcc.Split(';'))
                {
                    if (s.Trim().Contains("@"))
                    {
                        mailMsg.CC.Add(new MailAddress(s.Trim()));
                    }
                }
                mailMsg.Subject = subject;
                mailMsg.Body = body;
                mailMsg.Priority = MailPriority.High;

                //smtp服务器
                SmtpClient spClient = new SmtpClient("smtp.laibao.com.cn");
                //验证ID密码
                spClient.Credentials = new System.Net.NetworkCredential("SCP@laibao.com.cn", "SCPscp123");                
                spClient.Send(mailMsg);
                MessageBox.Show("发送成功!");
                return "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return ex.Message;
            }
            finally
            {

            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值