电信的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.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.Send(MMsg);
MailMessage MMsg = new MailMessage();
MMsg.Subject = "公网IP";
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.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;
}
}