使用C# Winform通过Gmail、Yahoo和Windows Live发送电子邮件

在本教程中,将学习如何使用C# Winform应用程序通过Gmail、Yahoo和Windows Live账户发送电子邮件。将使用SMTP协议,并展示一些基本的代码行,这些代码行在不同服务之间只有微小的变化。

致谢

这段代码是基于一个由Abhishek Sur创建的简单电子邮件应用程序构建的。可以在这里找到他的文章。

开始之前

需要准备5个文本框,6个标签,1个列表框和3个按钮。所有项目都保留默认名称,例如(textBox1、textBox2等)。将label1的文本更改为(TO)。将label2的文本更改为(GMAIL ACCT),label3更改为(密码),label4更改为(主题),label5更改为(消息正文)。标签1-5将与相应的文本框1-5配对。现在将button1的文本更改为(添加附件),button2更改为(移除附件),button3更改为(发送邮件)。现在取textBox5并将其设置为多行,并将其拉伸,直到文本框类似于文本区域。现在添加一个列表框,这个列表框将处理附件,并将label6更改为(附件)。

使用代码

如果像之前提到的那样保留了默认名称,使用代码就非常直观了。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Mail; using System.Net.Mime; namespace EmailClient { public partial class GoogleForm : Form { MailMessage mail = new MailMessage(); public GoogleForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { smtp_csharp.frmAddAttachment frm = new smtp_csharp.frmAddAttachment(); frm.ShowDialog(this); if (frm.txtFile.Text.Trim() != "") { listBox1.Items.Add(frm.txtFile.Text); } frm.Dispose(); } private void button2_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { listBox1.Items.RemoveAt(listBox1.SelectedIndex); } } private void button3_Click(object sender, EventArgs e) { using (MailMessage mailMessage = new MailMessage(new MailAddress(textBox1.Text), new MailAddress(textBox1.Text))) { mailMessage.Body = textBox5.Text; mailMessage.Subject = textBox4.Text; try { SmtpClient SmtpServer = new SmtpClient(); SmtpServer.Credentials = new System.Net.NetworkCredential(textBox2.Text, textBox3.Text); SmtpServer.Port = 587; SmtpServer.Host = "smtp.gmail.com"; SmtpServer.EnableSsl = true; mail = new MailMessage(); String[] addr = textBox1.Text.Split(','); mail.From = new MailAddress(textBox2.Text); Byte i; for (i = 0; i < addr.Length; i++) { mail.To.Add(addr[i]); } mail.Subject = textBox4.Text; mail.Body = textBox5.Text; if (listBox1.Items.Count != 0) { for (i = 0; i < listBox1.Items.Count; i++) { mail.Attachments.Add(new Attachment(listBox1.Items[i].ToString())); } } mail.IsBodyHtml = true; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.ReplyTo = new MailAddress(textBox1.Text); SmtpServer.Send(mail); } catch (Exception ex) { MessageBox.Show(ex.Message, "EMail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } } }

对于附件表单,创建一个新表单并添加1个文本框,名称为txtFile,以及3个按钮。第一个按钮的名称为btnBrowse,文本为浏览,第二个按钮的名称为btnOK,文本为确定,第三个按钮的名称为btnCancel,文本为取消。这个表单的代码如下:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace smtp_csharp { public class frmAddAttachment : System.Windows.Forms.Form { internal System.Windows.Forms.Button btnCancel; internal System.Windows.Forms.Button btnOK; internal System.Windows.Forms.Button btnBrowse; internal System.Windows.Forms.OpenFileDialog dlg; public System.Windows.Forms.TextBox txtFile; private System.ComponentModel.Container components = null; public frmAddAttachment() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void btnBrowse_Click(object sender, System.EventArgs e) { dlg.ShowDialog(); txtFile.Text = dlg.FileName; } private void btnOK_Click(object sender, System.EventArgs e) { this.Hide(); } private void btnCancel_Click(object sender, System.EventArgs e) { txtFile.Text = ""; this.Hide(); } } }

如果要通过Yahoo发送邮件,代码将保持不变,只需更改2行代码。

要更改的原始代码为:

SmtpServer.Host = "smtp.gmail.com"; SmtpServer.EnableSsl = true;

上述代码应更改为:

SmtpServer.Host = "smtp.mail.yahoo.com"; SmtpServer.EnableSsl = false;

现在可以通过Gmail和Yahoo发送电子邮件了。Live邮件稍微复杂一些,因为它使用SSL和SPA。但是将添加2行代码并更改1行代码,从原始的Gmail应用程序。

要更改的原始代码行是:

SmtpServer.Host = "smtp.gmail.com";

上述代码应更改为:

SmtpServer.Host = "smtp.live.com";

现在将向应用程序中添加2行代码:

第一行代码应添加到button3的点击事件中,位于:

SmtpClient SmtpServer = new SmtpClient();

和:

NetworkCredential cred = new NetworkCredential(textBox2.Text, textBox3.Text);

之间。要添加的代码是:

SmtpServer.Credentials = new System.Net.NetworkCredential(textBox2.Text, textBox3.Text); using System.Net;
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485