电子邮件功能无法在C#中运行 [英] Email functionality not working in C#
问题描述
我正在尝试为我开发的网站实现自动电子邮件功能。在最初的基础上,我使用Gmail凭据测试了电子邮件代码,它运行良好。以下代码工作正常。
public static int SendEmail( string 来自, string to, string subject, string body)
{
int result = 0 ;
MailMessage msg = new MailMessage();
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential( MyEmailID@gmail.com, MyGmailPassword);
SmtpServer.Port = 587 ;
SmtpServer.Host = smtp.gmail.com;
SmtpServer.EnableSsl = true ;
mail = new MailMessage();
尝试
{
mail.From = 新 MailAddress(< span class =code-sdkkeyword> from , ,System.Text。 Encoding.UTF8);
mail.To.Add(to);
mail.Subject = subject;
mail.IsBodyHtml = true ;
mail.Body = body;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyToList.Add( MyEmailID@gmail.com);
SmtpServer.Send(mail);
result = 1 ; // ---------消息成功发送
}
catch (例外情况)
{
result = 2 ; // ---------发送消息时发生错误
}
返回结果;
}
当我使用上述代码的客户端电子邮件设置时,它无效。客户向我提供了以下信息。
官方EmailID /密码:两个不同的电子邮件地址及其密码
SMTP:
外发服务器:用于SSL和非SSL设置的两个不同服务器
SMTP端口:587和465
我尝试了所有可能的电子邮件/密码组合,SMTP端口..但是没有工作。
这些组合我得到两种错误......
错误1:System.Security.Authentication.AuthenticationException:根据验证程序,远程证书无效。
错误2:操作已超时。
但是我可以使用提供的凭据登录webmail,所以我猜两个emailID和它们各自的密码都是正确的。
任何帮助将不胜感激。
谢谢和问候,
Sam
请看看以下链接,你会有所了解。
此AuthenticationException错误的解决方案 [ ^ ]
远程证书根据验证程序无效 [ ^ ]
使用c在asp.net中发送邮件错误# [ ^ ]
它有效,我添加了System.Net命名空间和System.Net.Mail命名空间......
I am trying to implement automatic email functionality for a website I developed. On initial basis I tested email code with Gmail credentials and it worked fine. Below is the code which is working fine.
public static int SendEmail(string from, string to, string subject, string body)
{
int result = 0;
MailMessage msg = new MailMessage();
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("MyEmailID@gmail.com", "MyGmailPassword");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
try
{
mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8);
mail.To.Add(to);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyToList.Add("MyEmailID@gmail.com");
SmtpServer.Send(mail);
result = 1; //--------- MESSAGE SENT SUCCESSFULLY
}
catch (Exception ex)
{
result = 2; //--------- ERROR OCCURRED WHILE SENDING MESSAGE
}
return result;
}
When I used client's email settings with above code, it is not working. Client has provided me with below information.
Official EmailID/Password: Two different email address and their passwords
SMTP:
Outgoing Server: Two different server for SSL and NON SSL settings
SMTP PORT: 587 and 465
I tried all the possible combination of email/password, SMTP port..etc but none is working.
I am getting two kinds of error with these combinations...
err 1: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
err 2: The operation has timed out.
But I am able to login on webmail with the credentials provided, so I guess both the emailIDs, and their respective passwords are correct.
Any help would be appreciated.
Thanks and regards,
Sam
Please have a look on the following links, you will get some idea.
Solution for this AuthenticationException error[^]
the remote certificate is invalid according to the validation procedure[^]
mail sending error in asp.net with c#[^]
It worked, I added System.Net Namespace along with System.Net.Mail namespace...
这篇关于电子邮件功能无法在C#中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!