How to send email in c#? Sorry for not showing any code, but I have no idea of how to do it.
5 Replies - 1377 Views - Last Post: 31 May 2009 - 12:40 AM
Replies To: email
#3
Re: email
Posted 30 May 2009 - 03:54 AM
After doing some research, I came up with this code:-
But it didn't work, this is what the exception said:-
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 28sm4214831eye.16"
So what am I doing wronge? I think it may be about the password but where should I write it?
static void Main()
{
MailMessage mail = new MailMessage();
mail.To.Add("deathstriker77@hotmail.com");
mail.From = new MailAddress("kyacout@gmail.com");
mail.Subject = "Test Email";
mail.Body = "This is a TEST EMAIL";
SmtpClient smtp = new SmtpClient(@"smtp.gmail.com");
smtp.Send(mail);
}
But it didn't work, this is what the exception said:-
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 28sm4214831eye.16"
So what am I doing wronge? I think it may be about the password but where should I write it?
This post has been edited by masri: 30 May 2009 - 03:56 AM
#4
Re: email
Posted 30 May 2009 - 11:31 AM
I found what I was doing wrong now no need for a reply.
#5
Re: email
Posted 30 May 2009 - 06:55 PM
@masri
can you tell me the solution to one such problem??
can you tell me the solution to one such problem??
#6
Re: email
Posted 31 May 2009 - 12:40 AM
Quote
@masri
can you tell me the solution to one such problem??
can you tell me the solution to one such problem??
//Create Mail Message Object with content that you want to send with mail.
MailMessage MyMailMessage = new MailMessage(sender,receipient, subject, mainBody);
//Proper Authentication Details need to be passed when sending email from gmail
NetworkCredential mailAuthentication = new NetworkCredential(email, password);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
//Enable SSL
mailClient.EnableSsl = true;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);
MessageBox.Show("Your Email has been successfuly sent to " + toTxt.Text, "Email succesfuly sent",
MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
This is the code I use in my program to send emails and it works. I am going to add support for attachments later.
Here is how it works:-
First thing you make a MailMessage object, you can have the information sent to the MailMessage object by users entering them to forms or something
//Create Mail Message Object with content that you want to send with mail. MailMessage MyMailMessage = new MailMessage(sender,receipient, subject, mainBody);
Then you authenticate. This is the part I couldn't do before.
//Proper Authentication Details need to be passed when sending an email NetworkCredential mailAuthentication = new NetworkCredential(email, password);
Then, you make a SmtpClient object which is required to send an email. I used "smtp.gmail.com" so the senders email must be from gmail, you can check others too like yahoo's is "smtp.mail.yahoo.com."
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
Finally, you enable ssl, set the mail authentication you got before, and send the email.
//Enable SSL mailClient.EnableSsl = true; mailClient.Credentials = mailAuthentication; mailClient.Send(MyMailMessage);
Happy emailing
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|