5 Replies - 1551 Views - Last Post: 31 May 2009 - 12:40 AM Rate Topic: -----

#1 masri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 13-April 09

email

Posted 30 May 2009 - 12:34 AM

How to send email in c#? Sorry for not showing any code, but I have no idea of how to do it.
Is This A Good Question/Topic? 0
  • +

Replies To: email

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: email

Posted 30 May 2009 - 12:54 AM

See Here
System.Net.Mail() Namespace
or
Email in C#
Was This Post Helpful? 0
  • +
  • -

#3 masri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 13-April 09

Re: email

Posted 30 May 2009 - 03:54 AM

After doing some research, I came up with this code:-

  
	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

Was This Post Helpful? 0
  • +
  • -

#4 masri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 13-April 09

Re: email

Posted 30 May 2009 - 11:31 AM

I found what I was doing wrong now no need for a reply.
Was This Post Helpful? 0
  • +
  • -

#5 versatile36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 02-October 08

Re: email

Posted 30 May 2009 - 06:55 PM

@masri

can you tell me the solution to one such problem??
Was This Post Helpful? 0
  • +
  • -

#6 masri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 13-April 09

Re: email

Posted 31 May 2009 - 12:40 AM

Quote

@masri
can you tell me the solution to one such problem??
yeah sure look here,

 //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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1