c# winform 程序用126邮箱发邮件
代码如下:
private void button1_Click(object sender, EventArgs e) { string strTitle = "alert! We have not receive new data from mera over 20 mins"; try { SmtpClient client = new SmtpClient("smtp.126.com", 25) { Credentials = new NetworkCredential("emailAddress", "password"), EnableSsl = true }; MailAddress from = new MailAddress(@"emailAddress", "senderName"); MailAddress to = new MailAddress(@"receiverAddress", "ReceiverName"); MailMessage myMail = new System.Net.Mail.MailMessage(from, to); myMail.Subject = strTitle; myMail.SubjectEncoding = System.Text.Encoding.UTF8; myMail.Body = ""; myMail.BodyEncoding = System.Text.Encoding.UTF8; client.Send(myMail); } catch (SmtpException ex) { throw new ApplicationException ("SmtpException has occured: " + ex.Message); } } // end of function
需要注意的是, 要使用注册很久的126邮箱发送,因为新注册的邮箱网易限制了这个功能 。