C# 郵件發送程式

來源:互聯網
上載者:User

用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服務了,據說新浪還可以吧...我是用公司的郵箱測試的上面的代碼是可以發送的~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.