/// <summary> /// Sends an email via G-Mail with the provided email settings. /// </summary> /// <param name="fromEmailAddress"></param> /// <param name="toEmailAddress"></param> /// <param name="subject"></param> /// <param name="messageBody"></param> /// <param name="password"></param> /// <returns></returns> public static void send(string fromEmailAddress, string fromName, string toEmailAddress, string subject, string messageBody, string password, string server, int port, int timeout) { CDO.Message smtp = new CDO.Message(); CDO.Configuration smtpConfig = new CDO.Configuration(); ADODB.Fields fieldCollection = smtpConfig.Fields; ADODB.Field serverField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; serverField.Value = server; ADODB.Field usernameField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/sendusername"]; usernameField.Value = fromEmailAddress; ADODB.Field passwordField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; passwordField.Value = password; ADODB.Field authField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; authField.Value = 1; ADODB.Field timeoutField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"]; timeoutField.Value = timeout; ADODB.Field serverPortField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; serverPortField.Value = port; ADODB.Field sendUsingField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/sendusing"]; sendUsingField.Value = 2; ADODB.Field useSSLField = fieldCollection["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; useSSLField.Value = 1; fieldCollection.Update(); smtp.Configuration = smtpConfig; smtp.Subject = subject; smtp.From = fromEmailAddress; smtp.To = toEmailAddress; smtp.Sender = fromName; smtp.Organization = fromName; smtp.ReplyTo = fromEmailAddress; smtp.TextBody = messageBody; smtp.Send(); }
/****** View with Source Object ******/ private ADODB.Recordset ConvertToRecordset(DataTable inTable) { ADODB.Recordset result = new ADODB.Recordset(); result.CursorLocation = ADODB.CursorLocationEnum.adUseClient; ADODB.Fields resultFields = result.Fields; System.Data.DataColumnCollection inColumns = inTable.Columns; foreach (DataColumn inColumn in inColumns) { resultFields.Append(inColumn.ColumnName , TranslateType(inColumn.DataType) , inColumn.MaxLength , inColumn.AllowDBNull ? ADODB.FieldAttributeEnum.adFldIsNullable : ADODB.FieldAttributeEnum.adFldUnspecified , null); } result.Open(System.Reflection.Missing.Value , System.Reflection.Missing.Value , ADODB.CursorTypeEnum.adOpenStatic , ADODB.LockTypeEnum.adLockOptimistic, 0); foreach (DataRow dr in inTable.Rows) { result.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value); for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++) { resultFields[columnIndex].Value = dr[columnIndex]; } } return(result); }
private void LoadExtraDataForView() { try { if (this._array.Count > 0) { ADODB.Connection cnn = new ADODB.Connection(); ADODB.Recordset rs = new ADODB.Recordset(); ADOX.Catalog cat = new ADOX.Catalog(); // Open the Connection cnn.Open(dbRoot.ConnectionString, null, null, 0); cat.ActiveConnection = cnn; rs.Source = cat.Views[this.View.Name].Command; rs.Fields.Refresh(); ADODB.Fields flds = rs.Fields; Column col = this._array[0] as Column; f_TypeName = new DataColumn("TYPE_NAME", typeof(string)); col._row.Table.Columns.Add(f_TypeName); f_AutoKey = new DataColumn("AUTO_INCREMENT", typeof(Boolean)); col._row.Table.Columns.Add(f_AutoKey); Column c = null; ADODB.Field fld = null; int count = this._array.Count; for (int index = 0; index < count; index++) { fld = flds[index]; c = (Column)this[fld.Name]; c._row["TYPE_NAME"] = fld.Type.ToString(); c._row["AUTO_INCREMENT"] = false; } rs.Close(); cnn.Close(); } } catch {} }
/// <summary> /// 邮件通过465端口发送会不成功,微软的System.Net.Mail类只支持 “Explicit SSL”,目前通过CDO的方式来实现,需要在IIS上做对应配置 /// </summary> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> public static void SendBy(string to, string subject, string body) { CDO.Message oMsg = new CDO.Message(); CDO.IConfiguration iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; oField.Value = CDO.CdoSendUsing.cdoSendUsingPort; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; oField.Value = CDO.CdoProtocolsAuthentication.cdoBasic; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; oField.Value = "smtp.exmail.qq.com"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; oField.Value = 465; oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; oField.Value = "*****@*****.**"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; oField.Value = "ZKTD51robotjob"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; oField.Value = "true"; oMsg.TextBody = body; oMsg.Subject = subject; oMsg.From = "*****@*****.**"; oMsg.To = to; try { oMsg.Send(); } catch (Exception) { throw; } }
/// <summary> /// 发送 /// </summary> /// <param name="To">收件人</param> /// <param name="HTMLBody">邮件正文</param> public static void Send(string To, string HTMLBody) { //需要两个DLL //Interop.ADODB.dll //Interop.CDO.dll string From = "*****@*****.**"; string Subject = "欢迎进入终生领导艺术学院学习"; string UsersName = "*****@*****.**"; string Password = "******"; string SMTP = "mail.situational.com.cn"; try { CDO.Message Msg = new CDO.Message(); Msg.From = From; Msg.To = To; Msg.Subject = Subject; Msg.HTMLBody = HTMLBody; //附件 //Msg.AddAttachment(@"C:\Users\Administrator\Desktop\20081022(008).rar", "", ""); //Msg.HTMLBody = HttpContent("http://www.situational.com.cn/LearningInformation/Details61.shtml"); CDO.IConfiguration Config = Msg.Configuration; ADODB.Fields oFields = Config.Fields; oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = UsersName; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = Password; oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = SMTP; oFields.Update(); Msg.BodyPart.Charset = "gb2312"; Msg.HTMLBodyPart.Charset = "gb2312"; Msg.Send(); Msg = null; } catch { } }
private void EnviarCDO(Email email) { String msgSenha = Decrypt.Executar(_config.SmtpSenha); CDO.Message message = new CDO.Message(); CDO.IConfiguration configuration = message.Configuration; ADODB.Fields fields = configuration.Fields; ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; field.Value = _config.SmtpServer; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; field.Value = _config.Porta; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; field.Value = CDO.CdoSendUsing.cdoSendUsingPort; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; field.Value = CDO.CdoProtocolsAuthentication.cdoBasic; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; field.Value = _config.SmtpUser; field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; field.Value = msgSenha; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; field.Value = _config.EnableSsl ? "true" : "false"; fields.Update(); List <string> lstArquivos = CopyTempPath(email); lstArquivos.ForEach(x => message.AddAttachment(x)); message.From = _config.Remetente; message.To = email.Destinatario.Replace(';', ','); message.Subject = email.Assunto; message.HTMLBody = email.Texto; try { message.Send(); } finally { lstArquivos.ForEach(x => System.IO.File.Delete(x)); string path = GerarDirTemp(email.Id); } }
public static void Update_ADODB_from_ADO(DataTable inTable, ref ADODB.Recordset adoRs) { // ADODB.Recordset result = adoRs.Clone(ADODB.LockTypeEnum.adLockOptimistic); ADODB.Fields adoFields = adoRs.Fields; System.Data.DataColumnCollection inColumns = inTable.Columns; //Delete adoRs.MoveFirst(); while (!adoRs.EOF) { adoRs.Delete(); adoRs.MoveNext(); } //Add foreach (DataRow dr in inTable.Rows) { adoRs.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value); for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++) { adoFields[columnIndex].Value = dr[columnIndex]; } } }
static public bool Helper_SendEmail(string Tos, string Subject, string Message) { string From = "*****@*****.**"; //HARDCODED System.Net.Mail.SmtpClient vSmtpClient; vSmtpClient = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SMTP_SERVER"], Convert.ToInt32(ConfigurationManager.AppSettings["SMTP_PORT"])); vSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; vSmtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"], ConfigurationManager.AppSettings["SMTP_PASSWORD"]); vSmtpClient.EnableSsl = true; try { //System.Net.Mail.MailMessage vMessage; //vMessage = new System.Net.Mail.MailMessage(); //string[] tab_Tos = Tos.Split(new char[] {','}); //foreach(string To in tab_Tos) // if(!string.IsNullOrWhiteSpace(To)) // vMessage.To.Add(To); ////vMessage.To.Add("*****@*****.**"); //HARDCODED //vMessage.Bcc.Add("*****@*****.**"); //HARDCODED ////"*****@*****.**" //vMessage.From = new System.Net.Mail.MailAddress(From); //vMessage.Subject = Subject; //vMessage.IsBodyHtml = true; //vMessage.Body = Message; //vMessage.Priority = System.Net.Mail.MailPriority.Normal; //vSmtpClient.Send(vMessage); CDO.Message message = new CDO.Message(); CDO.IConfiguration configuration = message.Configuration; ADODB.Fields fields = configuration.Fields; ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; field.Value = ConfigurationManager.AppSettings["SMTP_SERVER"]; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; field.Value = 465; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; field.Value = CDO.CdoSendUsing.cdoSendUsingPort; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; field.Value = CDO.CdoProtocolsAuthentication.cdoBasic; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; field.Value = ConfigurationManager.AppSettings["SMTP_USERNAME"]; field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; field.Value = ConfigurationManager.AppSettings["SMTP_PASSWORD"]; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; field.Value = "true"; fields.Update(); Utils.Helper_Trace("Email Service", String.Format("Building CDO Message...")); message.From = From; string[] tab_Tos = Tos.Split(new char[] { ',' }); foreach (string To in tab_Tos) { if (!string.IsNullOrWhiteSpace(To)) { message.To = To; //TODO } } message.Subject = Subject; message.TextBody = Message; // Send message. message.Send(); } catch (Exception ex) { Utils.Helper_Trace("Email Service", "Error sending email to the administrator : " + ex.Message + " " + ex.InnerException); return(false); } Utils.Helper_Trace("XORCISM Email Service", "Email sent"); return(true); }
/// <summary> /// Sends mail using the Mail message object /// </summary> /// <param name="IsBodyHtml"></param> /// <returns></returns> public bool SendMail() { try { //use CDO to send if the hostingplace is "HP" if (ConfigUtil.HostingPlace.ToLower() == "hp") { CDO.Message oMsg; ////CDO.IBodyPart iBp; // Create a new message. oMsg = new CDO.Message(); //oMsg.From = "*****@*****.**"; //oMsg.To = "*****@*****.**"; CDO.IConfiguration iConfg; iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "mailhost.prod"; // oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "localhost"; oMsg.Configuration.Fields.Update(); oMsg.From = MailMessageObject.From.Address; oMsg.To = string.Join(",", new List <string>(from e in MailMessageObject.To select e.Address).ToArray()); oMsg.CC = string.Join(",", new List <string>(from e in MailMessageObject.CC select e.Address).ToArray()); oMsg.BCC = string.Join(",", new List <string>(from e in MailMessageObject.Bcc select e.Address).ToArray()); oMsg.Subject = MailMessageObject.Subject; //oMsg.TextBody = MailMessageObject.Body; oMsg.HTMLBody = MailMessageObject.Body; // Send mail. oMsg.Send(); oMsg = null; return(true); } else { System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(ConfigUtil.EmailServer); if (ConfigUtil.EmailServerRequiresAuthentication == true) { System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(ConfigUtil.EmailServerUserName, ConfigUtil.EmailServerPassword); smtp.UseDefaultCredentials = false; smtp.Credentials = basicAuthenticationInfo; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; } ApplyBusinessRules(); smtp.Send(this.MailMessageObject); return(true); } } catch (Exception ex) { //TODO : Logout return(false); } }
public static void SendBy(string to, string subject, string body) { CDO.Message oMsg = new CDO.Message(); CDO.IConfiguration iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; oField.Value = CDO.CdoSendUsing.cdoSendUsingPort; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; oField.Value = CDO.CdoProtocolsAuthentication.cdoBasic; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; oField.Value = "smtp.exmail.qq.com"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; oField.Value = 465; oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; oField.Value = "*****@*****.**"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; oField.Value = "ZKTD51robotjob"; oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; oField.Value = "true"; oMsg.TextBody = body; oMsg.Subject = subject; oMsg.From = "*****@*****.**"; oMsg.To = to; try { oMsg.Send(); } catch (Exception) { throw; } //MailMessage mailMessage = new MailMessage(); //mailMessage.To.Add("*****@*****.**"); //mailMessage.From = new MailAddress("*****@*****.**"); //mailMessage.Subject = subject; //mailMessage.Body = body; //mailMessage.IsBodyHtml = true; ////需要读取配置 //SmtpClient client = new SmtpClient("smtp.exmail.qq.com", 465) //{ // EnableSsl = true //}; //client.Credentials = new NetworkCredential("*****@*****.**", "ZKTD51robotjob"); ////ConfigurationManager.AppSettings["user"].ToString(), ////ConfigurationManager.AppSettings["password"].ToString()); //try //{ // client.Send(mailMessage); // //LoggerHelper.Writelog( // // string.Format("Email发送成功,发送给:{0},发送标题为:{1},发送内容为:{2}", to, subject, body), // // LogLevel.Info); //} //catch (Exception ex) //{ // //LoggerHelper.Writelog(ex.Message, LogLevel.Error); //} }
/// <summary> /// Sends mail using the Mail message object /// </summary> /// <param name="IsBodyHtml"></param> /// <returns></returns> public bool SendMailCDO() { try { //use CDO to send if the hostingplace is "HP" CDO.Message oMsg; ////CDO.IBodyPart iBp; // Create a new message. oMsg = new CDO.Message(); //oMsg.From = "*****@*****.**"; //oMsg.To = "*****@*****.**"; CDO.IConfiguration iConfg; iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = ConfigUtil.SmtpSendUsing; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = ConfigUtil.SmtpHost; oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = ConfigUtil.SmtpAuthenticate; oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = ConfigUtil.SmtpCredentialsUid; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = ConfigUtil.SmtpCredentialsPwd; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = ConfigUtil.SmtpPort; oFields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value = ConfigUtil.SmtpConnectionTimeout; oMsg.Configuration.Fields.Update(); oMsg.From = MailMessageObject.From.Address; oMsg.To = string.Join(",", new List <string>(from e in MailMessageObject.To select e.Address).ToArray()); oMsg.CC = string.Join(",", new List <string>(from e in MailMessageObject.CC select e.Address).ToArray()); oMsg.BCC = string.Join(",", new List <string>(from e in MailMessageObject.Bcc select e.Address).ToArray()); oMsg.Subject = MailMessageObject.Subject; //oMsg.TextBody = MailMessageObject.Body; oMsg.HTMLBody = MailMessageObject.Body; // Send mail. oMsg.Send(); oMsg = null; return(true); } catch (Exception ex) { //TODO : Logout return(false); } }
private void sendMail1(string mailTo, string strToName, string mailCC, string mailBCC, string strTitle, string strBody, string strFilesPath) { CDO.Message msg = new CDO.Message(); string passWord = "******"; string from = "*****@*****.**"; //string from = "hyitech\\caservice"; string server = "192.168.10.32";// "192.168.10.32"; //发件人 msg.From = from; //收件人 msg.To = mailTo; //抄送 if (mailCC.Trim().Length > 0) { msg.CC = mailCC; } //密送 if (mailBCC.Trim().Length > 0) { msg.BCC = mailBCC; } //标题 msg.Subject = strTitle; //正文 msg.TextBody = strBody; if (strFilesPath.Trim().Length > 0) { msg.AddAttachment(strFilesPath); } CDO.IConfiguration iConfig = msg.Configuration; ADODB.Fields fields = iConfig.Fields; //fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; //fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server; //fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25; //fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; ////fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = true; //fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "*****@*****.**"; //fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = passWord; fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "*****@*****.**"; //修改这里,并保持发件人与这里的地址一致 fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "*****@*****.**"; //修改这里 fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "caservice"; //修改这里 fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "123.com"; //修改这里 fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; //value=0 代表Anonymous验证方式(不需要验证) //value=1 代表Basic验证方式(使用basic (clear-text) authentication. //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.) //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express) fields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804; fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "192.168.10.32"; fields.Update(); try { msg.Send(); msg = null; Response.Write(strTitle + "发送完成<br />"); } catch (Exception ex) { WebLog.WriteLog(ex.Message + ex.InnerException + ex.HelpLink + ex.Data + ex.Source + ex.StackTrace + ex.TargetSite, "sendMail"); } }
/// <summary> /// Send Mail /// </summary> /// <returns></returns> public NotifyComResponse Send() { NotifyComResponse notifyComResponse = new NotifyComResponse(); try { try { MailAddress ma = new MailAddress(_fromAddress); } catch (FormatException ex) { // invalid from mail address, set it to [email protected] LogBook.Write("The format for the from email address (" + _fromAddress + ") is incorrect. [email protected] will be used instead."); _fromAddress = "*****@*****.**"; } SmtpClient ss = new SmtpClient(_smtpServer, _smtpPort); MailMessage mm = new MailMessage(_fromAddress, _toAddress, _subject, _body); CDO.Message message = new CDO.Message(); /*Create Mail Message Object*/ MailMessage mailObj = new MailMessage(); /*Email from address*/ mailObj.From = new MailAddress(_fromAddress, _fromName); /*Email to address*/ mailObj.To.Add(new MailAddress(_toAddress)); /*Email subject*/ mailObj.Subject = _subject; /*Email Body Encoding*/ mailObj.BodyEncoding = Encoding.Default; /*Email Body*/ mailObj.Body = _body; /*Body format (HTML/Text)*/ mailObj.IsBodyHtml = _isBodyHTML; /*Via SMTP Gateway (i.e. your local Exchange Server)-> SmtpSendMethod = 0*/ /*Via Direct Domain SMTP Connection w/DNS MX Lookup-> SmtpSendMethod = 1*/ /*When SmtpSendMethod = 1 we are sending via local host instead of using SMTP settings*/ SmtpClient smtpClientObj = null; if (_sendMethod == 1) { //Send message string domain = mailObj.To[0].Address.Substring(mailObj.To[0].Address.IndexOf('@') + 1); //To Do :need to check for MX record existence before you send. Left intentionally for you. string mxRecord = SendSMTP.DnsLookUp.GetMXRecords(domain)[0]; smtpClientObj = new SmtpClient(mxRecord); } else { if (_isTLS == true && _isSSL == false) { ss.EnableSsl = true; ss.Timeout = 20000; ss.DeliveryMethod = SmtpDeliveryMethod.Network; ss.UseDefaultCredentials = false; ss.Credentials = new NetworkCredential(_smtpAuthUserName, _smtpAuthPassword); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mm.IsBodyHtml = _isBodyHTML; } else { CDO.IConfiguration configuration = message.Configuration; ADODB.Fields fields = configuration.Fields; ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; field.Value = _smtpServer; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; field.Value = _smtpPort; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; field.Value = CDO.CdoSendUsing.cdoSendUsingPort; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; if (_smtpAuthMethod == "" || _smtpAuthMethod.ToUpper() == "NONE") { field.Value = CDO.CdoProtocolsAuthentication.cdoAnonymous; } else if (_smtpAuthMethod.ToUpper() == "NTLM") { field.Value = CDO.CdoProtocolsAuthentication.cdoNTLM; } else { field.Value = CDO.CdoProtocolsAuthentication.cdoBasic; } field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; field.Value = _smtpAuthUserName; field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; field.Value = _smtpAuthPassword; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; field.Value = _isSSL; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"]; field.Value = 10; fields.Update(); message.From = @"""" + _fromName + @""" <" + _fromAddress + ">";; message.To = _toAddress; message.Subject = _subject; if (_isBodyHTML) { message.HTMLBody = _body; } else { message.TextBody = _body; } } } try { if (_sendMethod == 1) { smtpClientObj.Send(mailObj); } else { /*Send Mail*/ if (_isTLS == true && _isSSL == false) { ss.Send(mm); } else { message.Send(); } } /*Record notify response*/ notifyComResponse.IsError = false; notifyComResponse.IsSucceeded = true; notifyComResponse.ResponseContent = "Email sent to: " + "[" + _emailToName + "]" + _toAddress + ((_isAlphaPager) ? " (ALPHA PAGER) " : ""); } catch (Exception ex) { /*Record notify response*/ notifyComResponse.IsError = true; notifyComResponse.IsSucceeded = false; notifyComResponse.ResponseContent = "Email to: " + "[" + _emailToName + "]" + _toAddress + ((_isAlphaPager) ? " (ALPHA PAGER) " : "") + " Failed " + ex.Message; /*Debug Object values for reference*/ LogBook.Debug(notifyComResponse, this); /*Write exception log*/ LogBook.Write("Error has occurred while sending email to ." + _emailToName, ex, "CooperAtkins.NotificationServer.NotifyEngine.Email.EmailClient"); } finally { // Added on 2/19/2012 // Srinivas Rao Eranti // Added try catch to release the message object try { Marshal.FinalReleaseComObject(message); // GC.SuppressFinalize(message); } catch { } } } catch (Exception ex) { /*Record notify response*/ notifyComResponse.IsError = true; notifyComResponse.IsSucceeded = false; notifyComResponse.ResponseContent = "Email to: " + "[" + _emailToName + "]" + _toAddress + ((_isAlphaPager) ? "(ALPHA PAGER)" : "") + " Failed " + ex.Message; /*Debug Object values for reference*/ LogBook.Debug(notifyComResponse, this); /*Write exception log*/ LogBook.Write("Error has occurred while preparing SMTP setting.", ex, "CooperAtkins.NotificationServer.NotifyEngine.Email.EmailClient"); } return(notifyComResponse); }
/// <summary> /// Send an electronic message using the Collaboration Data Objects (CDO). /// </summary> /// <remarks>http://support.microsoft.com/kb/310212</remarks> private static bool SendUsingYahoo(MailMessage m, string userName, string password) { try { CDO.Message message = new CDO.Message(); CDO.IConfiguration configuration = message.Configuration; ADODB.Fields fields = configuration.Fields; // Set configuration. // sendusing: cdoSendUsingPort, value 2, for sending the message using the network. // smtpauthenticate: Specifies the mechanism used when authenticating to an SMTP service over the network. // Possible values are: // - cdoAnonymous, value 0. Do not authenticate. // - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields) // - cdoNTLM, value 2. The current process security context is used to authenticate with the service. ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; field.Value = "smtp.mail.yahoo.com"; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; field.Value = 465; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; field.Value = CDO.CdoSendUsing.cdoSendUsingPort; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; field.Value = CDO.CdoProtocolsAuthentication.cdoBasic; field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; field.Value = userName; field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; field.Value = password; field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; field.Value = "true"; fields.Update(); message.From = userName; message.To = string.Join(",", m.To.Select(a => a.Address)); message.Subject = m.Subject; if (m.IsBodyHtml) { message.HTMLBody = m.Body; } else { message.TextBody = m.Body; } message.Send(); if (Log.IsDebugEnabled) { Log.Debug("Email sent successfully."); } return(true); } catch (Exception ex) { Log.ErrorFormat("Error sending email to {0}.", string.Join(",", m.To.Select(a => a.Address))); Log.Error(ex.Message, ex); } return(false); }
public void CopyTable(ADOX.Table tblAccess) { ADODB.Recordset recMaster = new ADODB.Recordset(); ADODB.Recordset recLoop = new ADODB.Recordset(); int intLoop = 0; string strInfile = ""; string strSQL = "SELECT "; string strRecord; string strLoadFilePath = strSourceDbPath.Replace("\\", "\\\\"); string strFileName = strTempPath + tblAccess.Name + ".txt"; StreamWriter sw = new StreamWriter(strFileName, false); //create the infile strInfile += "LOAD DATA LOCAL INFILE '" + strFileName + "' INTO TABLE " + strMySQLDBName + "." + tblAccess.Name + " "; strInfile += "FIELDS TERMINATED BY ',' "; strInfile += "ESCAPED BY '\\\\' "; strInfile += "LINES TERMINATED BY 0x0d0a "; strInfile += "("; //loop through fields to enumerate them for the infile and build a select statement for (intLoop = 0; intLoop < tblAccess.Columns.Count; intLoop++) { strInfile += MySQLName((tblAccess.Columns[intLoop].Name)); switch (tblAccess.Columns[intLoop].Type) { case ADOX.DataTypeEnum.adDate: //convert to MySQL datetime format strSQL += "FORMAT([" + tblAccess.Columns[intLoop].Name + "], 'YYYY-MM-DD HH:MM:SS') as " + tblAccess.Columns[intLoop].Name; break; default: strSQL += "[" + tblAccess.Columns[intLoop].Name + "]"; break; } if (intLoop < tblAccess.Columns.Count - 1) { strSQL += ","; strInfile += ", "; } } strInfile += ");"; strSQL += " FROM [" + tblAccess.Name + "]"; //open the "Master" recordset recMaster.CursorLocation = ADODB.CursorLocationEnum.adUseClient; recMaster.Open(strSQL, conJCMS_db, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0); //create the "Loop" recordset, this is a clone of the master, with the exception //that the definedsize for text fields is lengthened. This is because the added //escape characters could potentially exceed the field length in the master recordset recLoop.CursorLocation = ADODB.CursorLocationEnum.adUseClient; ADODB.Fields fdsLoop = recLoop.Fields; ADODB.Fields fdsMaster = recMaster.Fields; foreach (ADODB.Field fldIn in fdsMaster) { if (fldIn.Type.ToString().IndexOf("Char") > 0) { fdsLoop.Append(fldIn.Name, fldIn.Type, fldIn.DefinedSize + 30, ADODB.FieldAttributeEnum.adFldIsNullable, null); } else { fdsLoop.Append(fldIn.Name, fldIn.Type, fldIn.DefinedSize, ADODB.FieldAttributeEnum.adFldIsNullable, null); } } recLoop.Open(System.Reflection.Missing.Value, System.Reflection.Missing.Value, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0); recLoop.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value); while (!recMaster.EOF) { for (int columnIndex = 0; columnIndex < recMaster.Fields.Count; columnIndex++) { recLoop.Fields[columnIndex].Value = recMaster.Fields[columnIndex].Value; if (recLoop.Fields[columnIndex].Value.ToString().Length > 0) { if ((recLoop.Fields[columnIndex].Value.ToString().IndexOf("\\", 0) + 1) > 0) { recLoop.Fields[columnIndex].Value = recLoop.Fields[columnIndex].Value.ToString().Replace("\\", "\\\\"); } if ((recLoop.Fields[columnIndex].Value.ToString().IndexOf(",", 0) + 1) > 0) { recLoop.Fields[columnIndex].Value = recLoop.Fields[columnIndex].Value.ToString().Replace(",", "\\,"); } if ((recLoop.Fields[columnIndex].Value.ToString().IndexOf(System.Environment.NewLine, 0) + 1) > 0) { recLoop.Fields[columnIndex].Value = recLoop.Fields[columnIndex].Value.ToString().Replace(System.Environment.NewLine, " "); } } } strRecord = recLoop.GetString(ADODB.StringFormatEnum.adClipString, 1, ",", System.Environment.NewLine, "\\N"); recLoop.MovePrevious(); sw.Write(strRecord); recMaster.MoveNext(); } recMaster.Close(); recMaster.ActiveConnection = null; try { recLoop.Close(); } catch { } sw.Close(); ExecuteSQL(strInfile); File.Delete(strFileName); recLoop = null; }