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 »

Send Email using Gmail


Posted Date: 20 Jun 2008    Resource Type: Code Snippets    Category: Email
Author: Danasegarane.AMember Level: Diamond    
Rating: 1 out of 5Points: 4



This code sample lets you send email using Gmail account. The steps involved are explained.


public static void SendMail(string Host, int Port, string strUserName, string strPassword, string strFromName, string strFromEmail,
string strToName, string strToEmail, string strHeader, string strMessage, bool bAuthenticated)
{
if (strToName.Length == 0)
strToName = strToEmail;
if (strFromName.Length == 0)
strFromName = strFromEmail;
System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
Mail.
//System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = Host;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = Port.ToString();
if (bAuthenticated)
{
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = strUserName;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = strPassword;
}
Mail.To = strToEmail;
Mail.From = strFromEmail;
Mail.Subject = strHeader;
Mail.Body = strMessage;
Mail.BodyFormat = System.Web.Mail.MailFormat.Html;

System.Web.Mail.SmtpMail.SmtpServer = Host;
System.Web.Mail.SmtpMail.Send(Mail);
}



And call the Function as

SendMail("smtp.gmail.com",
465,
"danasegarane@gmail.com",
"YourGmail Password",
"Dana",
"danasegarane@gmail.com",
"Danasegarane",
"danasegarane@gmail.com",
"Hi Dana ",
"Hello How are u !",
true);






Responses

Author: Pradeep Iyer    03 Nov 2008Member Level: Diamond   Points : 0
Hi,

Thanks for that !!

Regards
Pradeep


Author: D.Jeya kumar(JK)    15 May 2009Member Level: Diamond   Points : 1
Hi,


This SMTP server is not working... Please test it once before posting...

Regards
JK


Author: Danasegarane.A    15 May 2009Member Level: Diamond   Points : 1
I tested it previously... It was working..

Let me check it once again


Dana


Author: Muralidharan Rangaraju    17 May 2009Member Level: Gold   Points : 1
Hi Danasegarane,

For gmail SMTP server we can also use port number 587 instead of 465. In some networks 465 works and in some network 587 will work. For JK this may be a problem.

Regards,
Murali


Author: Sasikumar    19 May 2009Member Level: Gold   Points : 1
Hai Danasegarane.A,

This SMTP server is not working,
Please Check and test before posting the Resource

Regards
Sasikumar


Author: Danasegarane.A    19 May 2009Member Level: Diamond   Points : 1
Hi Sasikumar,
I have never posted resource without properly checking. It was working well for me...

Check the Murli's comment


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Sending E-Mail using .NET  .  Sending E-Mail from C#  .  Send E-Mail from ASP.NET web page  .  Send E-Mail from .net applications  .  

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: How to send E-mail from .NET applications
Previous Resource: Sending E-Mail through SQL Server stored procedures
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