Send Email in C# Project with File Attachment

In one of my C# project, I needed to receive some files from my User side application through email. So Now i have created a Tutorial on Windows Form Application & Console Application for this purpose in which we will see the source code of sending emails through c#. Lets Check it.

Source Code :

As usual first of all we will see the back-end code. The main aim of Csharpens. What really is the code behind it.

Output:

Click on the image to play Video.

Here is the output of this application. At first it shows my email inbox in which i don’t have any emails from form application. Next when i execute it, It ask me for my email, my password, email subject, email body & the receiver email. In first email i did not include any file attachment to show that we can also send email without attachment using this application. In second email i include a file. After this i showed both of these emails received in my inbox.

How it Works:

After loading of form when we have to enter all the values in the relative textboxes. After then when we press Send Email Button it will call SendEmail(string
SenderEmail,
string SenderPassword, string EmailAttachment, string
EmailSubject,
string EmailBody, bool AttachmentStatus,string

ReceiverEmail)
 
Method. This method is receiving all the values in its parameters. This method will create a new MailMessage & SmtpClient Classes Object. Then it will configure To, From properties of MailMessage’s object mail. Then it will check if any file is attached or not by checking if (AttachmentStatus == truewhich is a boolean. This will be true if any file is attached. After attachment of file it will configure credentials of SmtpClient’ object SmtpServer. Once this part is completed the email will be tried to send. If email sent, it will return true or if any exception occurred it will popup a message-box.

Note:

  • In this application, i have used Google Gmail’s smtp therefore you will need to use gmail email.
  • Another step is that the email account which you are using should not have 2 step verification otherwise this will through an unverification exception.
  • I have changed the passwords in the downloadable file here so you will need to use your own email & password.

What Can you Do by This Feature:

You can do a number of function by using this feature. Some of them can be like
  • If you want to get Error-Log file of your User side application, you can use this feature so you will know what causes your application to stop working without user’s complain.
  • If you want to get the details of your application users you can use this feature to receive email when a new user start using.
  • Get the response or feedback from your application user about your application.
  • etc etc.

Download:

In this EmailSendingC#.rar file, i included both Form Application & a Console Application. Use any of them as you need.
File Size : 70 KB
Visual Studio 2010 Files (Windows Form & Console)