Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用asp.net和C#向Gmail帐户发送电子邮件以进行访问_C#_Asp.net_Email_Gmail - Fatal编程技术网

使用asp.net和C#向Gmail帐户发送电子邮件以进行访问

使用asp.net和C#向Gmail帐户发送电子邮件以进行访问,c#,asp.net,email,gmail,C#,Asp.net,Email,Gmail,我是C#的新手,因此开始学习 我尝试发送一封谷歌邮件,它抛出了这个错误 请帮我完成我的工作 (SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。了解更多信息,请访问) 使用(MailMessage mm=新的MailMessage(“sender@gmail.com“,txtEmail.Text)) { mm.Subject=“账户激活”; string body=“Hello”+txtUsername.Text.Trim()+”,“; body+=“请

我是C#的新手,因此开始学习 我尝试发送一封谷歌邮件,它抛出了这个错误 请帮我完成我的工作

(SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。了解更多信息,请访问)

使用(MailMessage mm=新的MailMessage(“sender@gmail.com“,txtEmail.Text))
{
mm.Subject=“账户激活”;
string body=“Hello”+txtUsername.Text.Trim()+”,“;
body+=“

请单击以下链接激活您的帐户”; 正文+=“
”; 正文+=“

谢谢”; mm.主体=主体; mm.IsBodyHtml=true; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.EnableSsl=true; NetworkCredential NetworkCred=新的网络凭据(“sender@gmail.com", ""); smtp.UseDefaultCredentials=true; smtp.Credentials=NetworkCred; smtp.Port=587; smtp.Send(mm); }
使用gmail将EnableSSL设置为true并将端口设置为587是正确的,因为SmtpClient类不适用于端口465。参见本文 我认为问题在于smtp.UseDefaultCredentials=true。 尝试删除此行


您好。

您应该收到一封来自谷歌的电子邮件,说明他们已经阻止了对不太安全的应用程序的访问

您需要将UseDefaultCredentials设置为false,并需要允许从中访问不太安全的应用程序

我已经这样做了,我的应用程序能够使用gmail发送电子邮件。

要使用gmail服务器发送电子邮件,我们需要设置以下内容。在C#中使用这些名称空间。
To Send email using Gmail server we need to set following thing.Use these namespaces in C#.


     1. using System.IO;
     2. using System.Net;
     3. using System.Net.Mail;

MailMessage Class Properties

Following are the required properties of the MailMessage class.

 - From – Sender’s email address 
 - To – Recipient(s) Email Address 
 - CC –Carbon Copies (if any) 
 - BCC – Blind Carbon Copies (if any) 
 - subject –Subject of the Email  
 - Body – Body of the Email 
 - IsBodyHtml – Specify whether body contains text or HTML tag.
 - Attachments – Attachments(if any)
 - ReplyTo – ReplyTo Email address.

 SMTP Class Properties

  Following are the properties of the SMTP class.
  - Host – SMTP Server URL (Gmail: smtp.gmail.com)
  - EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
  - UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
  - Credentials – Input valid username and password
  - Port – Assign port number for (Gmail: 587)

Finally here is your Code.  

       using (MailMessage mm = new MailMessage(from, to))
        {
            mm.Subject = "Account Activation";
            string body = "Hello " + your username.Trim() + ",";
            body += "<br /><br />Please click the following link to activate your account";
            body += "<br /><a href = '" + new Uri("http://www.google.com", true).AbsoluteUri + "<a>Click here to activate your account.</a>";
            body += "<br /><br />Thanks";
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(username, password);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
1.使用System.IO; 2.Net系统; 3.使用System.Net.Mail; MailMessage类属性 以下是MailMessage类的必需属性。 -发件人–发件人的电子邮件地址 -收件人–收件人的电子邮件地址 -CC–复印件(如有) -密件抄送-盲拷贝(如有) -主题–电子邮件的主题 -正文–电子邮件的正文 -IsBodyHtml–指定正文是包含文本还是HTML标记。 -附件–附件(如有) -ReplyTo–ReplyTo电子邮件地址。 SMTP类属性 以下是SMTP类的属性。 -主机–SMTP服务器URL(Gmail:SMTP.Gmail.com) -EnableSsl–指定主机是否接受SSL连接(Gmail:True) -UseDefaultCredentials–设置为True以允许基于用于发送电子邮件的帐户的凭据进行身份验证 -凭据–输入有效的用户名和密码 -端口–为分配端口号(Gmail:587) 最后,这里是您的代码。 使用(MailMessage mm=新的MailMessage(from,to)) { mm.Subject=“账户激活”; string body=“Hello”+您的用户名.Trim()+”,”; body+=“

请单击以下链接激活您的帐户”; 正文+=“
”; 正文+=“

谢谢”; mm.主体=主体; mm.IsBodyHtml=true; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.EnableSsl=true; NetworkCredential NetworkCred=新的NetworkCredential(用户名、密码); smtp.UseDefaultCredentials=true; smtp.Credentials=NetworkCred; smtp.Port=587; smtp.Send(mm); }
如果出现如下错误:-

-SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:需要5.5.1身份验证

然后执行以下步骤:- 点击下面的链接。 -

单击单选按钮打开

它会起作用的


代码已经过测试。谢谢..

您正在为您的gmail帐户定义登录凭据:

NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
您需要删除这行代码,因为它告诉您的应用程序尝试使用windows凭据登录gmail。或者,如果这不起作用,您可以在提供新的NetworkCredentials之前将其设置为false:

smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");

smtp.UseDefaultCredentials=false;
NetworkCredential NetworkCred=新的网络凭据(“sender@gmail.com", "");

在那之后,如果你一直收到错误,你需要进入你的gmail帐户安全设置,并允许不太安全的应用程序。

你好,我的朋友,我刚刚发现了这个错误,只是你没有更改任何内容sender@gmail.com密码,只要把你创建的gmail和密码,正如你在我下面的修改中看到的,代码实际上没有问题,只是把你创建的电子邮件放在gmail的密码上,实际上上面所有的答案都是正确的,但是你必须将“”删除到你的密码中,并且你没有更改密码sender@gmail.com到你各自的gmail

      NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");


      NetworkCredential NetworkCred = new NetworkCredential("yourgmail.com", "yourpassword");
NetworkCredential NetworkCred=新的NetworkCredential(“sender@gmail.com", "");
NetworkCredential NetworkCred=新的网络凭据(“yourgmail.com”、“yourspassword”);

AFAIK,将
enablesssl
属性设置为
true
,并不一定意味着“通过SSL发送”-我认为这只是意味着请求来自HTTPS源。尝试删除此项,然后尝试使用
端口
465
,而不是:)另外,不要认为您需要
UseDefaultCredentials=true
-可能需要
false
它,要在设置凭据之前将
凭据设置为
null
,请执行以下操作:smtp.UseDefaultCredentials=false;smtp.EnableSsl=true;我记得有一次碰到一个类似的问题。Gmail本身阻止邮件发送,声称邮件来自不受信任的应用程序。我必须从Gmail设置中启用它。你有没有试着阅读“阅读更多…”下的内容?嗨@GeoffJames;)希望m
smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
      NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");


      NetworkCredential NetworkCred = new NetworkCredential("yourgmail.com", "yourpassword");