无法使用c#

时间:2017-06-27 15:53:13

标签: c# smtp

我喜欢我的系统在点击按钮时发送电子邮件,但是我每次都在smtp.send(消息)时遇到问题,没有任何错误或任何事情,它只是冻结了,尽管它仍然是运行。我写了4次尝试,但都没有成功。

第一次尝试:

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

第3次尝试:

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

}

第4次尝试:

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

2 个答案:

答案 0 :(得分:1)

你试过这个吗?似乎所有的尝试都有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")
};

答案 1 :(得分:1)

我曾经遇到过一个问题,我的电子邮件客户端会在使用gmp的stmp设置时冻结发送邮件。然后,我必须在Gmail帐户设置中创建应用程序密码,并在stmp设置中使用该应用程序密码以使我的客户端正常工作。试试这个,希望它能解决冻结问题

  1. 转到https://security.google.com/settings/security/apppasswords
  2. 选择并应用[自定义,如果有],设备[自定义,如果有],然后单击生成。
  3. 将生成如下密码:kxyo gmnz dais wcau
  4. 使用此应用密码,而不是您现在正在使用的密码设置。