c# - 通过 C# 发送邮件

标签 c# .net email

我正在尝试使用 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

最佳答案

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

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

关于c# - 通过 C# 发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39291207/

相关文章:

PHP-发送每日电子邮件

c# - 终止进程以 System.Diagnostic.Process.Start ("FileName"开始)

c# - 单例类和多线程

c# - ASP.NET Core 2.0 : static file (site. min.js) 找不到

c# - 一张表中的一条记录与另一张表中的多条记录之间的关系

c# - ServiceStack OrmLite : Use default database constraint instead of null value from data model

c# - 使用 C# System.DirectoryServices 在远程 Windows 服务器上进行用户管理

html - 如何在电子邮件 Html 模板中将 td 中的多个表格居中对齐

PHP 邮件脚本每晚同一时间自动运行

c# - 表达式翻译的自动化测试