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


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

create own application to send mail

Posted By:Anand       Posted Date: June 15, 2012    Points: 25    Category: Email    URL: http://www.dotnetspark.com  

Hi here i am explaining how to send a mail from our own application. very simple 3 steps. 1.MailMessage 2.NetworkCredential 3.SmtpClient
 

Email Application


       
using System.Net;
using System.Net.Mail;
private void btnSend_Click(object sender, EventArgs e) { string fromId=txtFrom.Text; string frompwd=txtPwd.Text; string to = txtTo.Text; string sub = txtSub.Text; string body = txtBody.Text; try { MailMessage msg = new MailMessage(fromId,to); msg.Subject = sub; msg.Body = body; NetworkCredential NetCrd = new NetworkCredential(); NetCrd.UserName = fromId; NetCrd.Password = fromPwd; SmtpClient mailClient = new SmtpClient(); mailClient.Host = "smtp.gmail.com"; mailClient.Port = 587; mailClient.UseDefaultCredentials = false; mailClient.Credentials = NetCrd; mailClient.EnableSsl = true; mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; mailClient.Send(msg); MessageBox.Show("Mail has been sent successfully","Info",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message); } }


3 Steps to send mail
First Step

                MailMessage msg = new MailMessage(fromId,to);
                    msg.Subject = sub;
                    msg.Body = body;

Second Step

                NetworkCredential NetCrd = new NetworkCredential();
                    NetCrd.UserName = fromId;
                    NetCrd.Password = fromPwd;

Third Step
                SmtpClient mailClient = new SmtpClient();
                    mailClient.Host = "smtp.gmail.com";
                    mailClient.Port = 587;
                    mailClient.UseDefaultCredentials = false;
                    mailClient.Credentials = NetCrd;
                    mailClient.EnableSsl = true;
                    mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    mailClient.Send(msg);


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More code samples in C#, ASP.Net, Vb.Net and more Here

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