شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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