SMTP协议操作
SMTP操作163邮箱
1private void button1_Click(object sender, EventArgs e)
2{
3SmtpClient SMTPClient = new SmtpClient("SMTP.163.com", 25);
4MailAddress SrcAddr = new MailAddress("XXX@163.com", "XX");
5MailAddress AimAddr = new MailAddress("XXX@163.com");
6SMTPClient.Credentials = new NetworkCredential("XXXX", "XXXX");
7MailMessage Msg = new MailMessage(SrcAddr.Address, AimAddr.Address, "Title_Test_A", "Testing Form Smtp Client");
8Msg.IsBodyHtml = false;
9Msg.Priority = MailPriority.High;
10Msg.ReplyTo = SrcAddr;
11Msg.SubjectEncoding = Encoding.UTF8;
12Msg.BodyEncoding = Encoding.UTF8;
13
14SMTPClient.SendCompleted += new SendCompletedEventHandler(SMTPClient_SendCompleted);
15SMTPClient.SendAsync(Msg, new object());
16}
17void SMTPClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
18{
19if (e.Cancelled)
20{
21MessageBox.Show("Cancel");
22}
23if (e.Error != null)
24{
25MessageBox.Show(e.Error.Message);
26}
27MessageBox.Show(e.UserState.ToString());
28}
2{
3SmtpClient SMTPClient = new SmtpClient("SMTP.163.com", 25);
4MailAddress SrcAddr = new MailAddress("XXX@163.com", "XX");
5MailAddress AimAddr = new MailAddress("XXX@163.com");
6SMTPClient.Credentials = new NetworkCredential("XXXX", "XXXX");
7MailMessage Msg = new MailMessage(SrcAddr.Address, AimAddr.Address, "Title_Test_A", "Testing Form Smtp Client");
8Msg.IsBodyHtml = false;
9Msg.Priority = MailPriority.High;
10Msg.ReplyTo = SrcAddr;
11Msg.SubjectEncoding = Encoding.UTF8;
12Msg.BodyEncoding = Encoding.UTF8;
13
14SMTPClient.SendCompleted += new SendCompletedEventHandler(SMTPClient_SendCompleted);
15SMTPClient.SendAsync(Msg, new object());
16}
17void SMTPClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
18{
19if (e.Cancelled)
20{
21MessageBox.Show("Cancel");
22}
23if (e.Error != null)
24{
25MessageBox.Show(e.Error.Message);
26}
27MessageBox.Show(e.UserState.ToString());
28}