我收到错误,因为SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.1客户端未经过身份验证
问题描述:
Hi Team,
这是我的代码。虽然iam执行此操作时出现以下错误。请任何人帮助解决这个问题。
Hi Team,
Here is my code. While iam executing this got the below error. Please anyone help on this issue.
Error"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated "
代码:
Code:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
SmtpClient smtpClient = new SmtpClient();
MailAddress fromAddress = new MailAddress(From, displayName);
message.From = fromAddress;
message.Subject = "Test Mail in SMTP";
message.To.Add(To);
smtpClient.Host = "smtp";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("Mail", "pwd");
//smtpClient.EnableSsl = true;
System.Net.NetworkCredential auth = new System.Net.NetworkCredential("Mail", "pwd");
smtpClient.Credentials = auth;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
答
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
错误表明您的用户名或密码管不正确,或者启用ssl
The error says either your username or passwoord is incorrect,or enable the ssl
使用此代码...
Use this code...
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Globalization;
string to = "rajeeshmenoth@wordpress.com";//To address
string from = "rajeeshmenoth@wordpress.com";//From address
MailMessage message = new MailMessage(from, to);
if (fuAttachment.HasFile)//Attaching document
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
message .Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
message.Subject = txt_subject.text;
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
或
or
smtpClient.EnableSsl = true;//enable the ssl