C# 取电信公网IP并发送邮件
2010-05-25 13:38
260 查看
电信的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; }
}
//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; }
}
相关文章推荐
- C# 取电信公网IP并发送邮件
- C#使用 System.Net.Mail发送邮件功能
- C#通过SMTP发送邮件
- C#使用CDO发送邮件
- C#发送邮件进度条问题
- c#发送邮件(情人节凌晨测试的第一个小模块)
- c# 发送邮件
- .Net锦囊-C#,.Net发送邮件三种方法(Localhost,SMTP,SSL-SMTP)
- C#实现邮件发送功能
- C#发送邮件
- C#发送Email邮件三种方法的总结
- 通过C#发送自定义的html格式邮件
- C# 发送邮件 参考代码
- C# 使用网易邮箱发送邮件
- C#邮件发送 - SendMail
- c#发送邮件
- c#邮件带附件发送成功后附件文件不能及时删除
- C#发送Email邮件三种方法的总结
- C# winform 通过调用outlook发送邮件