Saturday, June 20, 2009

How to send email from a website

This is a nice piece of task I get much pleasure. I think this is a very interesting task for a CSE student who is not yet recognized with the technique of sending an email. Let me state the process step by step. There are two sections:
a.Administrative part
b. Developer's part.

Administrative part:
For Administrative part you have to just set the configuration (shown above) in your web configuration (web.config) file. If you do it to the hosting company server's file then no need to think about other thing. But if you want to make local server (your computer) as the mail server then you have to do some other configuration:
1. Open Control Panel
2. Open Administrative tools
3. Select Internet Information Service (IIS)
4.Select Default SMTP>Properties
5. Click Access tab>Relay (Relay Restrictions)>Grant Access for 127.0.0.1

when you are done with this configuration you are ready to write the funtion for sending email. The required configuration is:

<!--Here Mailserver configuration-->
<system.net>
<mailSettings>
<smtp from="test@nochallenge.net">
<network host="localhost"
userName="test@company.net"
password="****"
port="25"
defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>


Developer's Part:
As a developer you just have to write a function for sending email.
Here is the code


protected void SendingEMail()
{

string from="test@yahoo.com";
string to="test@gmail.com";
MailMessage msg=new MailMessage(from,to);
msg.Subject="This is test mail";
msg.Body="Please respond if you get this mail";
SmtpClient client=new SmtpClient("localhost");
client.Send(msg);
Response.write("Mail Sent successfully!");

}


thanks
Masud(26.06.09)


No comments:

Post a Comment