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.
Click on the image to play Video.
How it Works:
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 == true) which 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.
- 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:
- 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.