sending email attachement using smtp in asp.net

send emails with or without attachment from your asp.net application/website. A sample project is available for download at the end of this article.

What is it

The provided project shows how to send emails (with or without attachment) . The project uses wdw_utilities.dll which has methods defined to send emails.. Please download the project and open it in visual studio to refer to as we go through the code. The project requires you to provide smtp credentials. So create an email account on your hosting server (if you have one) or you can use gmail’s SMTP server.

For those who are wondering what the heck is SMTP :

in simple terms you use one email accounts credentials to send emails to others email accounts. If you have a hosting account then go create a new email account something like ‘no-reply@yourdomain.com’. Your hosting provider will inform you about the smtpserver, smtp port etc.. your emailId becomes the smtpusername and your email accounts password becomes the smtppassword.
Alternatively you can use gmail’s smtp server. Below are the details :

smtp server : smtp.gmail.com
smtp port : 587
smtp username : yourmail@gmail.com
smtp password : your gmail accounts password

**Please note : if you are using gmail’s smtp details then you must set useSSL to true.

Lets see some code

Open email.aspx.cs from the file list in solution explorer.
The line using wdwUtilities; adds a reference to the wdwUtilities.dll. You will find following code inside btn_sendemail_click() function (the click event of send email button)

string _smtphost = txt_smtphost.Text.Trim();
int _smtpport = Convert.ToInt32(txt_smtpport.Text.Trim());
string _smtpuser = txt_smtpuname.Text.Trim();
string _smtppassword = txt_smtppassword.Text.Trim();
Email obj = new Email(_smtphost, _smtpport, _smtpuser, _smtppassword, useSSL);

bool success = obj.sendEmail(txt_from_email.Text.Trim(), txt_to_email.Text.Trim(), txt_subject.Text.Trim(), txt_message.Text.Trim(), ref errs);

if (success) { master_hiddenFld.Value = "Email sent successfuly"; }
else { master_hiddenFld.Value = "Error sending email : \n" + errs; }

the lines string _smtphost = txt_smtphost.Text.Trim();.... sets things up for creating the object of Email class (defined in wdwUtilities namespace).

Email obj = new Email(_smtphost, _smtpport, _smtpuser, _smtppassword, useSSL);

parameters are self-explanatory i hope. The last parameter useSSL tells if you wanna use SSL for communication. If you are not sure whether it should be true or false then leave it as false. But make sure if you are using gmail’s smtp details then you set it to true.
Then you call the sendEmail() method of Email class passing appropriate values.

bool success = obj.sendEmail(txt_from_email.Text.Trim(), txt_to_email.Text.Trim(), txt_subject.Text.Trim(), txt_message.Text.Trim(), ref errs);

this method sends the email.
the return type is bool, the returned value of true indicates the email was sent successfuly while false means there was some error ๐Ÿ™
parameters : from_email and to_email are self-explanatory.
subject is the emails subject and message is the entire body of your email. You can write HTML inside the message string.
The last parameter (passed by reference) brings back the erros(if any) that occured while trying to send email.
There are multiple overloads of the methods in Email class, try them as well.
That’s it as of now.. do post your comments…
hope you find it usefull.. cheers..


Published by

Chandan Chaudhary

Chandan is a WordPress guy. A programmer at heart. He loves building community sites with WordPress and BuddyPress.

4 thoughts on “sending email attachement using smtp in asp.net”

  1. An incredibly fascinating concept. Have you got more justifications? Focus on, it's going to be a great blog in the foreseeable future

Leave a Reply

Your email address will not be published. Required fields are marked *