【问题标题】:Sending mail via C#通过 C# 发送邮件
【发布时间】:2016-09-02 11:25:30
【问题描述】:

我正在尝试使用 gmail stmp 服务器通过 C# 向自己发送一封电子邮件,但我收到了一封来自 gmail 团队的电子邮件,内容是 “Google 刚刚阻止了一个不太安全的应用程序访问您的 Google 帐户。” 。现在我已更改设置以允许不太安全的应用登录谷歌,但我无法向自己发送电子邮件。以下是我的代码。

    private static string sendMail(System.Net.Mail.MailMessage mm)
    {
        try
        {
            string smtpHost = "smtp.gmail.com";
            string userName = "myemail@gmail.com";//write your email address
            string password = "xxxxxx";//write password
            System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
            mClient.Port = 587;
            mClient.EnableSsl = true;
            mClient.UseDefaultCredentials = false;
            mClient.Credentials = new NetworkCredential(userName, password);
            mClient.Host = smtpHost;
            mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            mClient.Send(mm);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        return "Send Sucessfully";
    }
private void f()
      {
          i = i + 1;
        string sysName = string.Empty;
        string sysUser = string.Empty;
        System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("myemail@gmail.com");
        System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("myemail@gmail.com");
        System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);
        sysName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        sysUser = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
        mm.Subject = sysName + " " + sysUser;
        string filename = string.Empty;
        mm.IsBodyHtml = true;
        mm.BodyEncoding = System.Text.Encoding.UTF8;
        MessageBox.Show(sendMail(mm).ToString());
        //sendMail(mm);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        f();
    }

我也尝试了我的雅虎帐户。然后我也没有收到任何电子邮件。 我已将端口更改为 465,将 smtphost 更改为“mail.yahoo.com”,并将电子邮件地址更改为 mymail@yahoo.com

【问题讨论】:

  • 如果您在世界上最喜欢的搜索引擎中搜索“Google 刚刚阻止了一个不太安全的应用程序访问您的 Google 帐户”,那么顶部的链接会非常清楚地说明该怎么做。 Allowing less secure apps to access your account。您对此解决方案是否不满意?
  • @spender 我刚刚做了,得到了两个结果,它们都没有多大帮助。
  • 那么,打开帐户设置“访问不太安全的应用程序”对您不起作用?你肯定做到了,是吗?
  • 这是否意味着即使在谷歌帐户中启用“允许不太安全的应用程序”后它仍然可以工作?或者您是否正在寻找更好的解决方案而不影响安全性?
  • 如果打开“访问安全性较低的应用程序”选项不好,看起来你必须做一个 oauth dance... 以下是详细信息:developers.google.com/gmail/oauth_overview

标签: c# .net email


【解决方案1】:
protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString(); 
    //Password of your gmail address
    const string fromPassword = "Password";
     // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

您已使用此代码。 它正在运行代码。

【讨论】:

    【解决方案2】:

    转到谷歌帐户设置,并允许不太安全的应用程序。 https://myaccount.google.com/u/1/security?utm_source=OGB#signin 您需要使用底部的“允许安全性较低的应用”滑块。

    默认 google SMTP 设置为 here

    【讨论】:

    • 您需要改进此答案。也许一些关于如何在谷歌做到这一点的步骤?也许一些支持文档?通常(尽管并非总是如此),当它是一个句子时,您可以告诉您需要改进您的答案,尤其是当它甚至没有换行到两行时。不过,这只是我的经验法则。
    • 是的,正在查看@google 帐户设置以查找直接链接。
    • 例如,如果您在 OP 的问题下复制/粘贴 spender 的评论,这已经是一个更好的答案(我并不是说您应该抄袭他...)
    • 复制粘贴没有意义 :) 只要它可以帮助他/她解决问题 - 这就是最重要的。
    • 链接文章没有回答问题。
    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2014-02-21
    • 2011-12-28
    • 2013-08-06
    • 2011-01-04
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多