C#实现SMTP服务发送邮件的示例代码

 更新时间:2022年12月27日 09:14:26   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现SMTP服务发送邮件的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下

实践过程

效果

代码

public partial class frmSend : Form
{
    public frmSend()
    {
        InitializeComponent();
    }
    //对邮件内容进行编码
    private static string Base64Encode(string str)
    {
        return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
    }

    private void SendEmail(MailMessage message)
    {
        message.Subject = Base64Encode(txtSubject.Text);    //设置发送邮件的主题
        message.Body = Base64Encode(txtContent.Text);       //设置发送邮件的内容
        //实例化SmtpClient邮件发送类对象
        SmtpClient client = new SmtpClient(txtServer.Text, Convert.ToInt32(txtPort.Text));
        //设置用于验证发件人身份的凭据
        client.Credentials = new System.Net.NetworkCredential(txtName.Text, txtPwd.Text);
        //发送邮件
        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))
            {
                //设置邮件发送人和接收人
                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("发送成功");
            }
        }
        catch
        {
            MessageBox.Show("发送失败!");
        }
    }

    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>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </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 = "邮件内容:";
        // 
        // 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 = "发件人密码:";
        // 
        // 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 = "发件人名称:";
        // 
        // 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 = "发件人:";
        // 
        // 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 = "发送";
        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服务发送不带附件的邮件";
        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#实现SMTP服务发送邮件的示例代码的文章就介绍到这了,更多相关C# SMTP服务发送邮件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#使用Resources资源文件

    C#使用Resources资源文件

    这篇文章介绍了C#使用Resources资源文件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 一个可逆加密的类(使用3DES加密)

    一个可逆加密的类(使用3DES加密)

    表示三重数据加密标准算法的基类,TripleDES 的所有实现都必须从此基类派生。是从 SymmetricAlgorithm 类里继承出来。
    2011-07-07
  • 详解C# 反射(Reflection)

    详解C# 反射(Reflection)

    这篇文章主要介绍了C# 反射(Reflection)的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • WPF实现背景灯光随鼠标闪动效果

    WPF实现背景灯光随鼠标闪动效果

    这篇文章主要为大家详细介绍了WPF实现背景灯光随鼠标闪动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • C#分屏控件用法实例

    C#分屏控件用法实例

    这篇文章主要介绍了C#分屏控件用法实例,需要的朋友可以参考下
    2014-08-08
  • c# 类型的字段和方法设计建议

    c# 类型的字段和方法设计建议

    这篇文章主要介绍了c# 字段和方法设计建议,帮助大家更好的理解和使用c#编程语言,感兴趣的朋友可以了解下
    2020-11-11
  • 浅谈C# async await 死锁问题总结

    浅谈C# async await 死锁问题总结

    这篇文章主要介绍了浅谈C# async await 死锁问题总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • C#使用foreach语句搜索数组元素的方法

    C#使用foreach语句搜索数组元素的方法

    这篇文章主要介绍了C#使用foreach语句搜索数组元素的方法,涉及C#使用foreach语句遍历数组实现搜索功能的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • c#隐藏基类方法的作用

    c#隐藏基类方法的作用

    这篇文章主要介绍了c#隐藏基类方法的作用,大家可以参考使用
    2013-12-12
  • C#调用dll报错:无法加载dll,找不到指定模块的解决

    C#调用dll报错:无法加载dll,找不到指定模块的解决

    这篇文章主要介绍了C#调用dll报错:无法加载dll,找不到指定模块的解决问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01

最新评论