如何通过Gmail使用C#发送电子邮件 [英] How to send an e-mail with C# through Gmail

查看:150
本文介绍了如何通过Gmail使用C#发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的网络服务发送电子邮件时出现错误。我曾尝试启用对安全性较低的应用的访问权限,禁用两步验证并通过网络浏览器登录帐户。 SO上的解决方案都没有为我工作。我仍然收到:


错误:System.Net.Mail.SmtpException:SMTP服务器需要
安全连接或客户端未通过身份验证。服务器
的响应是:5.5.1需要身份验证。


我该如何解决这个问题?

  namespace EmailService 
{
public class Service1:IService1
{
public string SendEmail( string inputEmail,string subject,string body)
{
string returnString =;
尝试
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host =smtp.gmail.com;

//设置Gmail服务器
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(myemail@gmail.com,mypassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;

//起草电子邮件
MailAddress fromAddress = new MailAddress(cse445emailservice@gmail.com);
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;

smtp.Send(email);

returnString =成功!请检查您的电子邮件。;

catch(Exception ex)
{
returnString =Error:+ ex.ToString();
}
return returnString;




解决方案

只需转到此处:安全性较低的应用程序,使用用于发送邮件的电子邮件和密码登录在您的C#代码中,并选择打开



另请点击此链接并点击继续 允许访问您的Google帐户



我也编辑一点:

  public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage ();

msg.From =新邮件地址(YourMail@gmail.com);
msg.To.Add(ReciverMail);
msg.Subject =Hello world!+ DateTime.Now.ToString();
msg.Body =你...... :);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host =smtp.gmail.com;
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(YourMail@gmail.com,YourPassword);
client.Timeout = 20000;
尝试
{
client.Send(msg);
返回邮件已成功发送!;
}
catch(Exception ex)
{
returnFail Has error+ ex.Message;
}
finally
{
msg.Dispose();




$ b

如果上述代码不起作用,请尝试像下面的代码一样改变它:

  SmtpClient client = new SmtpClient(); 
client.Host =smtp.gmail.com;
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(YourMail@gmail.com,YourPassword);


I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:

Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

What can I do to fix this issue?

namespace EmailService
{
    public class Service1 : IService1
    {    
        public string SendEmail(string inputEmail, string subject, string body)
        {
            string returnString = "";
            try
            {
                MailMessage email = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";

                // set up the Gmail server
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;

                // draft the email
                MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
                email.From = fromAddress;
                email.To.Add(inputEmail);
                email.Subject = body;
                email.Body = body;

                smtp.Send(email);

                returnString = "Success! Please check your e-mail.";
            }
            catch(Exception ex)
            {
                returnString = "Error: " + ex.ToString();
            }
            return returnString;
        }
    }
}

解决方案

Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.

Also please go to this link and click on Continue Allow access to your Google account

also I edit it little bit :

public string sendit(string ReciverMail)
{
    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("YourMail@gmail.com");
    msg.To.Add(ReciverMail);
    msg.Subject = "Hello world! " + DateTime.Now.ToString();
    msg.Body = "hi to you ... :)";
    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = true;
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
    client.Timeout = 20000;
    try
    {
       client.Send(msg);
        return "Mail has been successfully sent!";
    }
    catch (Exception ex)
    {
        return "Fail Has error" + ex.Message;
    }
    finally
    {
       msg.Dispose();
    }
}

If the above code don't work , try to change it like the following code :

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");

这篇关于如何通过Gmail使用C#发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆