自动发邮件的一段代码

  public static void SendMailAsSMTP(string strMailFromAddr, string strMailFromName, string strTo, string strSubject, string strBody)
        {

            if (strTo.Trim() == "")
                return;

            System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
            //myMail.From = new System.Net.Mail.MailAddress("notifications@i-meet.com", "i-Meet Notifications");
            myMail.From = new System.Net.Mail.MailAddress(strMailFromAddr, strMailFromName);

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("localhost");

            client.UseDefaultCredentials = true;
            client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

            try
            {
                if (System.Web.HttpContext.Current.Request.ServerVariables[33].ToString().Contains("127.0.0.1") || System.Web.HttpContext.Current.Request.ServerVariables[33].ToString().Contains("192.168.1.148") || System.Web.HttpContext.Current.Request.ServerVariables[33].ToString().Contains("local"))
                {
                    //myMail.From = new System.Net.Mail.MailAddress("qdog@wholuck.com", "xlj@klgz.net");
                    //client.UseDefaultCredentials = false;
                    client = new System.Net.Mail.SmtpClient("192.168.1.148");
                    //client.Credentials = new System.Net.NetworkCredential("xlj@klgz.net", "123456");
                }
            }
            catch (Exception ex)
            {

                //throw;
            }



            //myMail.From = new System.Net.Mail.MailAddress("smtp@1983111.com", "hmx");
            bool isHTML = true;
            try
            {
                if (strTo.Contains(","))
                {
                    string[] Email_s = strTo.Split(',');
                    foreach (string Mail in Email_s)
                    {
                        myMail.To.Add(Mail);
                    }                    
                }
                else
                    myMail.To.Add(strTo);
                
                myMail.Subject = strSubject;
                //myMail.SubjectEncoding = System.Text.Encoding.UTF8;
                myMail.Body = strBody;
                //myMail.BodyEncoding = System.Text.Encoding.UTF8;
                myMail.IsBodyHtml = isHTML;
                //myMail.Priority = System.Net.Mail.MailPriority.High;
            }
            catch (Exception ex)
            {

                //throw;
            }

            if (strMailFromAddr=="notifications@i-meet.com")
            {
                myMail.Body += GetTemplate("NotSpam");
            }
            
            //client.Credentials = new System.Net.NetworkCredential("smtp@1983111.com", "123456");

            //client.EnableSsl = true;
            try
            {
                client.Send(myMail);
            }
            catch (Exception ex)
            {
                //System.Web.HttpContext.Current.Response.Write(ex.Message);
                //System.Web.HttpContext.Current.Response.Write("<br>" + ex.InnerException .Message);
                //System.Web.HttpContext.Current.Response.End();
                //throw new Exception(ex.Message);
                //throw new Exception(ex.Message);
                //Log.Log.Message(ex.Message);
                //System.Web.HttpContext.Current.Response.Write("Send mail error:" + ex.InnerException.Message + "<br>" + ex.Message);
            }        
        }
posted on 2008-10-30 11:33  prayforsmile  阅读(341)  评论(0编辑  收藏  举报