添加引用using System.Net.Mail;using System.Net;
定义常量
const string ADDRESS_FROM = userid@gmail.com;
const string ADDRESS_TO = userid2@gmail.com;
const string SMTP_SERVER = "smtp.gmail.com";
const string USER_ID = "userid";
const string PASSWORD = "123456";
const string Mail_Title = "测试邮件";
const string Mail_Text = "内容";
const string Mail_AnnexPath = "c:\\1.jpg";
const int PORT = 587;
实现方法
public void SendMail(string smtpServer, int port)
{
SmtpClient mailClient = new SmtpClient(smtpServer, PORT);
mailClient.EnableSsl = true;
NetworkCredential crendetial = new NetworkCredential(USER_ID, PASSWORD);
mailClient.Credentials = crendetial;
MailMessage message = new MailMessage(ADDRESS_FROM, ADDRESS_TO, Mail_Title, Mail_Text);
Attachment attachment = new Attachment(Mail_AnnexPath);
message.Attachments.Add(attachment);
mailClient.Send(message);
}
调用方法
SendMail(SMTP_SERVER, PORT);