C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Code to send email


Posted Date: 27 Aug 2009    Resource Type: Code Snippets    Category: Email
Author: Vivek SharmaMember Level: Gold    
Rating: 1 out of 5Points: 5



Hi, below is the code to send the email.

First of all make changes in web config file in "app settings"
i.e.


<add key="SMTPServer" value="mail.test.com"/>
<add key="MailUserName" value="admin@test.com"/>
<add key="MailPassword" value="password"/>

Then use namespace on source code file.

i.e.
using System.Net.Mail;

Now write down the below code on button click (on which you want to send email.)with required perameter. i.e.


SmtpClient objSMTP = new SmtpClient(ConfigurationManager.AppSettings ["SMTPServer"].ToString());

objSMTP.Credentials = new System.Net.NetworkCredential(
ConfigurationManager.AppSettings["MailUserName"].ToString(),

ConfigurationManager.AppSettings["MailPassword"].ToString());

MailMessage objMailMessage = new MailMessage();

objMailMessage.From = new MailAddress(ConfigurationManager.AppSettings["MailUserName"].ToString());

objMailMessage.Subject = "Test mail";

objMailMessage.To.Add(new MailAddress(EmailAddress));

objMailMessage.Body = "Hi, this is a text mail.

objMailMessage.IsBodyHtml = true;

objSMTP.Send(objMailMessage);

lblMsg.Visible = true;

lblMsg.Text = "Mail has been sent.";


Regards
Vivek Sharma



Responses

Author: Mohan    03 Sep 2009Member Level: Diamond   Points : 1
HI,

Is Your code is working from any mail id to anyone?
or Anyone id shoule be a abc@test.com


Author: Vivek Sharma    03 Sep 2009Member Level: Gold   Points : 1
Sorry sir, but I am not getting you exactly??
Could you explain please??

Regards
Vivek Sharma


Author: Mohan    03 Sep 2009Member Level: Diamond   Points : 1
Means you are using mail.test.com server for sending mail

Is your code allow

from address=abc@gmail.com
to address=def@gmail.com


or it allow only

from address=abc@test.com
to address=def@test.com




Author: Punit    25 Sep 2009Member Level: Silver   Points : 0
Thank you vivek, this code was helpful for me.

Regards,
Punit


Author: Vivek Sharma    25 Sep 2009Member Level: Gold   Points : 0
Hi Punit, Thanks you very much.

Regards
Vivek Sharma


Author: Vivek Sharma    25 Sep 2009Member Level: Gold   Points : 1
Hi Mohan, You can pass any (required parameter in it) I mean in
Tomail and From mail .

Regards
vivek Sharma


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Simple code to send email  .  Send email  .  How to send email in asp.net  .  How to send email in .net  .  Email code  .  Code to send email in asp.net  .  Code to send email in .net  .  Code to send email  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sending Email:-
Previous Resource: Mail sending in asp.net
Return to Discussion Resource Index
Post New Resource
Category: Email


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use