/// <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(); }
/// <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); } }
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); }
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); }