Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / Windows Forms
Article

Windows Mail

Rate me:
Please Sign up or sign in to vote.
1.44/5 (17 votes)
17 Sep 20071 min read 29.4K   12   1
Code for sending emails through winforms by using C#

Navneet Sharma's Article for mailing from windows application


Screenshot - coolcode2.jpg

Introduction

This article can be used to send emails from windows forms while developing from C# applications in .Net 3.0 environment and I have used the assemblies and method to be added to the code for the smooth functionality.This code has been inherited and implemented by me after a survey on the web and contains the items useful for email over web as well as windows applications.

Background

This article can be used to send emails from windows forms while developing from C# applications in .Net 3.0 environment

Using the code

Copy this code to send emails from windows forms while developing from C# application in .Net 3.0 environment and I have used the assemblies and method to be added to the code for the smooth functionality. Also add the server name in the "SMTPServer" which is used to send emails which can be your localhost also which can be denoted by "." and the method named sendMail() can be called anywhere in your code(where you want) for e.g. Button1_Click event.

Copy the below code and paste it into your application.

C#
//
//-------------------------------Assemblies------------------------------------------------
using System.Net;
using System.Net.Mail;

//-------------------------------Method------------------------------------------------

private void sendMail()

{

string strMailTo, strMailFrom, strCC,strBCC, strSubject, strBody;
try

{

strMailTo = <a href="mailto:np.mca1982@gmail.com;strMailFrom">np.mca1982@gmail.com;</a>

strMailFrom = <a href="mailto:yourmailid@abc.com">yourmailid@abc.com</a>;
strSubject = <a href="mailto:abc@abc.com;strReportName">"Demo Mail"</a>;

strBody = "Mail Body to be sent goes here.";
strCC = <a href="mailto:np.mca1982@gmail.com;strBCC">np.mca1982@gmail.com</a>;

strBCC = <a href="mailto:np.mca1982@gmail.com;MailMessage">np.mca1982@gmail.com</a>;

MailMessage mm = new MailMessage();

mm.To.Add(strMailTo);

mm.From = new MailAddress(strMailFrom);

mm.CC.Add(strCC);

mm.Bcc.Add(strBCC);

mm.Subject = strSubject;

mm.Body = strBody;

mm.Priority = MailPriority.Normal;

mm.BodyEncoding = Encoding.UTF8;

mm.IsBodyHtml = false;

SmtpClient smtp=new SmtpClient("SMTPServer");   //Add the server name/ip or localhost here

smtp.Send(mm);
}
catch (Exception ex)

{

// **************** Exception Caught *******************

}
}

Language Used : C#

Platforms : Win XP Professional with SP 2, .Net 3.0

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

This code has been implemented here for the first time in this post and if you want some modifications in it then mail me or suggest me the required improvements and I will do the required changes.

Please vote this article if this was useful to you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
.Net Developer as well as Trainer

Comments and Discussions

 
GeneralReally it works fine Pin
sexy_rahul_sharma10-Dec-07 17:28
sexy_rahul_sharma10-Dec-07 17:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.