使用C#向系统管理员发送电子邮件

时间:2020-04-18 13:06:32

标签: c# windows visual-studio winforms email

登录表格

enter image description here

嗨 点击表单上的链接标签后 自动邮件仅发送到1个特定的电子邮件地址 电子邮件主题:重置[用户名]的密码 电子邮件正文:请重设用户[用户名]的密码

1 个答案:

答案 0 :(得分:1)

请阅读到最后,您一定可以解决您的问题。

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("youremailid", "youremailpassword");
   mySmtpClient.Credentials = basicAuthenticationInfo;
   mySmtpClient.EnableSsl = true;
   // add from,to mailaddresses
   MailAddress from = new MailAddress("youremailid@example.com", "TestFromName");
   MailAddress to = new MailAddress("toemailid@example.com", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyto = new MailAddress("reply@example.com");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Reset password for [username]";

   // set body-message and encoding
   myMail.Body = "Please reset password for user [username]";

   mySmtpClient.Send(myMail);
}

catch (SmtpException ex)
{
  throw new ApplicationException
    ("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
   throw ex;
}

Google可能会阻止某些未使用现代安全标准的应用或设备的登录尝试。由于这些应用程序和设备更容易被破解,因此屏蔽它们可以使您的帐户更安全。

因此,您必须在Google帐户中启用“不太安全的登录”(或“不太安全的应用程序访问”)。

登录到Google帐户后,转到:

https://www.google.com/settings/security/lesssecureapps

https://myaccount.google.com/lesssecureapps

上面的代码来自@NajiMakhoul的链接:) Sending E-Mail using C#,其余信息来自此链接:SMTP Error C#