Please visit my new Web Site https://coderstechzone.com
E-mail is one of the most common popular methods of communication for both personal and business purposes. It also plays an important role in each and every Web site specially for registration page or sales order page of an e-commerce site & also for online invoicing. The .NET framework makes the task of sending email from a Web page unbelievably easy and I'll show you how to do it in a matter of minutes.
Here i am using gmail smtp to send email using asp.net cause i think most of the reader has a gmail id so that he can just copy the below method & can send email. Just need to change the from & to email address. Better first try with gmail cause its easy to test. Then go for your own mail server.
Send an email using Visual Studio 2005:
At first import the namespace: using System.Net.Mail;
Now copy the below code under Send button:
C#:
Here i am using gmail smtp to send email using asp.net cause i think most of the reader has a gmail id so that he can just copy the below method & can send email. Just need to change the from & to email address. Better first try with gmail cause its easy to test. Then go for your own mail server.
Send an email using Visual Studio 2005:
At first import the namespace: using System.Net.Mail;
Now copy the below code under Send button:
C#:
VB.NET:
At first import the name space System.Net.Mail
Send email with Attachment:
Just copy the below line before smtp.Send(mailMsg).
You can also add more than one attachment at a time.
C#:
VB.NET:
Just copy the below line before smtp.Send(mailMsg).
You can also add more than one attachment at a time.
NOTES:
If you are going to host your web site then ask administrator the below questions:
1. Does mail server configured? If yes then collect smtp server name & replace with smtp.gmail.com
2. Does the mail server require authentication before sending an email? If yes then modify credentials else remove credential line from above code segment.
3. Does the mail server relay is on? If no then make it yes.
4. Does the mail server require SSL validation. If no then remove EnableSSL line.
Output Like:
If mail has an attacment:
Common error message:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for..............
Need to configure the SMTP server to allow relaying for either your IP, "FROM" address, or credentials. Notify hosting server administrator to allow the relay aganist your from email address. If you are using localhost then right click on mycomputer then manage then services & applications then IIS then right click on default smtp virtual server then properties then click on access tab then click on relay button then select only the list below then click on ADD & set the IP 127.0.0.1
To read how to validate entered email address using javascript click on the below link:
http://shawpnendu.blogspot.com/2009/04/cross-browser-javascript-aspnetc-email.html
6 comments:
dude ..
you are great .. i was stuck for the past two days .. your tutorial really helped me understand and successfully send a mail .. thnx a ton ..
also while sending a mail using gmail i had to also use the code
"smtp.Port = 587;"
thnx again ..
Do you have the same code for those sending messages using Windows 2003 SMTP virtual server in the same machine where IIS is running?
unlike the genleman above, I'm still stuck in my problem.
thanks!
Hi rodolpho, sorry i didn't get you. Could you please post the error message? Also you can read common error section to configure smtp server localy.
Do you know any way for connecting my application with Hotmail server with SSL. I want to aquire ticket for its CMS for addressbook.
HI ds r4,
Yes there is a open source project in sourceforge named OpenContacts.NET can resolve your problem. For more you can read the below link:
gnillydev blogspot 2007/12 opencontacts-10-release-with post.
I come from sending mail through PHP which is pretty simple but you have to setup your server to work with it. This was even easier because you don't have to chnage server setting to make it work.
What a great post!
I WOULD BE DELIGHTED TO HEAR FROM YOU