电信的IP过一段时间刷新一下, 所以我写了个类, 用来发送公网IP的邮件到自己邮箱, gmail, 163的都可以使用StmpClient, QQ好像不行, 在网上找了找, 说是 QQ 的Smtp 不能适应 StmpClient


public class SendEmailHelper 

 

      { 

 

          private static readonly ILog log = LogManager.GetLogger(typeof(SendEmailHelper)); 

 

          public static void SendEmail() 

 

          { 

 

              try 

 

              { 

 

                  //MailMessage MMsg = new MailMessage(); 

 

                  //MMsg.Subject = "公网IP"; 

 

                  //MMsg.From = new MailAddress("toddzhuang@gmail.com", "Todd Zhuang"); 

 

                  //MMsg.To.Add(new MailAddress("54733648@qq.com")); 

 
 
 
 

                  //MMsg.IsBodyHtml = true; 

 

                  //MMsg.BodyEncoding = System.Text.Encoding.Default; 

 

                  //MMsg.Body = GetWebContent(@"http://www.net.cn/static/customercare/yourIP.asp", lb); 

 
 
 
 

                  //SmtpClient SClient = new SmtpClient(); 

 

                  //SClient.Host = "smtp.gmail.com";//google的smtp地址 

 

                  //SClient.Port = 587;//google的smtp端口 

 

                  //SClient.EnableSsl = true;//因为google使用了SSL(安全套接字层)加密链接所以这里的EnableSsl必须设置为true。 

 

                  //SClient.Credentials = new NetworkCredential("toddzhuang@gmail.com", "your password"); 

 
 
 
 
 
 
 

                  //SClient.Send(MMsg); 

 

                  MailMessage MMsg = new MailMessage(); 

 

                  MMsg.Subject = "公网IP"; 

 

                  MMsg.From = new MailAddress("toddzhuang@163.com"); 

 

                  MMsg.To.Add(new MailAddress("54733648@qq.com")); 

 
 
 
 

                  MMsg.IsBodyHtml = true; 

 

                  MMsg.BodyEncoding = System.Text.Encoding.Default; 

 

                  MMsg.Body = GetWebContent(@"http://www.net.cn/static/customercare/yourIP.asp"); 

 
 
 
 

                  SmtpClient SClient = new SmtpClient(); 

 

                  SClient.Host = "smtp.163.com"; 

 

                  SClient.Credentials = new NetworkCredential("toddzhuang@163.com", "your password"); 

 
 
 
 

                  SClient.Send(MMsg); 

 

                  log.Info("邮件发送成功"); 

 

              } 

 

              catch(Exception e) 

 

              { 

 

                  log.Error(e.Message); 

 

              } 

 

          } 

 
 
 
 

          private static string GetWebContent(string Url) 

 

          { 

 

              string strResult = ""; 

 

              try 

 

              { 

 

                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 

 

                  //声明一个HttpWebRequest请求 

 

                  request.Timeout = 30000; 

 

                  //设置连接超时时间 

 

                  request.Headers.Set("Pragma", "no-cache"); 

 

                  HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

 

                  Stream streamReceive = response.GetResponseStream(); 

 

                  Encoding encoding = Encoding.GetEncoding("GB2312"); 

 

                  StreamReader streamReader = new StreamReader(streamReceive, encoding); 

 

                  strResult = streamReader.ReadToEnd(); 

 

                  log.Info("请求网页成功返回"); 

 

              } 

 

              catch (Exception e) 

 

              { 

 

                  log.Error(e.Message); 

 

              } 

 

              return strResult; 

 

          } 

 
 
 
 

      }