发送和接收邮箱要选择有smtp服务的邮箱,如:qq.com和163.com等
1.首先发送邮箱要开通smtp服务(找不到开启位置的可以百度一下,这里不做赘述)
2.如何生成授权码
3.Demo源码
public string smtpService = "smtp.qq.com";//发送邮箱的smtp服务地址 public string sendEmail = "xxxx@qq.com";//发送邮箱的账号 public string sendpwd = "xxxxx";//发送邮箱的密码 public string userPassword = "oelovozltpffbacf";//发送邮箱的授权码 public string Email() { try { string Mai = "xxxx@qq.com,xxxx@qq.com";//接收人邮箱(这里定义了多个循环发送,也可以定义一个去掉循环) string[] address = Mai.Split(','); for (int i = 0; i < address.Length; i++) { SmtpClient smtpClient = new SmtpClient(); smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = false; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 smtpClient.Host = smtpService; //指定SMTP服务器 smtpClient.Credentials = new System.Net.NetworkCredential(sendEmail, userPassword);//用户名和授权码 // 发送邮件设置 MailMessage mailMessage = new MailMessage(sendEmail, address[i]); // 发送人和收件人 mailMessage.Subject = "";//邮件主题 mailMessage.Body = "";//邮件内容 mailMessage.BodyEncoding = Encoding.UTF8;//正文编码 mailMessage.IsBodyHtml = true;//设置为HTML格式 mailMessage.Priority = MailPriority.Low;//优先级 smtpClient.Send(mailMessage); } return "发送成功"; } catch (Exception) { return "邮件发送异常"; } }