简体   繁体   English

无法在C#中使用smtp发送电子邮件

[英]Unable to send email using smtp in c#

I'd like my system to send email when a button is clicked, I however keep getting a problem at smtp.send(message) everytime, there is no error or anything, it just kind of freezes although its still running. 我希望我的系统在单击按钮时发送电子邮件,但是我每次都会在smtp.send(message)遇到问题,没有错误或任何东西,尽管它仍在运行,但只是冻结了。 I have written 4 attempts and all are unsuccessful. 我已经写了4次尝试,但都失败了。

1st attempt: 第一次尝试:

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);
}

2nd attempt: 第二次尝试:

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);

3rd attempt: 第三次尝试:

// 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);

}

4th attempt: 第四次尝试:

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");

Have you tried this? 你有尝试过吗? It seems all of your attempts had SSL+25, or 587+NoSSL. 您的所有尝试似乎都使用SSL + 25或587 + NoSSL。 SSL should be with port 587. 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")
};

I once had an issue where my email client would freeze on sending a mail when using stmp settings for gmail. 我曾经遇到一个问题,在为gmail使用stmp设置时,我的电子邮件客户端在发送邮件时会冻结。 I then had to create an App Password in Gmail Account settings and use that app-password in the stmp settings to make my client work. 然后,我不得不在Gmail帐户设置中创建一个应用密码,并在stmp设置中使用该应用密码来使我的客户端正常工作。 Try this, hope it fixes the freeze issue 试试这个,希望它能解决冻结问题

  1. Go To https://security.google.com/settings/security/apppasswords 转到https://security.google.com/settings/security/apppasswords
  2. Select and App[Custom, if any] , Device [Custom, if any] and then Click on Generate. 选择并选择应用程序[自定义(如果有)],设备[自定义(如果有)],然后单击生成。
  3. A password will be generated something like this : kxyo gmnz dais wcau 将会生成类似以下的密码:kxyo gmnz dais wcau
  4. Use this app-password instead of the password you are using right now in stmp settings. 使用此应用密码,而不是您现在在stmp设置中使用的密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM