<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Sending Mail through authenticated SMTP server</Title>
      <Shortcut>SendingMailthroughauthenticatedSMTPserver</Shortcut>
      <Description>Sending Mail through authenticated SMTP server [C#]</Description>
      <Author>Prasad GVL</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=1160c225-f161-4d64-8503-10e832289a48</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[        /// <summary>
        /// Prepares and sends an EMail
        /// </summary>
        /// <param name="sToAddress">Recipient(s), multiple recipients should be separated by ;</param>
        /// <param name="sFromAddress">Sender address</param>
        /// <param name="sCC">CC address</param>
        /// <param name="sSubject">Subject of Mail</param>
        /// <param name="sMessage">Body of Mail</param>
        /// <param name="sSMTPServer">Which server to use to send mail</param>
        /// <param name="bIsAuthReq">Boolean, indicating if authentication required for this server</param>
        /// <param name="sUserName">String, specifies UserName to login to SMTP Server</param>
        /// <param name="sPassword">String, specifies Password to login to SMTP Server</param>
        /// <returns>Success status(true/false)</returns>
        public static bool SendEMail(string sToAddress, string sFromAddress, string sCC, string sSubject, 
                            string sMessage, string sSMTPServer, bool bIsAuthReq, string sUserName, string sPassword)
        {
            MailMessage mmMessage=new MailMessage();

            mmMessage.To=sToAddress;
            mmMessage.From=sFromAddress;
            if (sCC.Length>0)
                mmMessage.Cc=sCC;
            mmMessage.Subject=sSubject;
            mmMessage.Priority=MailPriority.High;
            mmMessage.BodyFormat=MailFormat.Html;
            mmMessage.Body=sMessage;

            //Check whether target SMTP server requires authentication.
            //If yes, get authentication details from configuration settings.
            if (bIsAuthReq)
            {
                mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  //basic authentication
                mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sUserName); //set username 
                mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sPassword); //Set password 
            }

            try
            {
                SmtpMail.SmtpServer=sSMTPServer;
                SmtpMail.Send(mmMessage);
                return(true);
            }
            catch(Exception ex)
            {
                String sErr=ex.Message;
                return(false);
            }
        }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>