public string SetSmtpSettings(SMTPProperties objSmtp,string userID) { string msg = string.Empty; SqlConnection con = new SqlConnection(ConStr); try { SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = con; sqlCmd.CommandText = "sp_Set_SmtpSettings"; sqlCmd.CommandType = CommandType.StoredProcedure; SqlParameter spSmtpServer = sqlCmd.Parameters.Add("@SmtpServer", SqlDbType.VarChar, 50); spSmtpServer.Value =objSmtp.Server; SqlParameter spSmtpPort = sqlCmd.Parameters.Add("@SmtpPort", SqlDbType.Int); spSmtpPort.Value = objSmtp.Port; SqlParameter spSmtpUserID = sqlCmd.Parameters.Add("@SmtpUserID", SqlDbType.VarChar, 50); spSmtpUserID.Value = objSmtp.UserName; SqlParameter spSmtpPwd = sqlCmd.Parameters.Add("@SmtpPwd", SqlDbType.VarChar, 50); spSmtpPwd.Value = objSmtp.PassWord; SqlParameter spSmtpMailFrom = sqlCmd.Parameters.Add("@EmailFrom", SqlDbType.VarChar, 50); spSmtpMailFrom.Value = objSmtp.MailFrom; SqlParameter spSmtpMailTo = sqlCmd.Parameters.Add("@EmailTo", SqlDbType.VarChar, 50); spSmtpMailTo.Value = objSmtp.MailTo; SqlParameter spSmtpIsSSL = sqlCmd.Parameters.Add("@IsSSL", SqlDbType.Bit); spSmtpIsSSL.Value = objSmtp.IsSSL; SqlParameter spSmtpIsHTML = sqlCmd.Parameters.Add("@IsHTML", SqlDbType.Bit); spSmtpIsHTML.Value = objSmtp.IsHTML; SqlParameter spSmtpMailPriority = sqlCmd.Parameters.Add("@EmailPriority", SqlDbType.Char,1); spSmtpMailPriority.Value = objSmtp.EmailPriority; SqlParameter spUserID = sqlCmd.Parameters.Add("@UserID", SqlDbType.VarChar, 50); spUserID.Value = userID; con.Open(); sqlCmd.ExecuteNonQuery(); msg = "SMTP Settings Inserted Successfully..."; } catch (SqlException SqlEx) { objNLog.Error("SQLException : " + SqlEx.Message); throw new Exception("Exception re-Raised from DL with SQLError# " + SqlEx.Number + " while Inserting SMTP Settings.", SqlEx); } catch (Exception ex) { objNLog.Error("Exception : " + ex.Message); throw new Exception("**Error occured while Inserting SMTP Settings.", ex); } finally { con.Close(); } return msg; }
private void SendEmailMessage(string Message) { try { SmtpSettings objSmtp = new SmtpSettings(); DataSet dsSmtp = objSmtp.GetSmtpSettings(); if (dsSmtp.Tables.Count > 0) { if (dsSmtp.Tables[0].Rows.Count > 0) { SMTPProperties objSmtpProp = new SMTPProperties(); SMTPSendEMail objEmail = new SMTPSendEMail(); objSmtpProp.Server = dsSmtp.Tables[0].Rows[0][0].ToString(); objSmtpProp.Port = int.Parse(dsSmtp.Tables[0].Rows[0][1].ToString()); objSmtpProp.UserName = dsSmtp.Tables[0].Rows[0][2].ToString(); objSmtpProp.PassWord = dsSmtp.Tables[0].Rows[0][3].ToString(); if (txtEmailFrom.Text != "") objSmtpProp.MailFrom = txtEmailFrom.Text; else objSmtpProp.MailFrom = dsSmtp.Tables[0].Rows[0][4].ToString(); objSmtpProp.MailTo = txtEmailTo.Text; objSmtpProp.IsSSL = bool.Parse(dsSmtp.Tables[0].Rows[0][6].ToString()); objSmtpProp.IsHTML = bool.Parse(dsSmtp.Tables[0].Rows[0][7].ToString()); char mailPriority = char.Parse(dsSmtp.Tables[0].Rows[0][8].ToString()); switch (mailPriority) { case 'L': { objSmtpProp.MailPriority = SMTPProperties.Priority.Low; break; } case 'H': { objSmtpProp.MailPriority = SMTPProperties.Priority.High; break; } case 'N': { objSmtpProp.MailPriority = SMTPProperties.Priority.Normal; break; } } if (txtEmailSubject.Text != "") objSmtpProp.MailSubject = txtEmailSubject.Text; else objSmtpProp.MailSubject = "Call Log"; if (txtEmailMessage.Text != "") objSmtpProp.MailMessage = txtEmailMessage.Text + "<br/>" + Message; else objSmtpProp.MailMessage = Message; MailResponse objMailResp = objEmail.SendEmail(objSmtpProp); string str = "alert('" + objMailResp.StatusMessage + "');"; ScriptManager.RegisterStartupScript(imgBtnSendEmail, typeof(Page), "alert", str, true); } } } catch (Exception ex) { objNLog.Error("Error : " + ex.Message); } }
private void GetSmtpSettings() { SmtpSettings objSmtp = new SmtpSettings(); DataSet dsSmtp = objSmtp.GetSmtpSettings(); if (dsSmtp.Tables.Count > 0) { if (dsSmtp.Tables[0].Rows.Count > 0) { btnSmtpUpdate.Visible = true; btnSmtpSubmit.Visible = false; SMTPProperties objSmtpProp = new SMTPProperties(); SMTPSendEMail objEmail = new SMTPSendEMail(); if(dsSmtp.Tables[0].Rows[0][0]!= System.DBNull.Value) txtSmtpServer.Text = dsSmtp.Tables[0].Rows[0][0].ToString(); if (dsSmtp.Tables[0].Rows[0][1] != System.DBNull.Value) txtSmtpPort.Text = dsSmtp.Tables[0].Rows[0][1].ToString(); if (dsSmtp.Tables[0].Rows[0][2] != System.DBNull.Value) txtSmtpUserID.Text = dsSmtp.Tables[0].Rows[0][2].ToString(); if (dsSmtp.Tables[0].Rows[0][3] != System.DBNull.Value) txtSmtpPassword.Text = dsSmtp.Tables[0].Rows[0][3].ToString(); if (dsSmtp.Tables[0].Rows[0][4] != System.DBNull.Value) txtMailFrom.Text = dsSmtp.Tables[0].Rows[0][4].ToString(); if (dsSmtp.Tables[0].Rows[0][5] != System.DBNull.Value) txtMailTo.Text = dsSmtp.Tables[0].Rows[0][5].ToString(); if (dsSmtp.Tables[0].Rows[0][6] != System.DBNull.Value) chkIsSSL.Checked = bool.Parse(dsSmtp.Tables[0].Rows[0][6].ToString()); if (dsSmtp.Tables[0].Rows[0][7] != System.DBNull.Value) chkIsHTML.Checked = bool.Parse(dsSmtp.Tables[0].Rows[0][7].ToString()); char mailPriority='N'; if (dsSmtp.Tables[0].Rows[0][8] != System.DBNull.Value) mailPriority = char.Parse(dsSmtp.Tables[0].Rows[0][8].ToString()); switch (mailPriority) { case 'L': { ddlMailPriority.SelectedValue = "L"; break; } case 'H': { ddlMailPriority.SelectedValue = "H"; break; } case 'N': { ddlMailPriority.SelectedValue = "N"; break; } } } } else { btnSmtpSubmit.Visible = true; btnSmtpUpdate.Visible = false; } }
public static string GetSMTPProperty(EmailFunctions Function, SMTPProperties Property) { return(ConfigurationManager.AppSettings.Get(string.Format("SMTP_{0}.{1}", Function.ToString(), Property.ToString()))); }
public MailResponse SendEmail(SMTPProperties objSmtp) { try { System.Net.Mail.MailMessage emailMsg = new System.Net.Mail.MailMessage(); if (objSmtp.MailFrom != "") emailMsg.From = new System.Net.Mail.MailAddress(objSmtp.MailFrom); if(objSmtp.MailTo!="") emailMsg.To.Add( objSmtp.MailTo); if (objSmtp.MailCc != null) { for (int ccIndex = 0; ccIndex < objSmtp.MailCc.Count(); ccIndex++) { emailMsg.CC.Add(new System.Net.Mail.MailAddress(objSmtp.MailCc[ccIndex])); } } if (objSmtp.MailBcc != null) { for (int bccIndex = 0; bccIndex < objSmtp.MailBcc.Count(); bccIndex++) { emailMsg.Bcc.Add(new System.Net.Mail.MailAddress(objSmtp.MailBcc[bccIndex])); } } emailMsg.Subject = objSmtp.MailSubject; emailMsg.Body = objSmtp.MailMessage; emailMsg.IsBodyHtml = objSmtp.IsHTML; if (objSmtp.MailPriority == SMTPProperties.Priority.Normal) emailMsg.Priority = System.Net.Mail.MailPriority.Normal; if (objSmtp.MailPriority == SMTPProperties.Priority.High) emailMsg.Priority = System.Net.Mail.MailPriority.High; if (objSmtp.MailPriority == SMTPProperties.Priority.Low) emailMsg.Priority = System.Net.Mail.MailPriority.Low; if (objSmtp.MailAttachment != null) { Attachment att = new Attachment(objSmtp.MailAttachment); emailMsg.Attachments.Add(att); } System.Net.Mail.SmtpClient emailClient = new System.Net.Mail.SmtpClient(); emailClient.EnableSsl = objSmtp.IsSSL; emailClient.Host = objSmtp.Server; emailClient.Port = objSmtp.Port; emailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; System.Net.NetworkCredential emailCredential = new System.Net.NetworkCredential(); emailCredential.UserName = objSmtp.UserName; emailCredential.Password = objSmtp.PassWord; //emailClient.UseDefaultCredentials = true; emailClient.Credentials = emailCredential; // Send the message to the defined e-mail for processing and delivery with feedback. //string randomToken = "randonTokenTestValue"; //emailClient.SendAsync( emailMsg, randomToken ); object userState = emailMsg; try { //you can also call emailClient.SendAsync(emailMsg, userState); emailClient.Send(emailMsg); objMsgResp.StatusMessage = "Mail Sent Successfully"; } catch (System.Net.Mail.SmtpException ex) { objMsgResp.StatusMessage = "SMTP Error: " + ex.Message; } emailMsg.Dispose(); } catch (System.Net.Mail.SmtpException exSmtp) { objMsgResp.StatusMessage = "SMTP Error: " + exSmtp.Message; } catch (System.Exception exGen) { objMsgResp.StatusMessage = "Error: " + exGen.Message; } return objMsgResp; }