Exemplo n.º 1
0
        public static void SendEmail(string email, string subject, string body)
        {
            try
            {
                var unity         = new UnityContext();
                var configService = unity.GetInstance <IConfigService>();


                var cfg = configService.GetConfig(1);

                var smtp = new SmtpClient();

                smtp.Host = cfg.GetValue("host");
                smtp.Port = cfg.GetValue("port").TryParseToInt32();
                smtp.UseDefaultCredentials = false;
                smtp.EnableSsl             = "ssl".Equals(cfg.GetValue("secure")) ? true : false;

                System.Net.NetworkCredential nc = new System.Net.NetworkCredential(cfg.GetValue("username"), cfg.GetValue("password"));
                smtp.Credentials = nc.GetCredential(smtp.Host, smtp.Port, "NTLM");

                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                smtp.Send(cfg.GetValue("email"), email, subject, body);
            }
            catch (Exception ex)
            {
                Logger.GetLogger().Error("发送邮件出错\n", ex);
            }
        }
Exemplo n.º 2
0
        protected System.Net.NetworkCredential GetCredentials()
        {
            System.Net.NetworkCredential oPort = null;

            // Cada servicio Web XML necesita un espacio de nombres único para que las aplicaciones de cliente puedan distinguir este servicio de otros
            // servicios del Web. http://tempuri.org/ está disponible para servicios Web XML que están en desarrollo, pero los servicios Web XML publicados deberían
            // utilizar un espacio de nombres más permanente.
            Uri uri = new Uri("http://tempuri.org/"); //

            string userWS, passWS, domainWS;

            userWS   = _config[$"{BASE_WS_CONFIG}:UserWS"];
            passWS   = _config[$"{BASE_WS_CONFIG}:PassWS"];
            domainWS = _config[$"{BASE_WS_CONFIG}:DomainWS"];

            if (sw_validation_encrypted)
            {
                userWS   = Sec.DesencriptarCadenaDeCaracteres(userWS);
                passWS   = Sec.DesencriptarCadenaDeCaracteres(passWS);
                domainWS = Sec.DesencriptarCadenaDeCaracteres(domainWS);
            }
            // TODO encriptar credenciales en Web.Config
            System.Net.ICredentials credentials = new System.Net.NetworkCredential(userWS, passWS, domainWS);

            oPort = credentials.GetCredential(uri, "Windows");

            return(oPort);
        }
Exemplo n.º 3
0
        ///<summary>
        /// 邮件的发送
        ///</summary>
        public void Send()
        {
            try
            {
                if (mMailMessage != null)
                {
                    mSmtpClient = new SmtpClient
                    {
                        Host = mSenderServerHost,
                        Port = mSenderPort,
                        UseDefaultCredentials = false,
                        EnableSsl             = mEnableSsl
                    };

                    if (mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mSenderUsername,
                                                                                           mSenderPassword);

                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(mSenderUsername, mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 4
0
        public void webRequest()
        {
            System.Net.ServicePointManager.Expect100Continue = false;

            RequestEvent requestEvent = new RequestEvent();

            string message = requestEvent.createOadrRequestEvent("TH_VEN");

            System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_url + "/EiEvent");

            wr.Method = "POST";
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential("admin", "password");

            wr.ContentType = "application/xml";
            Stream s = wr.GetRequestStream();

            s.Write(Encoding.UTF8.GetBytes(message), 0, message.Length);

            wr.Credentials = nc.GetCredential(wr.RequestUri, "Basic");

            System.Net.WebResponse response = wr.GetResponse();

            Stream stream = response.GetResponseStream();

            StreamReader streamReader = new StreamReader(stream);

            string responseBody = streamReader.ReadToEnd();

            System.Console.WriteLine(responseBody);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 邮件的发送
 /// </summary>
 /// <returns>发送成功返回true,否则返回false</returns>
 public bool Send()
 {
     try
     {
         if (mMailMessage != null)
         {
             mSmtpClient = new SmtpClient();
             //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl             = this.mEnableSsl;
             if (!this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 6
0
 ///<summary>
 /// 邮件的发送
 ///</summary>
 public void Send()
 {
     try
     {
         if (mMailMessage != null)
         {
             mSmtpClient      = new SmtpClient();
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl             = this.mEnableSsl;
             if (this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //NTLM: Secure Password Authentication in Microsoft Outlook Express
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
         }
     }
     catch (Exception ex)
     {
         LoggerUtils.LogIn(LoggerUtils.ColectExceptionMessage(ex, "At function:Send() .OpporunMail 124 rows"), LogType.ErrorLog);
     }
 }
Exemplo n.º 7
0
 ///<summary>
 /// 邮件的发送
 ///</summary>
 public void Send()
 {
     try
     {
         if (mMailMessage != null)
         {
             mSmtpClient = new SmtpClient();
             //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl             = this.mEnableSsl;
             if (this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //NTLM: Secure Password Authentication in Microsoft Outlook Express
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            // create a service object
            RallyServiceService service = new RallyServiceService();

            service.Url = "https://rally1.rallydev.com/slm/webservice/1.42/RallyService";


            // login to service using HTTP Basic auth
            System.Net.NetworkCredential crediential =
                new System.Net.NetworkCredential("*****@*****.**", "secret");


            Uri uri = new Uri(service.Url);

            System.Net.ICredentials credentials = crediential.GetCredential(uri, "Basic");
            service.Credentials     = credentials;
            service.PreAuthenticate = true;

            // Configure the service to maintain an HTTP session cookie
            service.CookieContainer = new System.Net.CookieContainer();

            Workspace workspace = null;


            // Make the web service call


            //---------------------------
            Defect defect = new Defect();

            service.create(defect);

            // Name is required
            defect.Name        = "bad defect";
            defect.Description = "trouble";



            //Create defect on the server
            CreateResult createResult = service.create(defect);

            if (hasErrors(createResult))
            {
                // something went wrong
                Console.WriteLine("Could not create defect result:");
                printWarningsErrors(createResult);
            }
            else
            {
                // look at the object returned from create()
                defect = (Defect)createResult.Object;
                Console.WriteLine("Created defect, ref = " + defect.@ref);
            }
        }
        public static bool Send(List <string> recivers, List <string> ccs, string subject, string emailBody)
        {
            try
            {
                string toMail = string.Join(",", recivers);
                string cc     = "";
                if (ccs.Count > 0)
                {
                    cc = string.Join(",", ccs);
                }
                mMailMessage = new MailMessage();
                mMailMessage.To.Add(toMail);
                if (!string.IsNullOrWhiteSpace(cc))
                {
                    mMailMessage.CC.Add(cc);
                }
                mMailMessage.From         = new MailAddress(fromMail);
                mMailMessage.Subject      = subject;
                mMailMessage.Body         = emailBody;
                mMailMessage.IsBodyHtml   = true;
                mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mMailMessage.Priority     = MailPriority.Normal;
                if (mMailMessage != null)
                {
                    mSmtpClient                       = new SmtpClient();
                    mSmtpClient.EnableSsl             = mEnableSsl;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.DeliveryMethod        = System.Net.Mail.SmtpDeliveryMethod.Network; //指定邮件发送方式
                                                                                                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = mSenderServerHost;
                    mSmtpClient.Port = mSenderPort;


                    if (mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mSenderUsername, mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(mSenderUsername, mSenderPassword);
                    }
                    mSmtpClient.Send(mMailMessage);
                }
            }
            catch (InvalidCastException e)
            {
                return(false);
            }
            return(true);
        }
        static void Main(string[] args)
        {
            // create a service object
            RallyServiceService service = new RallyServiceService();

            service.Url = "https://rally1.rallydev.com/slm/webservice/1.42/RallyService";

            // login to service using HTTP Basic auth
            System.Net.NetworkCredential crediential =
               new System.Net.NetworkCredential("*****@*****.**", "secret");

            Uri uri = new Uri(service.Url);
            System.Net.ICredentials credentials = crediential.GetCredential(uri, "Basic");
            service.Credentials = credentials;
            service.PreAuthenticate = true;

            // Configure the service to maintain an HTTP session cookie
            service.CookieContainer = new System.Net.CookieContainer();

            Workspace workspace = null;

            // Make the web service call

            //---------------------------
            Defect defect = new Defect();
            service.create(defect);

            // Name is required
            defect.Name = "bad defect";
            defect.Description = "trouble";

            //Create defect on the server
            CreateResult createResult = service.create(defect);
            if (hasErrors(createResult))
            {
                // something went wrong
                Console.WriteLine("Could not create defect result:");
                printWarningsErrors(createResult);
            }
            else
            {
                // look at the object returned from create()
                defect = (Defect)createResult.Object;
                Console.WriteLine("Created defect, ref = " + defect.@ref);
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// 发送邮件
 /// </summary>
 /// <param name="to">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
 /// <param name="subject">邮件标题</param>
 /// <param name="body">邮件内容</param>
 /// <param name="attachmentsPath">附件的路径集合,以分号分隔</param>
 public void SendMail(string to, string subject, string body, string attachmentsPath = "")
 {
     if (email == null)
     {
         _logger.Error("配置出错,请检查配置内容!文件路径:" + filePath);
         return;
     }
     try
     {
         mailMessage = new MailMessage();
         mailMessage.To.Add(to);
         mailMessage.From         = new MailAddress(email.Form);
         mailMessage.Subject      = subject;
         mailMessage.Body         = body;
         mailMessage.IsBodyHtml   = true;
         mailMessage.BodyEncoding = Encoding.UTF8;
         mailMessage.Priority     = MailPriority.Normal;
         if (!string.IsNullOrEmpty(attachmentsPath))
         {
             AddAttachments(attachmentsPath);//添加附件
         }
         smtpClient                       = new SmtpClient();
         smtpClient.Host                  = email.Host;
         smtpClient.Port                  = email.Port;
         smtpClient.EnableSsl             = email.EnableSsl;
         smtpClient.UseDefaultCredentials = false;
         if (email.EnablePwdAuthentication)
         {
             System.Net.NetworkCredential nc = new System.Net.NetworkCredential(email.UserName, email.Password);
             //NTLM: Secure Password Authentication in Microsoft Outlook Express
             smtpClient.Credentials = nc.GetCredential(smtpClient.Host, smtpClient.Port, "NTLM");
         }
         else
         {
             smtpClient.Credentials = new System.Net.NetworkCredential(email.UserName, email.Password);
         }
         smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
         smtpClient.Send(mailMessage);
     }
     catch (Exception ex)
     {
         _logger.Debug("发送邮件时发生错误!", ex);
     }
 }
Exemplo n.º 12
0
        ///<summary>
        /// 邮件的发送
        ///</summary>
        public void Send()
        {
            try
            {
                if (mMailMessage != null)
                {
                    mSmtpClient = new SmtpClient();
                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = this.mSenderServerHost;
                    mSmtpClient.Port = this.mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl             = this.mEnableSsl;
                    if (this.mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
#if !NET_3_5
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
#else
                        //mSmtpClient.Credentials = nc.GetCredential(new Uri(mSmtpClient.Host + ":" + mSmtpClient.Port), "NTLM");
#endif
                    }
                    else
                    {
#if !NET_3_5
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
#else
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
#endif
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 13
0
 ///<summary>
 /// 邮件的发送
 ///</summary>
 public bool Send(string toMail, string emailBody)
 {
     try
     {
         mMailMessage.To.Add(toMail);
         mMailMessage.Subject = "月下之火的验证码";
         mMailMessage.Body    = "你的验证码为:" + emailBody;
         if (mMailMessage != null)
         {
             mSmtpClient = new SmtpClient();
             //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl             = this.mEnableSsl;
             if (this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //NTLM: Secure Password Authentication in Microsoft Outlook Express
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
         }
         return(true);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
         return(false);
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// 邮件的发送
        /// </summary>
        /// <returns>true:发送成功</returns>
        public bool Send()
        {
            if (!GetMailSetting.IsEnable)
            {
                return(false);
            }
            bool flag = false;

            try
            {
                if (mMailMessage != null)
                {
                    mSmtpClient      = new SmtpClient();
                    mSmtpClient.Host = this.mSenderServerHost;
                    mSmtpClient.Port = this.mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl             = this.mEnableSsl;
                    if (this.mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.ToString());
            }
            return(flag);
        }
Exemplo n.º 15
0
 ///<summary>
 /// 邮件的发送
 ///</summary>
 public void Send()
 {
     try
     {
         if (mMailMessage != null)
         {
             mSmtpClient = new SmtpClient();
             //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl = this.mEnableSsl;
             if (this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 //NTLM: Secure Password Authentication in Microsoft Outlook Express
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
Exemplo n.º 16
0
        ///<summary>
        /// 添加附件
        ///</summary>
        ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
        //public void AddAttachments(string attachmentsPath)
        //{
        //    string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
        //    Attachment data;
        //    ContentDisposition disposition;
        //    for (int i = 0; i < path.Length; i++)
        //    {
        //        data = new Attachment(path[i], MediaTypeNames.Application.Octet);
        //        disposition = data.ContentDisposition;
        //        disposition.CreationDate = File.GetCreationTime(path[i]);
        //        disposition.ModificationDate = File.GetLastWriteTime(path[i]);
        //        disposition.ReadDate = File.GetLastAccessTime(path[i]);
        //        mMailMessage.Attachments.Add(data);
        //    }
        //}
        ///<summary>
        /// 邮件的发送
        ///</summary>
        public SMSResponse Send(MailMessage mailMessage)
        {
            SMSResponse response = new SMSResponse() { ResponseData = "邮件参数设置错误", statusCode = "201" };

            try
            {
                if (mailMessage != null)
                {
                    mSmtpClient = new SmtpClient();
                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = this.mSenderServerHost;
                    mSmtpClient.Port = this.mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl = this.mEnableSsl;
                    if (this.mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mailMessage);
                    // send successfully.
                    response.statusCode = "200";
                }
            }
            catch (Exception e)
            {
                // send failed.
                response.statusCode = "202";
                response.InnerException = e;
                response.ResponseData = "邮件发送失败";
            }

            return response;
        }
Exemplo n.º 17
0
 /// <summary>  
 /// 邮件的发送  
 /// </summary>  
 /// <returns>true:发送成功</returns>  
 public bool Send()
 {
     if (!GetMailSetting.IsEnable)
     {
         return false;
     }
     bool flag = false;
     try
     {
         if (mMailMessage != null)
         {
             mSmtpClient = new SmtpClient();
             mSmtpClient.Host = this.mSenderServerHost;
             mSmtpClient.Port = this.mSenderPort;
             mSmtpClient.UseDefaultCredentials = false;
             mSmtpClient.EnableSsl = this.mEnableSsl;
             if (this.mEnablePwdAuthentication)
             {
                 System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                 mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
             }
             else
             {
                 mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
             }
             mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
             mSmtpClient.Send(mMailMessage);
             flag = true;
         }
     }
     catch (Exception ex)
     {
         //TODO:异常处理
     }
     return flag;
 }
Exemplo n.º 18
0
        /*
         * MailRelated mailr = new MailRelated();
         * //smtp.163.com "123.125.50.133";
         * string senderServerIp = "smtp.163.com";
         * //smtp.gmail.com
         * //string senderServerIp = "74.125.127.109";
         * //smtp.qq.com
         * //string senderServerIp = "58.251.149.147";
         * //string senderServerIp = "smtp.sina.com";
         * string toMailAddress = "*****@*****.**";
         * string fromMailAddress = "*****@*****.**";
         * string subjectInfo = "Test sending e_mail";
         * string bodyInfo = "Hello Shawn, This is my first testing e_mail";
         * string mailUsername = "******";
         * string mailPassword = "******"; //发送邮箱的密码()
         * string mailPort = "25";
         * string attachPath = @"\\Mac\Home\Desktop\Seattle.png";
         * bool sended=mailr.SendMail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort,attachPath, false, false);
         * Console.WriteLine(sended);
         */
        #endregion how to use
        ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="server">发件箱的邮件服务器地址</param>
        ///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
        ///<param name="fromMail">发件人地址</param>
        ///<param name="subject">邮件标题</param>
        ///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
        ///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:[email protected],用户名为:hello)</param>
        ///<param name="password">发件人邮箱密码</param>
        ///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
        ///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
        ///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
        public bool SendMail(string SenderServer, string toMail, string CC, string BCC, string fromMail, string subject, string emailBody, string username, string password, string port, string attachmentsPath, bool sslEnable, bool pwdCheckEnable)
        {
            try
            {
                #region createmail
                MailMessage mMailMessage;             //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
                SmtpClient  mSmtpClient;              //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
                int         mSenderPort;              //发送邮件所用的端口号(htmp协议默认为25)
                string      mSenderServerHost;        //发件箱的邮件服务器地址(IP形式或字符串形式均可)
                string      mSenderPassword;          //发件箱的密码或授权码
                string      mSenderUsername;          //发件箱的用户名(即@符号前面的字符串,例如:[email protected],用户名为:hello)
                bool        mEnableSsl;               //是否对邮件内容进行socket层加密传输
                bool        mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证
                mMailMessage = new MailMessage();
                mMailMessage.To.Add(toMail);
                mMailMessage.CC.Add(CC);
                mMailMessage.Bcc.Add(BCC);
                mMailMessage.From         = new MailAddress(fromMail);
                mMailMessage.Subject      = subject;
                mMailMessage.Body         = emailBody;
                mMailMessage.IsBodyHtml   = true;
                mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mMailMessage.Priority     = MailPriority.Normal;
                mSenderServerHost         = SenderServer;
                mSenderUsername           = username;
                mSenderPassword           = password;
                mSenderPort = Convert.ToInt32(port);
                mEnableSsl  = sslEnable;
                mEnablePwdAuthentication = pwdCheckEnable;
                #endregion createmail

                #region attachments
                if (attachmentsPath == "")
                {
                }
                else
                {
                    string[]           path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
                    Attachment         data;
                    ContentDisposition disposition;
                    for (int i = 0; i < path.Length; i++)
                    {
                        data        = new Attachment(path[i], MediaTypeNames.Application.Octet);
                        disposition = data.ContentDisposition;
                        disposition.CreationDate     = File.GetCreationTime(path[i]);
                        disposition.ModificationDate = File.GetLastWriteTime(path[i]);
                        disposition.ReadDate         = File.GetLastAccessTime(path[i]);
                        mMailMessage.Attachments.Add(data);
                    }
                }
                #endregion attachments

                #region sendmail
                if (mMailMessage != null)
                {
                    mSmtpClient = new SmtpClient();
                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = mSenderServerHost;
                    mSmtpClient.Port = mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl             = mEnableSsl;
                    if (mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mSenderUsername, mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(mSenderUsername, mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                }

                #endregion sendmail
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
        }