mailMessage.From = new MailAddress(fromEmailAddress);
mailMessage.Subject = "Test";
mailMessage.Body = "Manish";
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(toEmailAddress));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = userNAme;
NetworkCred.Password = password;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Send(mailMessage);
我正在尝试配置Gmail SMTP并尝试使用TSL和SSL,但上面的代码总是抛出TimeOut异常
答案 0 :(得分:0)
我认为您需要将usedefaultcredentials设置为false。否则,将不会使用您在网络凭据中设置的用户名/密码。
答案 1 :(得分:0)
您不希望为SmtpClient将UseDefaultCredentials设置为true,因为它将使用当前登录用户的凭据,按https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials%28v=vs.110%29.aspx
此外,通过web.config配置设置可能更有意义,因为任何更改都不需要重新编译和部署代码。类似的问题在这里回答:Send Email via C# through Google Apps account
如果您选择在代码中保留smtp设置,它应如下所示:
mailMessage.From = new MailAddress(fromEmailAddress);
mailMessage.Subject = "Test";
mailMessage.Body = "Manish";
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(toEmailAddress));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(userNAme, password);
smtp.Send(mailMessage);