Click here to Skip to main content
13,145,668 members (34,159 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 7 Nov 2012

Sending mail using ASP.NET with optional parameters

, 7 Nov 2012
Rate this:
Please Sign up or sign in to vote.
Sending mail using ASP.NET with optional parameters.


In daily development we need to add some mail functionality in our project to send e-mail to customer or other in our web site. There are many Free SMTP server. We will show you how to use google or gmail smtp server using in ASP.NET.


Though we have implemented mail functionality several times but still every day several developer asks for code to sent mail from depending upon their requirements like different option for host or how to sent attachments with mail etc.

Using the code

For sending mail from ASP.NET we generally use System.Net.Mail namespace. And here I have also used optional parameters for sending mail with different options. Like this:

public bool SendEmail(string sender, string receipents, String mailbody, String subject,
            String MailCc = null, String MailBcc = null,
            bool IsBodyHtml = false, String LnkSrc = null, String Attachment = null)

Optional arguments

The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.

Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used.Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported.

First create an object of MailMessage class and add sender, recipients  like this:

MailMessage _email = new MailMessage();
_email.To.Add(new MailAddress(receipents));
_email.From = new MailAddress(sender);

Add below code if you need to sent attachments with mail or link resources.

Attachment attach = new Attachment(Attachment);

Create object for SmtpClient using localhost or set your mail server host name, port, credentials like this:

SmtpClient MailSupportSender = new SmtpClient("localhost");

Or we can use below code to set our mail server with Credentials

NetworkCredential Credentials = new NetworkCredential();
Credentials.Domain = "";
Credentials.UserName = "";
Credentials.Password = "********"; 
SmtpClient smtp = new SmtpClient("");
smtp.Credentials = Credentials;
smtp.Host = "";

Or send mail using gmail/ POP email. You also need to enable POP in your Gmail account.

Login to Gmail : settings > Forwarding and POP in your gmail account

SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("", "YourGmailPassword");
smtp.EnableSsl = true;

Points of Interest 

Here use of different classes of System.Net.Mail and use op optional parameters in a method is applied.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
MCPD 3.5 in 2011
Working as a Senior Dot Net Developer/Integration Engineer since last six years

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by SoumenBanerjee
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid