今天研究了一下利用C#发邮件,很简单的,贴上代码。
//Define what mail i want to send MailMessage myMail = new MailMessage(); myMail.To.Add("test@test.com"); myMail.From = new MailAddress("test@test.com", "Yis", Encoding.UTF8); myMail.Subject = mailSubject; myMail.SubjectEncoding = Encoding.UTF8; myMail.Body = mailBody; myMail.BodyEncoding = Encoding.UTF8; myMail.IsBodyHtml = false; myMail.Priority = MailPriority.High; //Define the cliend I use to send the mail SmtpClient myClient = new SmtpClient(); myClient.Credentials = new NetworkCredential("test@test.com", "123456"); myClient.Port = 56; myClient.Host = "smtp.gmail.com"; myClient.EnableSsl = false; //Send mail try { myClient.Send(myMail); Console.WriteLine("Mail sending success"); } catch (SmtpException e) { Console.WriteLine("Failed. {0}", e.Message); }
由上可知,其实发邮件分为两部分:1)利用MailMessage定义好要发的邮件, 2)利用SmtpClinet定义好发送邮件的客户端
即可发送。