public static bool SendEmail(SMTPDetails smtpDetails, string emailToAddress, string subject, string body, string[] cc = null, string[] bcc = null, int tenantId = 0, string ConStrings = null) { bool isMailSent = false; ErrorLogs errorlog = new ErrorLogs(); try { SmtpClient smtpClient = new SmtpClient(smtpDetails.SMTPServer, Convert.ToInt32(smtpDetails.SMTPPort)); smtpClient.EnableSsl = smtpDetails.EnableSsl; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new NetworkCredential(smtpDetails.FromEmailId, smtpDetails.Password); { using (MailMessage message = new MailMessage()) { message.From = new MailAddress(smtpDetails.FromEmailId, smtpDetails.EmailSenderName); if (cc != null) { if (cc.Length > 0) { for (int i = 0; i < cc.Length; i++) { message.CC.Add(cc[i]); } } } if (bcc != null) { if (bcc.Length > 0) { for (int k = 0; k < bcc.Length; k++) { message.CC.Add(bcc[k]); } } } message.Subject = subject == null ? "" : subject; message.Body = body == null ? "" : body; message.IsBodyHtml = smtpDetails.IsBodyHtml; message.To.Add(emailToAddress); smtpClient.Send(message); } } isMailSent = true; } catch (Exception ex) { errorlog.SendErrorToText(ex, ConStrings); } return(isMailSent); }
public static SMTPDetails GetSMTPDetails(int TenantID, string ConStrings) { DataSet ds = new DataSet(); SMTPDetails sMTPDetails = new SMTPDetails(); ErrorLogs errorlog = new ErrorLogs(); try { MySqlCommand cmd = new MySqlCommand(); if (conn != null && conn.State == ConnectionState.Closed) { conn.Open(); } cmd.Connection = conn; MySqlCommand cmd1 = new MySqlCommand("SP_getSMTPDetails", conn); cmd1.CommandType = CommandType.StoredProcedure; cmd1.Parameters.AddWithValue("@Tenant_ID", TenantID); MySqlDataAdapter da = new MySqlDataAdapter(); da.SelectCommand = cmd1; da.Fill(ds); if (ds != null && ds.Tables[0] != null) { sMTPDetails.EnableSsl = Convert.ToBoolean(ds.Tables[0].Rows[0]["EnabledSSL"]); sMTPDetails.SMTPPort = Convert.ToString(ds.Tables[0].Rows[0]["SMTPPort"]); sMTPDetails.FromEmailId = Convert.ToString(ds.Tables[0].Rows[0]["EmailUserID"]); sMTPDetails.IsBodyHtml = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsBodyHtml"]); sMTPDetails.Password = Convert.ToString(ds.Tables[0].Rows[0]["EmailPassword"]); sMTPDetails.SMTPHost = Convert.ToString(ds.Tables[0].Rows[0]["SMTPHost"]); sMTPDetails.SMTPServer = Convert.ToString(ds.Tables[0].Rows[0]["SMTPHost"]); sMTPDetails.EmailSenderName = Convert.ToString(ds.Tables[0].Rows[0]["EmailSenderName"]); } } catch (Exception ex) { errorlog.SendErrorToText(ex, ConStrings); } finally { if (ds != null) { ds.Dispose(); } } return(sMTPDetails); }