//***重点:需要开启服务器STMP或其他 string host = "smtp.163.com";// 邮件服务器smtp.163.com表示网易邮箱服务器 string userName = "manlong@test.com";// 发送端账号 string password = "test1";// 第三方授权码或发送端密码(这个客户端重置后的密码)
SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 client.Host = host;//邮件服务器 client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential(userName, password);//用户名、密码(邮箱第三方登录授权码)
// string strfrom = userName; string strto = "1234@qq.com"; string strcc = "manlong@test.cn";//抄送
string subject = "测试邮件标题";//邮件的主题 string body = "测试邮件内容";//发送的邮件正文
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(strfrom, "manlong");//发送人 msg.To.Add(strto); msg.CC.Add(strcc);
msg.Subject = subject;//邮件标题 msg.Body = body;//邮件内容 msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 msg.IsBodyHtml = true;//是否是HTML邮件 msg.Priority = MailPriority.High;//邮件优先级 string rt = "";
try { client.Send(msg); rt = "发送成功"; } catch (System.Net.Mail.SmtpException ex) { rt = ex.Message; } //Console.WriteLine(rt);