【问题标题】:Send system administrator email in C# [duplicate]用 C# 发送系统管理员电子邮件 [重复]
【发布时间】:2020-08-01 00:23:44
【问题描述】:

登录表单

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

【问题讨论】:

  • 这能回答你的问题吗? Sending E-mail using C#
  • 不,这不起作用
  • 你有什么问题?
  • 这些代码根本不起作用
  • System.Net.Mail.SmtpException: 'SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:需要 5.7.0 身份验证。

标签: c# windows visual-studio winforms email


【解决方案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 帐户中启用不太安全的登录(或不太安全的应用访问)。

登录谷歌账号后,前往:

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

https://myaccount.google.com/lesssecureapps

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多