c#通过126邮箱自动发送 微信端实现QQ邮箱邮件提醒
程序员文章站
2022-06-16 08:42:14
...
c#通过126邮箱自动发送 微信端实现QQ邮箱邮件提醒
一、首先开启126邮箱的SMTP服务,需要短信验证,设置邮箱密码等操作。详细操作:https://jingyan.baidu.com/article/17bd8e526ddb4185aa2bb852.html
二、微信端设置开启QQ邮箱提醒,具体操作:https://kf.qq.com/touch/faq/150612YrMBvY150612YN3mUz.html
开启QQ邮箱提醒。
三、发送邮箱为126邮箱,接收邮箱为QQ邮箱。
public void sxSendEmail(string mailSubject, string mailContent, string tostr)
{ // 设置发送方的邮件信息,例如使用网易的smtp
ArrayList userlist = new ArrayList();
StreamReader objReader = new StreamReader("userlist.txt");
string sLine = "111";
// ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
userlist.Add(sLine);
}
objReader.Close();
string smtpServer = userlist[0].ToString();// "smtp.126.com"; //SMTP服务器
string mailFrom = userlist[1].ToString();// "aaa@qq.com"; //登陆用户名
string userPassword = userlist[2].ToString();//"xxx";//登陆密码
// string recipientsDisplayName = "111111";
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// MailAddress from = new MailAddress(mailFrom, "aaa@qq.com");
// 发送邮件设置
MailMessage mailMessage = new MailMessage(); // 发送人和收件人
// mailMessage.From.Address = "aaa@qq.com";
// mailMessage.From.DisplayName = "aaa@qq.com";
mailMessage.From = new MailAddress(mailFrom, userlist[1].ToString());
// for(int i=0;i<maillist.Count;i++)
mailMessage.To.Add(tostr);//(maillist[i].ToString());
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
try
{
smtpClient.Send(mailMessage); // 发送邮件
sxWriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + tostr + mailSubject + mailContent + "发送成功");
}
catch (SmtpException ex)
{
sxWriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + tostr + mailSubject + mailContent + ex.ToString() + "发送邮件出错");
}
}
四、定时检测,本次检测与上一次检测不同,则向指定QQ邮箱发送提醒邮件,从而实现微信提醒。
string resultstr = “111111111111111111111111111111111111111111111111111111111111111111111111111111111”;//默认检测的81路端口信号都为正常状态1(UP)。不正常状态为2(DOWN)
private void timer2_Tick(object sender, EventArgs e)
{
try
{
string tempstr = "";
string sxstr = readTxt("sxoid.txt");
string sxplace=readTxt("sxplace.txt");
char[] chs={','};
string[] places = sxplace.Split(chs, StringSplitOptions.RemoveEmptyEntries);
string[] nums = sxstr.Split(chs, StringSplitOptions.RemoveEmptyEntries);
OctetString community = new OctetString("zjgdread");
AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver1;
for (int i = 2; i < nums.Length; i++)
{
IpAddress agent = new IpAddress(nums[0].ToString());//("10.1.92.252");
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 200, 1);
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
// pdu.VbList.Add("1.3.6.1.2.1.2.2.1.2." + nums[i].ToString()); //interface name
// pdu.VbList.Add("1.3.6.1.2.1.2.2.1.5." + nums[i].ToString()); //interface speed
// pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18." + nums[i].ToString()); //ifalias//
pdu.VbList.Add(nums[1].ToString() + nums[i].ToString()); //interface oper status
SnmpV1Packet result = null;
try
{
// Make SNMP request
result = (SnmpV1Packet)target.Request(pdu, param);
}
catch (Exception err)
{
sxWriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + err.ToString());
result = null;
}
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
WriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "Error in SNMP reply");
}
else
{
// d1 = double.Parse(result.Pdu.VbList[0].Value.ToString()) / 10.0;
// d2 = double.Parse(result.Pdu.VbList[1].Value.ToString()) / 10.0;
//numstr = d1.ToString() + "," + d2.ToString();
/* dr[1] = result.Pdu.VbList[0].Value.ToString();
dr[2] = result.Pdu.VbList[1].Value.ToString();
dr[3] = GetChsFromHex(result.Pdu.VbList[2].Value.ToString());
dr[4] = result.Pdu.VbList[3].Value.ToString();
dr[5] = DateTime.Now.ToLongTimeString();
* */
tempstr = tempstr + result.Pdu.VbList[0].Value.ToString();
}
}
else
{
// Label28.Text = Label28.Text + string.Format("No response received from SNMP agent. \r\n");
sxWriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "No response received from SNMP agent. \r\n");
}
// dt.Rows.Add(dr);
target.Dispose();
}
if (tempstr == resultstr)
{
textBox5.Text = DateTime.Now.ToLongTimeString();
}
else
{
string ss = "";
if (tempstr.Length == 81)
{
int j = 0;
for (int i = 0; i < places.Length; i++)
{
j = i * 3;
ss = ss + places[i] + tempstr[j].ToString() + tempstr[j+1].ToString() + tempstr[j+2].ToString();
}
}
else
{
ss = "hw error";
}
resultstr = tempstr;
for (int i = 0; i < sxmaillist.Count; i++)
{
sxSendEmail(i.ToString() + "hw alarm", i.ToString() + ss, sxmaillist[i].ToString());
System.Threading.Thread.Sleep(1000);
}
}
//dataGridView1.DataSource = ds.Tables[0];
// GridView3.DataSource = ds.Tables[0];
// GridView3.DataBind();
}
catch (Exception err)
{
sxWriteForTxt(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + err.ToString());
//WriteForTxt( + err.ToString());
}
}
总结:利用C#程序设计通过SNMP协议,定时读取H3C交换机端口信息,当检测到端口出现异常时,会发送邮件在微信上提醒,从而实现网管中的消息提醒功能,在长距离链路传输中,具有一定价值。