Friday, January 19, 2007

How to send mail with c#

How to send mail with c#
If you want to sending the mail with c# , you must import "System.Net.Mail" namespace .And then we need to use
the SmtpClient class.This class have two properties and two method that we need to use.

Properties -
1.) Host
2.) Port

Method
1.) Send()
2.) SendAsync()

In properties ,it is too easy to know what it is mean.So I only discuss the method.The different between Send() and
SendAsync() is Send() method other relative operation will be wait untail sending mail operation is finish.If you
use the SendAsync() method ,you not need to wait sending mail operation is finish.So thinking yourself and try to use it.

Now I will show you a sample ,

private void SendMyMail() {

string msg_header="How to send mail!"; //mail subject (or) mail header
string msg_body="Hi!Friend,How are you!May I know how to send mail"; //mail body

SmtpClient client = new SmtpClient();
client.Host = "MyMailServer.com.net"; // this is mail server name
client.Port = 25;

//Now send mail here
client.Send("youname@yourserver","yourfriend@yourfrdserver",msg_header,msg_body);

}

In Send() method ,first parameter is from address ,second parameter is to address and third parameter is mail header,
and final parameter is mail body.
Now ,you can start to send mail with c# !

thank for your interest.
regard by
kokothet@kavan

1 comment:

Hnin said...

Is it only for local mail? when i tested according to ur post, i can send mail internally but i can't send to external mail like... gmail or others. How should i do to send to gmail or other web mail?
Thanks for sharing knowledge... :)

Google Ad