我已经尝试了2天让我的ASP.NET webforms应用程序发送电子邮件。
我使用outlook和gmail尝试了这个。我从this tutorial获得了两者的smtp信息:
当我在下面的示例中尝试使用端口587时,我收到错误消息: 类型为' System.Net.Mail.SmtpException'发生在System.dll中但未在用户代码中处理 附加信息:SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。
当我尝试使用以下示例中的端口465时: 我的应用程序永远挂起,我的页面永远不会收回我在PageLoad中的电子邮件功能。
需要注意的一件事只是让其中一个搞砸了我:
我甚至前往google DisplayUnlockCaptcha page以允许访问我的帐户
protected void SendEmail()
{
string EmailAddress = "myemail@gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
答案 0 :(得分:1)
此代码应该可以正常使用
protected void SendEmail()
{
string EmailAddress = "myemail@gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
您需要设置投放模式,否则gmail会返回错误
编辑:
投掷'使用'围绕' MailMessage'也许是一件聪明的事情
答案 1 :(得分:1)
事实证明,这是因为GMail安全设置。 https://www.google.com/settings/security/lesssecureapps
您必须为不太安全的应用启用访问权限。
答案 2 :(得分:0)
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx@gmail.com");
mail.From = new MailAddress("yyy@gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy@gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/