Skip to main content
 首页 » 编程设计

c#之通过 C# 发送邮件

2024年01月13日31emanlee

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

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