利用System.Net.Mail中的MialMessage对象和SmtpClient对象实现邮件发送
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Mail;//导入命名空间 namespace PandaMail { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static MailMessage Mail; public static SmtpClient Client; //取消 private void button2_Click(object sender, EventArgs e) { this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; this.textBox4.Text = ""; } //关闭 private void button3_Click(object sender, EventArgs e) { this.Close(); Application.Exit(); } public void TestMessage(string server) { try { Mail = new MailMessage(textBox2.Text.Trim(),textBox3.Text.Trim()); //获取邮件的主题行 Mail.Subject = textBox4.Text.Trim(); //邮件内容 Mail.Body = textBox1.Text; Client = new SmtpClient(server,25); Client.Send(Mail); MessageBox.Show(this,"邮件发送成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //发送 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Trim()!= null) { TestMessage("pop.163.com"); } else { MessageBox.Show("请认真填写邮件"); return; } } } }