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

时间:2016-09-01 11:58:45

标签: c# asp.net email gmail

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

(SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。了解详情)

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
    {
        mm.Subject = "Account Activation";
        string body = "Hello " + txtUsername.Text.Trim() + ",";
        body += "<br /><br />Please click the following link to activate your account";
        body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>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("sender@gmail.com", "<password>");
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }

5 个答案:

答案 0 :(得分:0)

使用gmail将EnableSSL设置为true并将Port设置为587是正确的,因为SmtpClient类不适用于端口465.请参阅本文http://www.codeproject.com/KB/IP/GmailSmtp.aspx?q=SmtpClient+ssl+yahoo 我认为问题是smtp.UseDefaultCredentials = true。 尝试删除此行。

您好。

答案 1 :(得分:0)

你应该收到来自谷歌的电子邮件,说明他们已经阻止访问安全性较低的应用。

您需要设置UseDefaultCredentials = false,并且需要允许访问来自https://www.google.com/settings/security/lesssecureapps?rfn=27&rfnc=1&asae=2&anexp=lbe3-R2_C的安全性较低的应用

我已经这样做了,我的应用程序可以使用Gmail发送电子邮件。

答案 2 :(得分:0)

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

如果您收到错误,请执行以下操作: -

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

然后执行以下步骤: - 点击下面的链接。 - https://www.google.com/settings/security/lesssecureapps

单击单选按钮启用。

它会起作用。

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

答案 3 :(得分:0)

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

I combine both styles you have provided:

<Charting:Chart 
x:Name="LineChart" Grid.Column="1" FontSize="16" VerticalAlignment="Top" 
HorizontalAlignment="Left" Margin="94,27,0,0" 
FontWeight="SemiBold"  Width="651" Height="506">

<Charting:LineSeries 
Title="Station1" Margin="0" FontSize="16" Foreground="Blue" FontWeight="SemiBold" IndependentValuePath="Q_interval" DependentValuePath="Q_size" IsSelectionEnabled="True">

<Charting:LineSeries.Style>

(1)
  <Style TargetType="Charting:LineSeries">

      <Setter Property="IsTabStop" Value="False" />
      <Setter Property="PolylineStyle">
          <Setter.Value>
             <Style TargetType="Polyline">
             <Setter Property="StrokeThickness" Value="3" />
             <Setter Property="StrokeMiterLimit" Value="1" />
             </Style>
          </Setter.Value>
      </Setter>

      <Setter Property="Template">
         <Setter.Value>
         <ControlTemplate TargetType="Charting:LineSeries">
            <Canvas x:Name="PlotArea">
               <Polyline
                   Style="{TemplateBinding PolylineStyle}"
                            Stroke="Blue"
                            Points="{TemplateBinding Points}" />
             </Canvas>
         </ControlTemplate>
        </Setter.Value>
      </Setter>
   </Style>                    
</Charting:LineSeries.Style>

(2)

<Charting:LineSeries.DataPointStyle>                        
<Style TargetType="Charting:LineDataPoint">                            
    <Setter Property="Width" Value="20" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Background" Value="Blue"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
 </Style>
</Charting:LineSeries.DataPointStyle>

</Charting:LineSeries>              

</Charting:Chart>

但是,您通过将NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>"); 属性设置为true来覆盖这些详细信息。

UseDefaultCredentials

您需要删除该行代码,因为它告诉您的应用程序尝试使用您的Windows凭据登录gmail。或者,如果这不起作用,您可以在提供新的NetworkCredentials之前将其设置为false:

smtp.UseDefaultCredentials = true;

之后,如果您不断收到错误消息,则需要进入gmail帐户安全设置并允许安全性较低的应用程序。

答案 4 :(得分:0)

您好我的朋友我只是想出了这个错误,它只是您没有更改sender@gmail.com和密码的任何内容,只需输入您创建的Gmail和密码,就像您一样在下面的修改中看到,代码实际上没有任何问题,只是把你创建的电子邮件放到那个gmail的密码上,实际上上面的所有答案都是正确的但是你必须删除那个&#34;&lt;&gt; &#34;到你的密码,你没有将sender@gmail.com改为各自的gmail ......

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


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