用C#實現的一個簡單的winForm郵件發送程式,具體如下:
在Design介面拖入幾個TextBox,Label 和一個Button,見:
在Send按鈕的點擊事件代碼如下:
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient mailClient = new SmtpClient("mail.sina.com"); //SMTP伺服器的名字;
mailClient.Credentials = new NetworkCredential("xxx@sina.com", "****"); //使用者名稱和密碼;
MailAddress from = new MailAddress("xxx@sina.com");//寄件者地址;
MailAddress to = new MailAddress(this.txtMailTo.Text);
MailMessage message = new MailMessage(from, to);
message.Subject = this.txtSubject.Text;
message.Body = this.rtxtContent.Text;
if (this.txtCC.Text.Trim() != "")
{
MailAddress copy = new MailAddress(this.txtCC.Text.Trim());
message.CC.Add(copy);
}
try
{
mailClient.Send(message);
MessageBox.Show(this, "提示", "發送成功!", MessageBoxButtons.OK, MessageBoxIcon.Information); }
catch (Exception ex)
{
throw ex;
}
}
需要添加的引用:
using System;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
就這樣,一個簡單的郵件發送程式完成了~~~
當然,你如果需要群發,郵件模板等功能的話可以在此基礎上修改。
現在網上大部分免費的郵箱都不提供免費的SMTP服務了,據說新浪還可以吧...我是用公司的郵箱測試的上面的代碼是可以發送的~