.Net Basic

This blogs gives introduction to C#.NET programming for beginners. This blogs assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby!after Refer this blog

23 June 2009

Sending Email By using C#.net

In this coding we need to change "from@gmail.com", "to@gmail.com" and "Password"..

try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.To.Add("to@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("From@gmail.com", "Password");
SmtpServer.EnableSsl = true;
// send mail to With Attachement
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:\\DSC00081.JPG");
mail.Attachments.Add(attachment);
// Send mail to With HTML Page
mail.IsBodyHtml = true;
string htmlBody = "create html page";
mail.Body = htmlBody;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

Labels: , , ,