PDA

View Full Version : سوال: The SMTP server requires a secure connection or the client was not authenticated



forodo
جمعه 09 آبان 1393, 19:22 عصر
سلام
من می خوام از طریق لوکال و توسط gmail ایمیل ارسال کنم ولی همش این ارور رو میده.
سرچ کردم و حتی برنامه مثال رو دانلود کردم ولی توی اون برنامه هم برای من همین ارور رو میده.
این کدها رو از این سایت گرفتم. (http://www.webblogsforyou.com/how-to-send-test-mail-using-gmail-smtp-settings-in-asp-net-c-vb-net/)

SMTP Exception: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

try
{
string Subject = "This is test mail using smtp settings",
Body = txtMessage.Text.Trim(),
ToEmail = txtToEmail.Text.Trim();

string SMTPUser = "ali.moshkian.p@gmail.com", SMTPPassword = "*****";

//Now instantiate a new instance of MailMessage
MailMessage mail = new MailMessage();

//set the sender address of the mail message
mail.From = new System.Net.Mail.MailAddress(SMTPUser, "Webblogsforyou");

//set the recepient addresses of the mail message
mail.To.Add(ToEmail);

//set the subject of the mail message
mail.Subject = Subject;

//set the body of the mail message
mail.Body = Body;

//leave as it is even if you are not sending HTML message
mail.IsBodyHtml = true;

//set the priority of the mail message to normal
mail.Priority = System.Net.Mail.MailPriority.Normal;

//instantiate a new instance of SmtpClient
SmtpClient smtp = new SmtpClient();

//if you are using your smtp server, then change your host like "smtp.yourdomain.com"
smtp.Host = "smtp.gmail.com";

//chnage your port for your host
smtp.Port = 25; //or you can also use port# 587

//provide smtp credentials to authenticate to your account
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);

//if you are using secure authentication using SSL/TLS then "true" else "false"
smtp.EnableSsl = true;

smtp.Send(mail);

lblMsg.Text = "Success: Mail sent successfully!";
lblMsg.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
//catched smtp exception
lblMsg.Text = "SMTP Exception: " + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}
catch (Exception ex)
{
lblMsg.Text = "Error: " + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}

fakhravari
شنبه 10 آبان 1393, 12:17 عصر
using System.Net.Mail;using System.Net;
using System;
public class SendMail
{
public string sendmail_fromyahoo()
{
try
{
string To = "fakhravary@yahoo.com";
string From = "fakhravary@yahoo.com";
string Subject = "This is a test";
string Body = "It works!";
string FromName = "your Name";
String ToName = "your Friend`s Name";
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.mail.yahoo.com",
Port = 25,
EnableSsl = false,// yahoo does not support the secure connection but gmail does!
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("fakhravary@yahoo.com", "mofh3j1")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = false,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
return "true";
}
}
catch (Exception ex)
{
return String.Format("{0}", ex);
}
}
}

forodo
شنبه 10 آبان 1393, 13:54 عصر
اول که EnableSsl مقدارش false بود یه اروره بلند بالایی داد ولی وقتی true کردم درست شد.
مشکلی که نداره؟