本篇文章为大家展示了C#中怎么发送Email邮件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
C#发送Email邮件方法之一:通过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.CC.Add("c@c.com"); msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8); 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, "发送邮件出错"); } }
C#发送Email邮件方法之二:通过普通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.CC.Add("c@c.com"); msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8); 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) ...{ MessageBox.Show(ex.Message, "发送邮件出错"); } }
上述方法不适用于所有SMTP,经测试zj.com可以,而smtp.163.com不行
C#发送Email邮件方法之三:通过SSL的SMTP
public void SendMailUseGmail() ...{ System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.To.Add("a@a.com"); msg.To.Add("b@b.com"); msg.CC.Add("c@c.com"); msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8); 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@gmail.com", "password"); //上述写你的GMail邮箱和密码 client.Port = 587;//Gmail使用的端口 client.Host = "smtp.gmail.com"; client.EnableSsl = true;//经过ssl加密 object userState = msg; try ...{ client.SendAsync(msg, userState); //简单一点儿可以client.Send(msg); MessageBox.Show("发送成功"); } catch (System.Net.Mail.SmtpException ex) ...{ MessageBox.Show(ex.Message, "发送邮件出错"); } }
上述内容就是C#中怎么发送Email邮件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。