)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Smtp (Ia.Cl.Model)

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:             Send(name, email, subject, cc, bcc, content, MailType.Plain/*, out result*/);
   46:  
   47:             return true;
   48:         }
   49:  
   50:         ////////////////////////////////////////////////////////////////////////////
   51:  
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         public static bool SendHtml(string name, string email, string subject, string cc, string bcc, string content/*, out string result*/)
   56:         {
   57:             Send(name, email, subject, cc, bcc, content, MailType.Html/*, out result*/);
   58:  
   59:             return true;
   60:         }
   61:  
   62:         ////////////////////////////////////////////////////////////////////////////
   63:  
   64:         /// <summary>
   65:         ///
   66:         /// </summary>
   67:         public static bool SendPlain(string name, string email, string subject, string cc, string content/*, out string result*/)
   68:         {
   69:             Send(name, email, subject, cc, null, content, MailType.Plain/*, out result*/);
   70:  
   71:             return true;
   72:         }
   73:  
   74:         ////////////////////////////////////////////////////////////////////////////
   75:  
   76:         /// <summary>
   77:         ///
   78:         /// </summary>
   79:         public static bool SendHtml(string name, string email, string subject, string cc, string content/*, out string result*/)
   80:         {
   81:             Send(name, email, subject, cc, null, content, MailType.Html/*, out result*/);
   82:  
   83:             return true;
   84:         }
   85:  
   86:         ////////////////////////////////////////////////////////////////////////////
   87:  
   88:         /// <summary>
   89:         ///
   90:         /// </summary>
   91:         public static bool SendPlain(string name, string email, string subject, string content/*, out string result*/)
   92:         {
   93:             Send(name, email, subject, null, null, content, MailType.Plain/*, out result*/);
   94:  
   95:             return true;
   96:         }
   97:  
   98:         ////////////////////////////////////////////////////////////////////////////
   99:  
  100:         /// <summary>
  101:         ///
  102:         /// </summary>
  103:         public static bool SendHtml(string name, string email, string subject, string content/*, out string result*/)
  104:         {
  105:             Send(name, email, subject, null, null, content, MailType.Html/*, out result*/);
  106:  
  107:             return true;
  108:         }
  109:  
  110:         ////////////////////////////////////////////////////////////////////////////
  111:  
  112:         /// <summary>
  113:         ///
  114:         /// </summary>
  115:         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*/)
  116:         {
  117:             Send(name, email, subject, null, null, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Plain, enableSsl/*, out result*/); //.Wait();
  118:  
  119:             return true;
  120:         }
  121:  
  122:         ////////////////////////////////////////////////////////////////////////////
  123:  
  124:         /// <summary>
  125:         ///
  126:         /// </summary>
  127:         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*/)
  128:         {
  129:             Send(name, email, subject, null, null, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Html, enableSsl/*, out result*/); //.Wait();
  130:  
  131:             return true;
  132:         }
  133:  
  134:         ////////////////////////////////////////////////////////////////////////////
  135:  
  136:         /// <summary>
  137:         ///
  138:         /// </summary>
  139:         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*/)
  140:         {
  141:             Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, MailType.Plain, enableSsl/*, out result*/); //.Wait();
  142:  
  143:             return true;
  144:         }
  145:  
  146:         ////////////////////////////////////////////////////////////////////////////
  147:  
  148:         /// <summary>
  149:         ///
  150:         /// </summary>
  151:         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*/)
  152:         {
  153:             Send(name, email, subject, cc, bcc, 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 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*/)
  164:         {
  165:             MailType mailType;
  166:  
  167:             //result = "";
  168:  
  169:             if (mailTypeString == "html" || mailTypeString == "plain")
  170:             {
  171:                 if (mailTypeString == "html") mailType = MailType.Html;
  172:                 else /*if (mail_type == "plain")*/ mailType = MailType.Plain;
  173:  
  174:                 Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, mailType, enableSsl/*, out result*/); //.Wait();
  175:             }
  176:             else
  177:             {
  178:                 //b = false;
  179:             }
  180:  
  181:             return true;
  182:         }
  183:  
  184:         ////////////////////////////////////////////////////////////////////////////
  185:  
  186:         /// <summary>
  187:         ///
  188:         /// </summary>
  189:         private static bool Send(string name, string email, string subject, string cc, string bcc, string content, MailType mailType/*, out string result*/)
  190:         {
  191:             bool smtpServerEnableSsl;
  192:             string smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword;
  193:  
  194:             /* 
  195:              * app.config:
  196:              * <appSettings>
  197:              * <add key="smtpServerHost" value="*" />
  198:              * <add key="smtpServerUserName" value="*" />
  199:              * <add key="smtpServerUser" value="*" />
  200:              * <add key="smtpServerPassword" value="*" />
  201:              * <add key="smtpServerEnableSsl" value="*" />             
  202:              */
  203:  
  204:             smtpServerUserName = ConfigurationManager.AppSettings["smtpServerUserName"].ToString();
  205:             smtpServerHost = ConfigurationManager.AppSettings["smtpServerHost"].ToString();
  206:             smtpServerUser = ConfigurationManager.AppSettings["smtpServerUser"].ToString();
  207:             smtpServerPassword = ConfigurationManager.AppSettings["smtpServerPassword"].ToString();
  208:             smtpServerEnableSsl = bool.TryParse(ConfigurationManager.AppSettings["smtpServerEnableSsl"].ToString(), out bool b) && b;
  209:  
  210:             Send(name, email, subject, cc, bcc, content, smtpServerHost, smtpServerUserName, smtpServerUser, smtpServerPassword, mailType, smtpServerEnableSsl/*, out result*/); //.Wait();
  211:  
  212:             return true;
  213:         }
  214:  
  215:         ////////////////////////////////////////////////////////////////////////////
  216:  
  217:         /// <summary>
  218:         ///
  219:         /// </summary>
  220:         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*/)
  221:         {
  222:             bool b;
  223:             int port;
  224:             MailAddress fromMailAddress, toMailAddress;
  225:             MailMessage mailMessage;
  226:             NetworkCredential credential;
  227:  
  228:             // If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=\"true\" %>
  229:  
  230:             b = false;
  231:             port = 587;
  232:  
  233:             credential = new NetworkCredential(smtpServerUser, smtpServerPassword);
  234:  
  235:             using (SmtpClient smtpClient = new SmtpClient(smtpServerHost, port))
  236:             {
  237:                 smtpClient.EnableSsl = enableSsl;
  238:                 smtpClient.UseDefaultCredentials = false;
  239:                 smtpClient.Credentials = credential;
  240:  
  241:                 fromMailAddress = new MailAddress(smtpServerUser, smtpServerUserName, Encoding.UTF8);
  242:                 toMailAddress = new MailAddress(email, name, Encoding.UTF8);
  243:  
  244:                 mailMessage = new MailMessage(fromMailAddress, toMailAddress)
  245:                 {
  246:                     Subject = subject,
  247:                     SubjectEncoding = Encoding.UTF8,
  248:                     BodyEncoding = Encoding.UTF8
  249:                 };
  250:  
  251:                 if (cc != null)
  252:                 {
  253:                     foreach (string s in cc.Split(';'))
  254:                     {
  255:                         // mm.CC.Add(new MailAddress(s, name, Encoding.UTF8));
  256:                     }
  257:                 }
  258:  
  259:                 if (bcc != null)
  260:                 {
  261:                     foreach (string s in bcc.Split(';'))
  262:                     {
  263:                         mailMessage.Bcc.Add(new MailAddress(s)); //, name, Encoding.UTF8));
  264:                     }
  265:                 }
  266:  
  267:                 //result = "";
  268:  
  269:                 if (mailType == MailType.Html || mailType == MailType.Plain)
  270:                 {
  271:                     mailMessage.Body = content;
  272:  
  273:                     if (mailType == MailType.Html) mailMessage.IsBodyHtml = true;
  274:                     else if (mailType == MailType.Plain) mailMessage.IsBodyHtml = false;
  275:  
  276:                     try
  277:                     {
  278:                         //send_state = "send without blocking but no guarantee code";
  279:                         smtpClient.Send(mailMessage); //.SendAsync(mm,send_state);
  280:                         //await smtpClient.SendMailAsync(mailMessage);
  281:  
  282:                         b = true;
  283:                     }
  284:                     catch (SmtpException ex)
  285:                     {
  286: #if DEBUG
  287:                         //result = "Error in Ia.Cl.Models.Mail.Send(): Your email couldn't be sent: " + ex.ToString();
  288: #else
  289:                         //result = "Error in Ia.Cl.Models.Mail.Send(): Your email couldn't be sent: " + ex.Message;
  290: #endif
  291:                         b = false;
  292:  
  293:                         Debug.WriteLine("Send() exception: " + ex);
  294:                     }
  295:                     finally
  296:                     {
  297:                         Debug.WriteLine("Send() done");
  298:                     }
  299:                 }
  300:             }
  301:  
  302:             return b;
  303:         }
  304:  
  305:         ////////////////////////////////////////////////////////////////////////////
  306:         ////////////////////////////////////////////////////////////////////////////
  307:     }
  308: }