最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)SMTP服務發(fā)送郵件的示例代碼

 更新時間:2022年12月27日 09:14:26   作者:芝麻粒兒  
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)SMTP服務發(fā)送郵件的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

實踐過程

效果

代碼

public partial class frmSend : Form
{
    public frmSend()
    {
        InitializeComponent();
    }
    //對郵件內(nèi)容進行編碼
    private static string Base64Encode(string str)
    {
        return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
    }

    private void SendEmail(MailMessage message)
    {
        message.Subject = Base64Encode(txtSubject.Text);    //設置發(fā)送郵件的主題
        message.Body = Base64Encode(txtContent.Text);       //設置發(fā)送郵件的內(nèi)容
        //實例化SmtpClient郵件發(fā)送類對象
        SmtpClient client = new SmtpClient(txtServer.Text, Convert.ToInt32(txtPort.Text));
        //設置用于驗證發(fā)件人身份的憑據(jù)
        client.Credentials = new System.Net.NetworkCredential(txtName.Text, txtPwd.Text);
        //發(fā)送郵件
        client.Send(message);
    }

    private void frmSend_Load(object sender, EventArgs e)
    {
        txtServer.Text = Dns.GetHostName();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            if (validateEmail(txtSend.Text))
            {
                //設置郵件發(fā)送人和接收人
                MailMessage message = null;
                if (txtTo.Text.IndexOf(",") != -1)
                {
                    string[] strEmail = txtTo.Text.Split(',');
                    string sumEmail = "";
                    for (int i = 0; i < strEmail.Length; i++)
                    {
                        sumEmail = strEmail[i];
                        message = new MailMessage(new MailAddress(txtSend.Text), new MailAddress(sumEmail));
                        SendEmail(message);
                    }
                }
                else
                {
                    message = new MailMessage(new MailAddress(txtSend.Text), new MailAddress(txtTo.Text));
                    SendEmail(message);
                }
                MessageBox.Show("發(fā)送成功");
            }
        }
        catch
        {
            MessageBox.Show("發(fā)送失敗!");
        }
    }

    private void frmSend_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult = DialogResult.OK;
    }

    #region  驗證輸入為Email
    /// <summary>
    /// 驗證輸入為Email
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool validateEmail(string str)
    {
        return Regex.IsMatch(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    }
    #endregion
}
partial class frmSend
{
    /// <summary>
    /// 必需的設計器變量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的資源。
    /// </summary>
    /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗體設計器生成的代碼

    /// <summary>
    /// 設計器支持所需的方法 - 不要
    /// 使用代碼編輯器修改此方法的內(nèi)容。
    /// </summary>
    private void InitializeComponent()
    {
        this.txtContent = new System.Windows.Forms.TextBox();
        this.txtSubject = new System.Windows.Forms.TextBox();
        this.txtPort = new System.Windows.Forms.TextBox();
        this.txtServer = new System.Windows.Forms.TextBox();
        this.txtPwd = new System.Windows.Forms.TextBox();
        this.txtName = new System.Windows.Forms.TextBox();
        this.txtTo = new System.Windows.Forms.TextBox();
        this.txtSend = new System.Windows.Forms.TextBox();
        this.label8 = new System.Windows.Forms.Label();
        this.label7 = new System.Windows.Forms.Label();
        this.label6 = new System.Windows.Forms.Label();
        this.label5 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.btnSend = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // txtContent
        // 
        this.txtContent.Location = new System.Drawing.Point(23, 143);
        this.txtContent.Multiline = true;
        this.txtContent.Name = "txtContent";
        this.txtContent.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.txtContent.Size = new System.Drawing.Size(519, 100);
        this.txtContent.TabIndex = 7;
        // 
        // txtSubject
        // 
        this.txtSubject.Location = new System.Drawing.Point(88, 94);
        this.txtSubject.Name = "txtSubject";
        this.txtSubject.Size = new System.Drawing.Size(186, 21);
        this.txtSubject.TabIndex = 6;
        // 
        // txtPort
        // 
        this.txtPort.Location = new System.Drawing.Point(356, 62);
        this.txtPort.Name = "txtPort";
        this.txtPort.Size = new System.Drawing.Size(186, 21);
        this.txtPort.TabIndex = 5;
        // 
        // txtServer
        // 
        this.txtServer.Location = new System.Drawing.Point(88, 63);
        this.txtServer.Name = "txtServer";
        this.txtServer.Size = new System.Drawing.Size(186, 21);
        this.txtServer.TabIndex = 4;
        // 
        // txtPwd
        // 
        this.txtPwd.Location = new System.Drawing.Point(356, 34);
        this.txtPwd.Name = "txtPwd";
        this.txtPwd.PasswordChar = '*';
        this.txtPwd.Size = new System.Drawing.Size(186, 21);
        this.txtPwd.TabIndex = 3;
        // 
        // txtName
        // 
        this.txtName.Location = new System.Drawing.Point(88, 35);
        this.txtName.Name = "txtName";
        this.txtName.Size = new System.Drawing.Size(186, 21);
        this.txtName.TabIndex = 2;
        // 
        // txtTo
        // 
        this.txtTo.Location = new System.Drawing.Point(356, 6);
        this.txtTo.Name = "txtTo";
        this.txtTo.Size = new System.Drawing.Size(186, 21);
        this.txtTo.TabIndex = 1;
        // 
        // txtSend
        // 
        this.txtSend.Location = new System.Drawing.Point(88, 6);
        this.txtSend.Name = "txtSend";
        this.txtSend.Size = new System.Drawing.Size(186, 21);
        this.txtSend.TabIndex = 0;
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Location = new System.Drawing.Point(15, 121);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(65, 12);
        this.label8.TabIndex = 12;
        this.label8.Text = "郵件內(nèi)容:";
        // 
        // label7
        // 
        this.label7.AutoSize = true;
        this.label7.Location = new System.Drawing.Point(15, 93);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(65, 12);
        this.label7.TabIndex = 11;
        this.label7.Text = "郵件主題:";
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(290, 65);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(53, 12);
        this.label6.TabIndex = 14;
        this.label6.Text = "端口號:";
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(21, 65);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(53, 12);
        this.label5.TabIndex = 17;
        this.label5.Text = "服務器:";
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(278, 37);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(77, 12);
        this.label4.TabIndex = 16;
        this.label4.Text = "發(fā)件人密碼:";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(9, 37);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(77, 12);
        this.label3.TabIndex = 15;
        this.label3.Text = "發(fā)件人名稱:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(290, 9);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(53, 12);
        this.label2.TabIndex = 13;
        this.label2.Text = "收件人:";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(21, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(53, 12);
        this.label1.TabIndex = 18;
        this.label1.Text = "發(fā)件人:";
        // 
        // btnSend
        // 
        this.btnSend.Location = new System.Drawing.Point(217, 251);
        this.btnSend.Name = "btnSend";
        this.btnSend.Size = new System.Drawing.Size(116, 23);
        this.btnSend.TabIndex = 9;
        this.btnSend.Text = "發(fā)送";
        this.btnSend.UseVisualStyleBackColor = true;
        this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
        // 
        // frmSend
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(551, 279);
        this.Controls.Add(this.txtContent);
        this.Controls.Add(this.txtSubject);
        this.Controls.Add(this.txtPort);
        this.Controls.Add(this.txtServer);
        this.Controls.Add(this.txtPwd);
        this.Controls.Add(this.txtName);
        this.Controls.Add(this.txtTo);
        this.Controls.Add(this.txtSend);
        this.Controls.Add(this.label8);
        this.Controls.Add(this.label7);
        this.Controls.Add(this.label6);
        this.Controls.Add(this.label5);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.btnSend);
        this.Name = "frmSend";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "使用SMTP服務發(fā)送不帶附件的郵件";
        this.Load += new System.EventHandler(this.frmSend_Load);
        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmSend_FormClosing);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox txtContent;
    private System.Windows.Forms.TextBox txtSubject;
    private System.Windows.Forms.TextBox txtPort;
    private System.Windows.Forms.TextBox txtServer;
    private System.Windows.Forms.TextBox txtPwd;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.TextBox txtTo;
    private System.Windows.Forms.TextBox txtSend;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button btnSend;
}

到此這篇關于C#實現(xiàn)SMTP服務發(fā)送郵件的示例代碼的文章就介紹到這了,更多相關C# SMTP服務發(fā)送郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 一文掌握C# JSON(2023最新整理)

    一文掌握C# JSON(2023最新整理)

    JSON的全稱是JavaScript Object Notation,意思是JavaScript對象表示法,它是一種基于文本,獨立于語言的輕量級數(shù)據(jù)交換格式,這篇文章主要介紹了C#中的JSON(2023最新整理),需要的朋友可以參考下
    2023-05-05
  • C# WinForm程序設計簡單計算器

    C# WinForm程序設計簡單計算器

    這篇文章主要為大家詳細介紹了C# WinForm程序設計簡單計算器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子

    在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子

    這篇文章介紹了在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子,有需要的朋友可以參考一下
    2013-09-09
  • C#視頻轉(zhuǎn)換類分享

    C#視頻轉(zhuǎn)換類分享

    這篇文章主要為大家分享了C#視頻轉(zhuǎn)換類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#實現(xiàn)帶進度條的ListView

    C#實現(xiàn)帶進度條的ListView

    這篇文章主要介紹了C#實現(xiàn)帶進度條的ListView 的相關資料,需要的朋友可以參考下
    2016-02-02
  • C#編程之事務用法

    C#編程之事務用法

    這篇文章主要介紹了C#編程之事務用法,結合實例形式對比分析了C#中事務提交與回滾的具體實現(xiàn)技巧與相關注意事項,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • C#實現(xiàn)rar壓縮與解壓縮文件的方法

    C#實現(xiàn)rar壓縮與解壓縮文件的方法

    這篇文章主要介紹了C#實現(xiàn)rar壓縮與解壓縮文件的方法,實例分析了C#利用winrar程序?qū)崿F(xiàn)文件的壓縮與解壓縮的相關技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用Interlocked實現(xiàn)線程同步

    C#使用Interlocked實現(xiàn)線程同步

    今天小編就為大家分享一篇關于C#使用Interlocked實現(xiàn)線程同步,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • C#計時器的三種實現(xiàn)方法

    C#計時器的三種實現(xiàn)方法

    這篇文章主要介紹了C#計時器的三種實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • Unity3D運行報DllNotFoundException錯誤的解決方案

    Unity3D運行報DllNotFoundException錯誤的解決方案

    這篇文章主要介紹了Unity3D運行報DllNotFoundException錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論

上杭县| 穆棱市| 灵山县| 丹江口市| 南宁市| 五寨县| 黔西县| 枞阳县| 阿合奇县| 渝中区| 饶河县| 松桃| 剑川县| 横峰县| 迭部县| 涿州市| 万年县| 唐山市| 龙泉市| 平果县| 南和县| 玉山县| 南京市| 威信县| 新和县| 镇雄县| 长顺县| 伊宁县| 莱阳市| 兴仁县| 东宁县| 荆门市| 绍兴县| 广西| 宣恩县| 二连浩特市| 依安县| 玉屏| 衡水市| 邯郸市| 乐清市|