May 18, 2015

[Working Solution] Send Email From C# C-Sharp

Let Me Explain You How To Send Emails Through C# Coding.
Using Reference System.Net and System.Net.Mail

//You Will Be Able Understand The Code By Comments
//IF You Face Any Problems Please Contact Me.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
//First You Will Have To Include All These Libraries

namespace PMSWeb.Helpers
{
    public static class EmailHelper
    {  
public static void SendEmail(string fromUser, string toUsers, string ccUsers, string subject, string EmailMessage)
        {
//fromUser - will be the name of your Project . eg. Project Management
//toUsers - List of emails addresses separated by a ,(coma) this will we the To Addresses of the email eg. "test@gmail.com,test@yahoo.com"
//ccUsers - List of emails of CC users separated by a ,(coma)
//subject - subject of the Email Your Sending
//EmailMessage - This will be your email, the body of the email in other words


            string fromUserAddress = "yourEmail@domain";
            // The Email Address Which You Will Be using for Sending the Email.
            string fromUserPassword = "yourEmailPassword";
           //This will be your email addresses password
            string emailHost = "mail.gmail.com";
          //This will be your domains host id eg. mail.gmail.com  this is Gmails Host
            try
            {

                MailMessage message = new MailMessage();
//Create an object from MailMessage
//now (message) object will be an email instance

                string[] ToMuliId = toUsers.Split(',');
                foreach (string ToEMailId in ToMuliId)
                {
                    message.To.Add(new MailAddress(ToEMailId)); //adding multiple Email Addresses
                }

                if (ccUsers != null)
                {
                    string[] CCId = ccUsers.Split(',');

                    foreach (string CCEmail in CCId)
                    {
                        message.CC.Add(new MailAddress(CCEmail));
                       //Adding Multiple CC Email Addresses
                    }
                }
//you could use this if you want BCC also. and you will have to add a parameter for this class
                //string[] bccid = bccUsers.Split(',');

                //foreach (string bccEmailId in bccid)
                //{
                //    message.Bcc.Add(new MailAddress(bccEmailId)); //Adding Multiple BCC email Id
                //}


                message.Subject = subject;
                //assigning the subject received from the parameter to the emails object's subject

                message.IsBodyHtml = true;
                // you can send an HTML String With Styles and Stuff if you keep this true.
//if its just a string email you can keep it false. But you could still send string emails when it is //true

                message.Body = EmailMessage;
               // this will be the place you add the emailMessage to the body of your Email.

                message.From = new MailAddress(fromUserAddress, fromUser);
// sending your email address and your name.

                SmtpClient smtp = new SmtpClient(emailHost, 587);
// your emailHost and and the port , 587 would would work in most cases
             
                //smtp.EnableSsl = true;
//depending on your email host you could make the ssl true or false.


                // Do not send the DefaultCredentials with requests
// Don't know why,  I Also was asked not To.
                smtp.UseDefaultCredentials = false;

                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

  smtp.Credentials = new NetworkCredential(fromUserAddress, fromUserPassword);
//passing your email address and password.

                smtp.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
                     
        }
    }
}

No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...