Creating a mail message using C#

Now that you know the complete email sending process and the role played by C# in it let’s learn how to create a mail message using C#

How to create a mail message

In C# there is a dedicated class given to you for creating mail message. This class is present in the System.Net.Mail namespace and the name of the class is MailMessage. This class helps you in creating a complete mail message with subject, body, sender information, receiver information, cc list, bcc list, attachments etc. To create your email message you will have to create an object of this class and set the appropriate properties

Look at the code example below

static void Main(string[] args)
        {      
            // Create object of the mail message class      
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

            // Give a subject to your message      
            message.Subject = "My first email using C#";
            
            // Write the body content      
            message.Body = "This is where you write your email message.";
            
            // Add the sender email id      
            message.From = new System.Net.Mail.MailAddress("team@openeschool.com");
            
            // Add all email ids you want to send this mail to      
            message.To.Add("friend1@gmail.com");
            message.To.Add("friend2@yahoo.com");
            message.To.Add("friend3@outlook.com");
            
            // Add people in the cc list      
            message.CC.Add("ccuser1@gmail.com");
            message.CC.Add("ccuser2@yahoo.com");
            
            // Add people to the bcc list      
            message.Bcc.Add("bccuser1@gmail.com");
            message.Bcc.Add("bccuser2@gmail.com");
        }

Read the comments in the above code to understand what it does. Here we are just creating the mail message and adding all the important information which any email message should contain. Till this point we are not sending the email. We are just creating the message and doing nothing. In the next tutorial you will learn how to send this mail message.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp