在C#asp.net MVC5中发送电子邮件 [英] send email in C# asp.net MVC5

查看:71
本文介绍了在C#asp.net MVC5中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我试图从gmail或hotmail发送电子邮件,雅虎到任何方向,但没有工作...

喜欢: http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail [ ^ ]

并且有我的gmail代码:

 尝试 
{

string 来自 = xxx@gmail.com;
// 将其替换为您自己的正确Gmail地址

string to = yyy@gmail.com;
// 将其替换为电子邮件地址
// 您要向其发送邮件

System.Net.Mail.MailMessage mail = < span class =code-keyword> new
System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress( from One Ghost,System.Text.Encoding.UTF8);
mail.Subject = 这是一个测试邮件;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = 这是电子邮件正文;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient( smtp.gmail.com 587 );

// 添加Creddentials-使用您自己的电子邮件ID和密码
System.Net.NetworkCredential nt =
new System.Net.NetworkCredential( from zzzzzz);

client.Port = 587 ; // Gmail适用于此端口
client.EnableSsl = ; // Gmail适用于服务器安全层
client.UseDefaultCredentials = ;
client.Credentials = nt;
client.Send(mail);

}
catch (例外情况)
{
Response.Write( 无法发送电子邮件 - 错误: + ex.Message);
}
return 查看();





错误说:

无法发送电子邮件 - 错误:失败发送邮件。



我应该在Web.config中添加一些内容进行配置吗?



谢谢!!

解决方案

您遇到的错误是由于网络问题导致的错误。



System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法连接到远程---> System.Net.Sockets.SocketException服务器:连接尝试期间发生错误,因为连接方在一段时间后没有正确响应或建立的连接发生错误,因为已连接的主机无法响应



这意味着公司要么没有收到您的网络请求,要么公司不想回复您的请求。确保添加请求处理所需的所有详细信息,例如在请求中启用SSL,添加端口等。



深入到例外,这个例外是因为端口。尝试更改端口,25个端口号适用于大多数SMTP服务器(对于gmail也是如此,我总是使用那个)。还要确保凭据正常。如果没有,请修复它们,如果是,那么网络可能会导致此问题,如服务器关闭或服务不适用于您所在地区的那种问题。



评论如果再次遇到问题,请提供更多信息。


错误说:

System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法连接到远程---> System.Net.Sockets.SocketException服务器:连接尝试期间发生错误,因为连接方在一段时间后没有正确响应或建立的连接发生错误,因为连接的主机无法响应....... .etc



请!!帮助...

Hi! I Tried to send a email from gmail or hotmail, yahoo to any directionm, but didnt work...
like: http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail[^]
and there is my code for gmail:

try
{

    string from = "xxx@gmail.com";
    //Replace this with your own correct Gmail Address

    string to = "yyy@gmail.com";
    //Replace this with the Email Address
    //to whom you want to send the mail

    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add(to);
    mail.From = new MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
    mail.Subject = "This is a test mail";
    mail.SubjectEncoding = System.Text.Encoding.UTF8;
    mail.Body = "This is Email Body Text";
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.IsBodyHtml = true;
    mail.Priority = MailPriority.High;

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

    //Add the Creddentials- use your own email id and password
    System.Net.NetworkCredential nt =
    new System.Net.NetworkCredential(from, "zzzzzz");

    client.Port = 587; // Gmail works on this port
    client.EnableSsl = true; //Gmail works on Server Secured Layer
    client.UseDefaultCredentials = false;
    client.Credentials = nt;
    client.Send(mail);

}
catch(Exception ex)
{
    Response.Write("Could not send the e-mail - error: " + ex.Message);
}
return View();



And error said:
Could not send the e-mail - error: Failure sending mail.

Should I add something in Web.config for configuration??

Thanks!!

解决方案

The error that you're having is the error due to network issues.

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: An error occurred during the connection attempt because the connected party did not properly respond after a period time or an error occurred in the connection established because connected host has failed to respond

It means, that the company is either not recieving your network request, or the company doesnot want to respond to your request. Make sure to add all the details required for the Request to be process, like Enabling SSL in the request, adding a port etc.

Going deep down into the exception, this exception is because of either the port. Try changing the port, 25 port number works for most the SMTP servers (so does for gmail, I always use that one). Also make sure the credentials are OK. If not, please fix them, if yes, then the network might be causing this problem like server down or service not available in your area sort of problem.

Comment for more information if you encounter problem again.


the error said:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote ---> System.Net.Sockets.SocketException server: An error occurred during the connection attempt because the connected party did not properly respond after a period time or an error occurred in the connection established because connected host has failed to respond........etc

please!! help...


这篇关于在C#asp.net MVC5中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆