本文演示了在C#中使用邮件服务的一般方法:
//发送邮件 MailMessage mail = new MailMessage(); mail.Subject = subject; mail.From = new MailAddress(EmailFrom, EmailFromDip); mail.To.Add(new MailAddress(EmailTo, EmailToDsp)); //可以添加多个收件人 //mail.CC.Add(new MailAddress("xx@xxx.cn", "xys")); //抄送 mail.Body = EmailBody; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = false; mail.Priority = MailPriority.High; SmtpClient smtp = new SmtpClient(EmailSmtp, 25); smtp.EnableSsl = false; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(UserName, PassWord); //通行证 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //递送方法 为 网络 try { smtp.Send(mail); sw.WriteLine("EMail send success" + " " + DateTime.Now.ToLongDateString()); sw.Flush(); } catch (Exception ex) { sw.WriteLine("EMail send error" + " "+ DateTime.Now.ToLongDateString()); sw.Flush(); }