PDA

View Full Version : خطا در ارسال ایمیل



arash_flag
شنبه 20 تیر 1394, 14:05 عصر
سلام دوستان من این کد ها رو برای ارسال ایمیل جمع آوری کردم اما خطا میده مشکل کجاست؟
SmtpClient smtp = new SmtpClient();


MailMessage email = new MailMessage();
email.From = new MailAddress(txt_email.Text, txt_name.Text);
email.To.Add("my email");
email.Subject = txt_subject.Text;
email.SubjectEncoding = Encoding.UTF8;
email.Body = txt_text.Text;
email.BodyEncoding = Encoding.UTF8;




smtp.Host = "mail.google.com";
smtp.UseDefaultCredentials = false;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "MY email";
nc.Password = "password";

smtp.Credentials = nc;



smtp.Send(email);
lblmsg.Text = "پیام با موفقیت ارسال شد";


اگر کد بهتری دارین ک برای جیمیل و یاهو جواب میده لطفا بگذارید

komeil64
شنبه 20 تیر 1394, 14:54 عصر
برای جیمیل باید پورت رو هم بدید

daffy_duck376
شنبه 20 تیر 1394, 18:51 عصر
البته ssl رو هم باید true کنید

arash_flag
یک شنبه 21 تیر 1394, 11:44 صبح
دوستان کسی کد بهتری نداره ک کار بده؟
لطفا FROM , TO ,.... را توضیح بدین

Motevalizadeh
یک شنبه 21 تیر 1394, 12:31 عصر
https://www.emailarchitect.net/easendmail/kb/csharp.aspx
http://stackoverflow.com/questions/449887/sending-e-mail-using-c-sharp

arash_flag
یک شنبه 21 تیر 1394, 14:10 عصر
سلام دوستان برای ارسال ایمیل از یاهو به .... مشکلی ندارم
اما برای ارسال جی میل به .... این خطا رو میده


An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code

Additional information: 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


اینم کد های نوشته شده

// string smtpAddress = "smtp.mail.yahoo.com";
string smtpAddress = "smtp.gmail.com";

int portNumber = 587;
bool enableSSL = true;


string emailFrom = "MY_EMAIL@gmail.com";
string password = "MY_PASS";
string emailTo = "youmail@yahoo.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.

// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
// mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

SmtpClient smtp = new SmtpClient(smtpAddress, portNumber);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;


smtp.Send(mail);
lblmsg.Text= "ok";