//第一种
MailMessage mm = new MailMessage();
MailAddress Fromma = new MailAddress("cat@mycat1314.com");
MailAddress Toma = new MailAddress(Email, null);
mm.From = Fromma;
//收件人
mm.To.Add(Email);
//邮箱标题
mm.Subject = "【CAT】验证码";
mm.IsBodyHtml = true;
//邮件内容
mm.Body = "您好,您的验证码为:" + code + "";
//内容的编码格式
mm.BodyEncoding = System.Text.Encoding.UTF8;
//mm.ReplyTo = Toma;
//mm.Sender =Fromma;
//mm.IsBodyHtml = false;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
mm.CC.Add(Toma);
SmtpClient sc = new SmtpClient();
NetworkCredential nc = new NetworkCredential();
nc.UserName = "cat@mycat1314.com";//你的邮箱地址
nc.Password = "******";//你的邮箱密码
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
sc.Host = "smtp.gmail.com";
sc.Send(mm);
//第二种
//string addressFrom = "cat@mycat1314.com";
//string userID = "cat@mycat1314.com";
//string userPassword = "*****";
//SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
//mailClient.EnableSsl = true;
//System.Net.NetworkCredential crendetial = new NetworkCredential(userID, userPassword);
//mailClient.Credentials = crendetial;
//MailMessage message = new MailMessage(addressFrom, Email, "【CAT】验证码", "您好,您的验证码为:" + code + "");
//message.IsBodyHtml = true;
//mailClient.Send(message);
每次请求返回的是
System.Net.Mail.SmtpException: SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为:5.5.1 Authentication Required. Learn more at
在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
在 System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
在 SendCodeBLL.SendEmial(String Email, String code)
目前Gmail设置已经启用了POP
记录第二天 给谷歌帐号开了两步验证,在用了应用专业密码,自己的账户成功发送了邮件,但是使用公司的企业邮箱,同样的操作还是发送失败。。。。继续研究