Simplest way to send email from your C# (ASP.net) application

<1> IMPORT LIBRARIES >> TO FORM(or ASPX page ) & CLASS 

using System.Net.Mail;

<2>INCLUDE CLASS BELOW 

class sendmail
{
public void testmail(string to,string subject,string body)
{
try
{
string To=to;
string sub=subject ;
string Body=body ;
MailMessage mail = new MailMessage();

SmtpClient SmtpServer = new SmtpClient(“smtp.gmail.com”);

mail.From = new MailAddress(“absccd@gmail.com”);

// email adress that i have created on gmail

// or you can use any mail server such as yahoo,aol,msn,and so on

mail.To.Add(To);

mail.Subject = sub;

mail.Body = Body;

SmtpServer.Port = 587;

SmtpServer.Credentials = new System.Net.NetworkCredential(“userNAme”, “Password”);

//enter uname and pwd of email address you want to send email from

SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);

MessageBox.Show(“mail Sent successfully”);

}

catch (Exception ex)
{

MessageBox.Show(ex.ToString());

}

}

}

<3>CREATE OBJECTS FROM THAT(in the form or page) and USE IT

sendmail obj = new sendmail();
obj.testmail(toContentTextBox.Text, subjectContentTextBox.Text, messageContentTextBox.Text);

 

//enjoy your program

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s