zoukankan      html  css  js  c++  java
  • C# 取电信公网IP并发送邮件

    电信的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;
            }

        }
  • 相关阅读:
    luogu 1865 数论 线性素数筛法
    洛谷 2921 记忆化搜索 tarjan 基环外向树
    洛谷 1052 dp 状态压缩
    洛谷 1156 dp
    洛谷 1063 dp 区间dp
    洛谷 2409 dp 月赛题目
    洛谷1199 简单博弈 贪心
    洛谷1417 烹调方案 dp 贪心
    洛谷1387 二维dp 不是特别简略的题解 智商题
    2016 10 28考试 dp 乱搞 树状数组
  • 原文地址:https://www.cnblogs.com/todd/p/1743537.html
Copyright © 2011-2022 走看看