.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Send Email To any Email ID

Posted By: Vikramraj     Posted Date: June 25, 2010    Points:2   Category :C#
I want to send Email to any EmailId from gmail account. so how it is possible in visual studio 2010

Author: Sunil Yadav       [Moderator]      
Posted Date: June 26, 2010     Points: 5   


Yes it is possible in VS 2010.Please refer below sample code..

Add following entries in web.config.

<add key="MailServer" value="smtp.gmail.com" />
<add key="MailServerPort" value="587" />
<add key="UserName" value="yorgmailAccount@gmail.com" />
<add key="Password" value="password" />

C# Code.
NameValueCollection settings;
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
settings = System.Configuration.ConfigurationManager.AppSettings;
string sUserName= settings["UserName"];
string SPassword = settings["Password"];
string SMTP = settings["MailServer"];
int PORT = Convert.ToInt32(settings["MailServerPort"]);

MailMessage msg = new MailMessage();
msg.From = new MailAddress(sUserName);
msg.To.Add(new MailAddress("sunilyadav165@gmail.com"));// receiver email address
msg.Subject = "Mail message - " + DateTime.Now.ToShortDateString();
msg.Body = body;
msg.IsBodyHtml = false;

SmtpClient smtp = new SmtpClient();

smtp.Host = SMTP;

smtp.Port = PORT;

smtp.Credentials = new System.Net.NetworkCredential(sUserName, SPassword);
smtp.EnableSsl = true;

hope this helps.

Author: Kirtan             
Posted Date: June 26, 2010     Points: 5   

you can simply send email using

MailMessage class

set the properties of MailMessage class object and send it using smtp client object by passing object of MailMessage .

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend