我试图发送一个基本的电子邮件,使用C#mvc ...由于某种原因它不会工作..我尝试了许多不同的解决方案,但在浏览器中继续得到相同的错误,说“操作已经超时”..其中我认为是一个连接问题。错误从“client.Send(mail);我在我的本地机器上执行此操作。我尝试使用端口587,465,995,25,但没有运气。我在Visual Studio的编译器中,当我构建它时,我没有得到任何错误。我只需在网络浏览器中获取错误“client.Send(mail);”
控制器:
public void SendEmail(History hi)
{
SmtpClient client = new SmtpClient(); //server type
client.Port = 587;
client.Host = "smtp.gmail.com"; //google
client.EnableSsl = true; //SSl set to true
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("papasmurf985@gmail.com", "******");
MailMessage mail = new MailMessage("papasmurf985@gmail.com", "papasmurf985@gmail.com", "Test Score", "Your score is" + hi.score);
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mail);
}
的web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" />
</smtp>
</mailSettings>
</system.net>
我也在web.config中试过这个:
<system.net>
<mailSettings>
<smtp from="no-reply@no-reply.com">
<network host="smtp.gmail.com" port="587" enableSsl="true"
userName="papasmurf985@gmail.com" password="PASSWORD" />
</smtp>
</mailSettings>
</system.net>
答案 0 :(得分:1)
请尝试以下方式
public void SendMessage(string toAddress, string subjectText, string bodyText)
{
//create a object to hold the message
MailMessage newMessage = new MailMessage();
//Create addresses
MailAddress senderAddress = new MailAddress(your_gmail_address);
MailAddress recipentAddress = new MailAddress(toAddress);
//Now create the full message
newMessage.To.Add(recipentAddress);
newMessage.From = senderAddress;
newMessage.Subject = subjectText;
newMessage.Body = bodyText;
//Create the SMTP Client object, which do the actual sending
SmtpClient client = new SmtpClient(your_gmail_server_address, your_gmail_port)
{
Credentials = new NetworkCredential(your_gmail_address, your_password),
EnableSsl = true
};
//now send the message
client.Send(newMessage);
}
此代码对我有用
答案 1 :(得分:0)
我首先要确保您的ISP不会阻止外部SMTP服务器