当前位置:编程学习 > C#/ASP.NET >>

C#发送Email的3种途径实例讲解

通过.Net FrameWork 2.0下提供的“System.Net.Mail”可以轻松的实现,本文列举了3种途径来发送:
1.通过Localhost;
2.通过普通SMTP;
3.通过SSL的SMTP;

下面一个一个来说:

1.通过LocalHost
public void SendMailLocalhost()
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("a@a.com ");
            msg.To.Add("b@b.com ");
            /*
             * msg.To.Add("b@b.com");
             * msg.To.Add("b@b.com");
             * msg.To.Add("b@b.com");可以发送给多人
             */
            msg.CC.Add("c@c.com ");
            /*
             * msg.CC.Add("c@c.com");
             * msg.CC.Add("c@c.com");可以抄送给多人
             */            
            msg.From = new MailAddress("a@a.com ", "AlphaWu ", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
            msg.Subject = "这是测试邮件 ";//邮件标题            
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
            msg.Body = "邮件内容 ";//邮件内容
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
            msg.IsBodyHtml = false ;//是否是HTML邮件
            msg.Priority = MailPriority.High;//邮件优先级
 
            SmtpClient client = new SmtpClient();
            client.Host = "localhost ";
            object userState = msg;
            try
            {                
                client.SendAsync(msg, userState);
                //简单一点儿可以client.Send(msg);
                MessageBox.Show("发送成功 ");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错 ");
            }     
        }

2.通过普通SMTP
public void SendMailUseZj()
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("a@a.com ");
            msg.To.Add("b@b.com ");
            /*
             * msg.To.Add("b@b.com");
             * msg.To.Add("b@b.com");
             * msg.To.Add("b@b.com");可以发送给多人
             */
            msg.CC.Add("c@c.com ");
            /*
             * msg.CC.Add("c@c.com");
             * msg.CC.Add("c@c.com");可以抄送给多人
             */
            msg.From = new MailAddress("a@a.com ", "AlphaWu ", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
            msg.Subject = "这是测试邮件 ";//邮件标题            
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
            msg.Body = "邮件内容 ";//邮件内容
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
            msg.IsBodyHtml = false ;//是否是HTML邮件
            msg.Priority = MailPriority.High;//邮件优先级
 
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("username@zj.com ", "userpass ");
            //在zj.com注册的邮箱和密码
            client.Host = "smtp.zj.com ";
            object userState = msg;
            try
            {
                client.SendAsync(msg, userState);
                //简单一点儿可以client.Send(msg);
                MessageBox.Show("发送成功 ");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
             &
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,