C# 无法在c中使用smtp发送电子邮件#
我想我的系统发送电子邮件时,一个按钮被点击,但我不断得到一个问题在smtp。发送(消息)每次,没有错误或任何事情,它只是一种冻结,虽然它仍然在运行。我写了4次尝试,都没有成功 第一次尝试:C# 无法在c中使用smtp发送电子邮件#,c#,smtp,C#,Smtp,我想我的系统发送电子邮件时,一个按钮被点击,但我不断得到一个问题在smtp。发送(消息)每次,没有错误或任何事情,它只是一种冻结,虽然它仍然在运行。我写了4次尝试,都没有成功 第一次尝试: try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("robinx0852@
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("robinx0852@gmail.com");
mail.To.Add("robinx0852@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
MessageBox.Show("Email sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
//string s = $"User {edtID} has failed to login";
MailMessage message = new MailMessage();
message.To.Add("robinx0852@gmail.com");
message.Subject = "Employee login failure";
message.From = new MailAddress("armeepatel@gmail.com");
message.Body = "Hello";
SmtpClient smtp = new SmtpClient("smtp.saix.net");
smtp.Send(message);
MessageBox.Show("mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("mymail@gmail.com", "ME"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "Test";
smtp.Send(mailMessage);
// string s = $"User {edtID} has failed to login";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("mymail@gmail.com");
mail.From = new MailAddress("hotelfrontdesk@testhotel.com");
mail.Subject = "Employee login failure";
mail.IsBodyHtml = true;
mail.Body = "Hello";
SmtpServer.Host = "smtp.saix.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
var sysLogin = "yourlogin@gmail.com";
var sysPass = "y0urP@ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mymail#gmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
MessageBox.Show("Success");
第二次尝试:
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("robinx0852@gmail.com");
mail.To.Add("robinx0852@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
MessageBox.Show("Email sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
//string s = $"User {edtID} has failed to login";
MailMessage message = new MailMessage();
message.To.Add("robinx0852@gmail.com");
message.Subject = "Employee login failure";
message.From = new MailAddress("armeepatel@gmail.com");
message.Body = "Hello";
SmtpClient smtp = new SmtpClient("smtp.saix.net");
smtp.Send(message);
MessageBox.Show("mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("mymail@gmail.com", "ME"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "Test";
smtp.Send(mailMessage);
// string s = $"User {edtID} has failed to login";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("mymail@gmail.com");
mail.From = new MailAddress("hotelfrontdesk@testhotel.com");
mail.Subject = "Employee login failure";
mail.IsBodyHtml = true;
mail.Body = "Hello";
SmtpServer.Host = "smtp.saix.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
var sysLogin = "yourlogin@gmail.com";
var sysPass = "y0urP@ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mymail#gmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
MessageBox.Show("Success");
第三次尝试:
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("robinx0852@gmail.com");
mail.To.Add("robinx0852@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
MessageBox.Show("Email sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
//string s = $"User {edtID} has failed to login";
MailMessage message = new MailMessage();
message.To.Add("robinx0852@gmail.com");
message.Subject = "Employee login failure";
message.From = new MailAddress("armeepatel@gmail.com");
message.Body = "Hello";
SmtpClient smtp = new SmtpClient("smtp.saix.net");
smtp.Send(message);
MessageBox.Show("mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("mymail@gmail.com", "ME"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "Test";
smtp.Send(mailMessage);
// string s = $"User {edtID} has failed to login";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("mymail@gmail.com");
mail.From = new MailAddress("hotelfrontdesk@testhotel.com");
mail.Subject = "Employee login failure";
mail.IsBodyHtml = true;
mail.Body = "Hello";
SmtpServer.Host = "smtp.saix.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
var sysLogin = "yourlogin@gmail.com";
var sysPass = "y0urP@ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mymail#gmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
MessageBox.Show("Success");
第四次尝试:
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("robinx0852@gmail.com");
mail.To.Add("robinx0852@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
MessageBox.Show("Email sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
//string s = $"User {edtID} has failed to login";
MailMessage message = new MailMessage();
message.To.Add("robinx0852@gmail.com");
message.Subject = "Employee login failure";
message.From = new MailAddress("armeepatel@gmail.com");
message.Body = "Hello";
SmtpClient smtp = new SmtpClient("smtp.saix.net");
smtp.Send(message);
MessageBox.Show("mail Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("mymail@gmail.com", "ME"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "Test";
smtp.Send(mailMessage);
// string s = $"User {edtID} has failed to login";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("mymail@gmail.com");
mail.From = new MailAddress("hotelfrontdesk@testhotel.com");
mail.Subject = "Employee login failure";
mail.IsBodyHtml = true;
mail.Body = "Hello";
SmtpServer.Host = "smtp.saix.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
var sysLogin = "yourlogin@gmail.com";
var sysPass = "y0urP@ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mymail#gmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
MessageBox.Show("Success");
你试过这个吗?似乎您所有的尝试都使用SSL+25或587+NoSSL。SSL应与端口587连接
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = true,
Credentials = new NetworkCredential("Myemail@gmail.com", "MyPassword")
};
我曾经遇到过一个问题,在使用gmail的stmp设置时,我的电子邮件客户端在发送邮件时会冻结。然后,我必须在Gmail帐户设置中创建一个应用程序密码,并在stmp设置中使用该应用程序密码使我的客户端工作。试试这个,希望它能解决冻结问题
如果你的代码都被注释掉了,你希望它怎么做?如果您使用的是
SmtpClient
,如何“使用SQL发送电子邮件”?为什么要将SmtpClient
的实例调用为SmtpServer
?这似乎是倒退。它全部被注释掉了,因为我尝试了每一个单独的,它运行了所有的东西,但在smtp.Send(message)失败;请看一看:如果您每次尝试都包含异常消息,这将对我们很有帮助。@RandRandom我怀疑这是一个web应用程序,它冻结的行实际上是MessageBox.Show()行,因为它正在等待有人单击无人能看到的消息框上的“确定”按钮。谢谢Rahul。您能给我一些如何实现的指导吗?这取决于您是否启用了双因素身份验证。如果您确实有2FA,则需要此特定于应用程序的密码。如果您不使用2FA,则需要启用“不太安全的应用程序”,或更新您的C#代码以使用OAuth(并非微不足道)。@Armee我已更新了我的答案,但我的解决方案要求您的帐户启用两步验证。那么只有这样才能起作用。另外,gmail现在对SMTP访问非常挑剔。只有在启用了“不太安全的应用程序”(如果您未使用双因素身份验证)或使用特定于应用程序的密码(如果您使用的是双因素身份验证)的情况下,此功能才有效。