通過smtp服務器發送郵件:
//txtFrom.text是發件人郵件地址,txtGet.Text是收件人郵件地址
MailMessage Mail = new MailMessage(this.txtFrom.Text, this.txtGet.Text);
Mail.Subject = this.txtSubject.Text.Trim().ToString();
Mail.Body = this.richTextBox1.Text.Trim().ToString();
SmtpClient Client = new SmtpClient("smtp.126.com",25); //實例一個SmtpClient類
//txtusername是發件人郵箱的帳號,txtpwd是郵箱的密碼
Client.Credentials = new System.Net.NetworkCredential(txtusername.Text,txtpwd.Text);
//Client.Host = "smtp.126.com";
//附件
if (txtAttach.Text.Length > 0)
{
//添加附件
Mail.Attachments.Add(new Attachment(txtAttach.Text, MediaTypeNames.Application.Octet));
}
//發送郵件
Client.Send(Mail);
程序的界面設計如下: