Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Background

Though we have implemented mail functionality several times but still every day several developer asks for code to sent mail from asp.net 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);
_email.Attachments.Add(attach);

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 = "yourdomaim.com";
Credentials.UserName = "user@yourdomaim.com";
Credentials.Password = "********"; 
SmtpClient smtp = new SmtpClient("mail.yourdomaim.com");
smtp.Credentials = Credentials;
smtp.Host = "mail.yourdomaim.com";

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.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com", "YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(_email);

Points of Interest 

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

License

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

Share

About the Author

SoumenBanerjee
Software Developer (Senior) ERICSSON INDIA GLOBAL SERVICES PVT. LTD
India India
MCPD 3.5 in 2011
Working as a Senior Dot Net Developer/Integration Engineer since last six years

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by SoumenBanerjee
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid