Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Smtp

Public general use code classes and xml files that we've compiled and used over the years:

SMTP send mail server suppot class.

   1:  using System;
   2:  using System.Web;
   3:  using System.IO;
   4:  using System.Xml;
   5:  using System.Data;
   6:  using System.Collections;
   7:  using System.Globalization;
   8:  using System.Configuration;
   9:  using System.Net;
  10:  using System.Text;
  11:  using System.Net.Mail;
  12:  using System.Diagnostics;
  13:  using System.Threading.Tasks;
  14:   
  15:  namespace Ia.Cl.Model
  16:  {
  17:      ////////////////////////////////////////////////////////////////////////////
  18:   
  19:      /// <summary publish="true">
  20:      /// SMTP send mail server suppot class.
  21:      /// </summary>
  22:      /// <remarks> 
  23:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  24:      ///
  25:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  26:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  27:      ///
  28:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  29:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  30:      /// 
  31:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  32:      /// 
  33:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  34:      /// </remarks> 
  35:      public class Smtp
  36:      {
  37:          private enum MailType { Plain, Html };
  38:   
  39:          ////////////////////////////////////////////////////////////////////////////
  40:   
  41:          /// <summary>
  42:          ///
  43:          /// </summary>
  44:          public Smtp() { }
  45:   
  46:          ////////////////////////////////////////////////////////////////////////////
  47:   
  48:          /// <summary>
  49:          ///
  50:          /// </summary>
  51:          public static bool SendPlain(string name, string email, string subject, string cc, string bcc, string content/*, out string result*/)
  52:          {
  53:              bool b;
  54:   
  55:              Send(name, email, subject, cc, bcc, content, MailType.Plain/*, out result*/);
  56:   
  57:              return true;
  58:          }
  59:   
  60:          ////////////////////////////////////////////////////////////////////////////
  61:   
  62:          /// <summary>
  63:          ///
  64:          /// </summary>
  65:          public static bool SendHtml(string name, string email, string subject, string cc, string bcc, string content/*, out string result*/)
  66:          {
  67:              bool b;
  68:   
  69:              Send(name, email, subject, cc, bcc, content, MailType.Html/*, out result*/);
  70:   
  71:              return true;
  72:          }
  73:   
  74:          ////////////////////////////////////////////////////////////////////////////
  75:   
  76:          /// <summary>
  77:          ///
  78:          /// </summary>
  79:          public static bool SendPlain(string name, string email, string subject, string cc, string content/*, out string result*/)
  80:          {
  81:              bool b;
  82:   
  83:              Send(name, email, subject, cc, null, content, MailType.Plain/*, out result*/);
  84:   
  85:              return true;
  86:          }
  87:   
  88:          ////////////////////////////////////////////////////////////////////////////
  89:   
  90:          /// <summary>
  91:          ///
  92:          /// </summary>
  93:          public static bool SendHtml(string name, string email, string subject, string cc, string content/*, out string result*/)
  94:          {
  95:              bool b;
  96:   
  97:              Send(name, email, subject, cc, null, content, MailType.Html/*, out result*/);
  98:   
  99:              return true;
 100:          }
 101:   
 102:          ////////////////////////////////////////////////////////////////////////////
 103:   
 104:          /// <summary>
 105:          ///
 106:          /// </summary>
 107:          public static bool SendPlain(string name, string email, string subject, string content/*, out string result*/)
 108:          {
 109:              bool b;
 110:   
 111:              Send(name, email, subject, null, null, content, MailType.Plain/*, out result*/);
 112:   
 113:              return true;
 114:          }
 115:   
 116:          ////////////////////////////////////////////////////////////////////////////
 117:   
 118:          /// <summary>
 119:          ///
 120:          /// </summary>
 121:          public static bool SendHtml(string name, string email, string subject, string content/*, out string result*/)
 122:          {
 123:              bool b;
 124:   
 125:              Send(name, email, subject, null, null, content, MailType.Html/*, out result*/);
 126:   
 127:              return true;
 128:          }
 129:   
 130:          ////////////////////////////////////////////////////////////////////////////
 131:   
 132:          /// <summary>
 133:          ///
 134:          /// </summary>
 135:          public static bool SendPlain(string name, string email, string subject, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, bool enableSsl/*, out string result*/)
 136:          {
 137:              bool b;
 138:   
 139:              Send(name, email, subject, null, null, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Plain, enableSsl/*, out result*/); //.Wait();
 140:   
 141:              return true;
 142:          }
 143:   
 144:          ////////////////////////////////////////////////////////////////////////////
 145:   
 146:          /// <summary>
 147:          ///
 148:          /// </summary>
 149:          public static bool SendHtml(string name, string email, string subject, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, bool enableSsl/*, out string result*/)
 150:          {
 151:              bool b;
 152:   
 153:              Send(name, email, subject, null, null, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Html, enableSsl/*, out result*/); //.Wait();
 154:   
 155:              return true;
 156:          }
 157:   
 158:          ////////////////////////////////////////////////////////////////////////////
 159:   
 160:          /// <summary>
 161:          ///
 162:          /// </summary>
 163:          public static bool SendPlain(string name, string email, string subject, string cc, string bcc, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, bool enableSsl/*, out string result*/)
 164:          {
 165:              bool b;
 166:   
 167:              Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Plain, enableSsl/*, out result*/); //.Wait();
 168:   
 169:              return true;
 170:          }
 171:   
 172:          ////////////////////////////////////////////////////////////////////////////
 173:   
 174:          /// <summary>
 175:          ///
 176:          /// </summary>
 177:          public static bool SendHtml(string name, string email, string subject, string cc, string bcc, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, bool enableSsl/*, out string result*/)
 178:          {
 179:              bool b;
 180:   
 181:              Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Html, enableSsl/*, out result*/); //.Wait();
 182:   
 183:              return true;
 184:          }
 185:   
 186:          ////////////////////////////////////////////////////////////////////////////
 187:   
 188:          /// <summary>
 189:          ///
 190:          /// </summary>
 191:          public static bool Send(string name, string email, string subject, string cc, string bcc, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, string mailTypeString, bool enableSsl/*, out string result*/)
 192:          {
 193:              bool b;
 194:              MailType mailType;
 195:   
 196:              //result = "";
 197:   
 198:              if (mailTypeString == "html" || mailTypeString == "plain")
 199:              {
 200:                  if (mailTypeString == "html") mailType = MailType.Html;
 201:                  else /*if (mail_type == "plain")*/ mailType = MailType.Plain;
 202:   
 203:                  Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, mailType, enableSsl/*, out result*/); //.Wait();
 204:              }
 205:              else
 206:              {
 207:                  b = false;
 208:              }
 209:   
 210:              return true;
 211:          }
 212:   
 213:          ////////////////////////////////////////////////////////////////////////////
 214:   
 215:          /// <summary>
 216:          ///
 217:          /// </summary>
 218:          private static bool Send(string name, string email, string subject, string cc, string bcc, string content, MailType mailType/*, out string result*/)
 219:          {
 220:              bool b, smtpServerEnableSsl;
 221:              string smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword;
 222:   
 223:              /* 
 224:               * app.config:
 225:               * <appSettings>
 226:               * <add key="smtpServerHost" value="*" />
 227:               * <add key="smtpServerUserName" value="*" />
 228:               * <add key="smtpServerUser" value="*" />
 229:               * <add key="smtpServerPassword" value="*" />
 230:               * <add key="smtpServerEnableSsl" value="*" />             
 231:               */
 232:   
 233:              smtpServerUserName = ConfigurationManager.AppSettings["smtpServerUserName"].ToString();
 234:              smtpServerHost = ConfigurationManager.AppSettings["smtpServerHost"].ToString();
 235:              smtpServerUser = ConfigurationManager.AppSettings["smtpServerUser"].ToString();
 236:              smtpServerPassword = ConfigurationManager.AppSettings["smtpServerPassword"].ToString();
 237:              smtpServerEnableSsl = bool.TryParse(ConfigurationManager.AppSettings["smtpServerEnableSsl"].ToString(), out b) ? b : false;
 238:   
 239:              Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, mailType, smtpServerEnableSsl/*, out result*/); //.Wait();
 240:   
 241:              return true;
 242:          }
 243:   
 244:          ////////////////////////////////////////////////////////////////////////////
 245:   
 246:          /// <summary>
 247:          ///
 248:          /// </summary>
 249:          private static /*async Task<*/bool/*>*/ Send/*Async*/(string name, string email, string subject, string cc, string bcc, string content, string smtpServerHost, string smtpServerUserName, string smtpServerUser, string smtpServerPassword, MailType mailType, bool enableSsl/*, out string result*/)
 250:          {
 251:              bool b;
 252:              int port;
 253:              MailAddress fromMailAddress, toMailAddress;
 254:              MailMessage mailMessage;
 255:              NetworkCredential credential;
 256:   
 257:              // If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=\"true\" %>
 258:   
 259:              b = false;
 260:              port = 587;
 261:   
 262:              credential = new NetworkCredential(smtpServerUser, smtpServerPassword);
 263:   
 264:              using (SmtpClient smtpClient = new SmtpClient(smtpServerHost, port))
 265:              {
 266:                  smtpClient.EnableSsl = enableSsl;
 267:                  smtpClient.UseDefaultCredentials = false;
 268:                  smtpClient.Credentials = credential;
 269:   
 270:                  fromMailAddress = new MailAddress(smtpServerUser, smtpServerUserName, Encoding.UTF8);
 271:                  toMailAddress = new MailAddress(email, name, Encoding.UTF8);
 272:   
 273:                  mailMessage = new MailMessage(fromMailAddress, toMailAddress);
 274:                  mailMessage.Subject = subject;
 275:                  mailMessage.SubjectEncoding = Encoding.UTF8;
 276:                  mailMessage.BodyEncoding = Encoding.UTF8;
 277:   
 278:                  if (cc != null)
 279:                  {
 280:                      foreach (string s in cc.Split(';'))
 281:                      {
 282:                          // mm.CC.Add(new MailAddress(s, name, Encoding.UTF8));
 283:                      }
 284:                  }
 285:   
 286:                  if (bcc != null)
 287:                  {
 288:                      foreach (string s in bcc.Split(';'))
 289:                      {
 290:                          mailMessage.Bcc.Add(new MailAddress(s)); //, name, Encoding.UTF8));
 291:                      }
 292:                  }
 293:   
 294:                  //result = "";
 295:   
 296:                  if (mailType == MailType.Html || mailType == MailType.Plain)
 297:                  {
 298:                      mailMessage.Body = content;
 299:   
 300:                      if (mailType == MailType.Html) mailMessage.IsBodyHtml = true;
 301:                      else if (mailType == MailType.Plain) mailMessage.IsBodyHtml = false;
 302:   
 303:                      try
 304:                      {
 305:                          //send_state = "send without blocking but no guarantee code";
 306:                          smtpClient.Send(mailMessage); //.SendAsync(mm,send_state);
 307:                          //await smtpClient.SendMailAsync(mailMessage);
 308:   
 309:                          b = true;
 310:                      }
 311:                      catch (SmtpException ex)
 312:                      {
 313:  #if DEBUG
 314:                          //result = "Error in global::Ia.Cl.Model.Mail.Send(): Your email couldn't be sent: " + ex.ToString();
 315:  #else
 316:                          //result = "Error in global::Ia.Cl.Model.Mail.Send(): Your email couldn't be sent: " + ex.Message;
 317:  #endif
 318:                          b = false;
 319:   
 320:                          Debug.WriteLine("Send() exception: " + ex);
 321:                      }
 322:                      finally
 323:                      {
 324:                          Debug.WriteLine("Send() done");
 325:                      }
 326:                  }
 327:              }
 328:   
 329:              return b;
 330:          }
 331:   
 332:          ////////////////////////////////////////////////////////////////////////////
 333:          ////////////////////////////////////////////////////////////////////////////
 334:      }
 335:  }