使用asp.net和c#发送电子邮件到Gmail帐户进行激活
本文关键字:Gmail 激活 电子邮件 net asp 使用 | 更新日期: 2023-09-27 18:05:04
我是c#的新手,并开始学习我试着发送谷歌邮件,它抛出这个错误请帮助我完成我的工作
(SMTP服务器需要安全连接或客户端未通过身份验证。)服务器响应为:5.5.1 Authentication Required。点击
了解更多信息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);
}
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 Authentication Required。
则执行以下步骤:-点击下面的链接。——https://www.google.com/settings/security/lesssecureapps
点击单选按钮打开。
It will work.
测试代码。谢谢. .
与gmail是正确的设置EnableSSL为true和设置端口为587,因为SmtpClient类不与端口465工作。请看这篇文章http://www.codeproject.com/KB/IP/GmailSmtp.aspx?q=SmtpClient+ssl+yahoo我认为问题出在smtp上。UseDefaultCredentials = true。尝试删除这一行。
嗨。
你应该收到一封来自谷歌的电子邮件,声明他们已经阻止了一个不太安全的应用程序的访问。
你需要设置UseDefaultCredentials = false,并且需要允许从https://www.google.com/settings/security/lesssecureapps?rfn=27&rfnc=1&asae=2&anexp=lbe3-R2_C访问不太安全的应用
我已经这样做了,我的应用程序能够使用gmail发送电子邮件。
您正在定义gmail帐户的登录凭据:
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
但是,通过将UseDefaultCredentials
属性设置为true,您将覆盖这些详细信息。
smtp.UseDefaultCredentials = true;
你需要删除那行代码,因为它告诉你的应用程序尝试用你的windows凭证登录gmail。或者,如果这不起作用,您可以在提供新的NetworkCredentials之前将其设置为false:
smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
之后,如果你一直得到错误,你需要进入你的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");