C#实现SMTP邮件附件发送功能详解

作者:芝麻粒儿 时间:2022-08-14 10:09:38 

实践过程

效果

C#实现SMTP邮件附件发送功能详解

代码

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

private void AddFile(string strFile,MailMessage message)
   {
       //为要发送的邮件创建附件信息
       Attachment myAttachment = new Attachment(strFile, System.Net.Mime.MediaTypeNames.Application.Octet);
       //为附件添加时间信息
       System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
       disposition.CreationDate = System.IO.File.GetCreationTime(strFile);
       disposition.ModificationDate = System.IO.File.GetLastWriteTime(strFile);
       disposition.ReadDate = System.IO.File.GetLastAccessTime(strFile);
       message.Attachments.Add(myAttachment);//将创建的附件添加到邮件中
   }

private void SendEmail(MailMessage message)
   {
       message.Subject = Base64Encode(txtSubject.Text);    //设置发送邮件的主题
       message.Body = Base64Encode(txtContent.Text);       //设置发送邮件的内容
       if (txtAttachment.Text != "")
       {
           if (txtAttachment.Text.IndexOf(",") != 0)
           {
               string[] strAttachment = txtAttachment.Text.Split(',');
               for (int i = 0; i < strAttachment.Length; i++)
               {
                   AddFile(strAttachment[i], message);
               }
           }
           else
           {
               AddFile(txtAttachment.Text, message);
           }
       }
       //实例化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 btnSelect_Click(object sender, EventArgs e)
   {
       if (openFileDialog.ShowDialog() == DialogResult.OK)
       {
           if (txtAttachment.Text == "")
           {
               txtAttachment.Text = openFileDialog.FileName;
           }
           else
           {
               txtAttachment.Text += "," + openFileDialog.FileName;
           }
       }
   }

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.txtAttachment = new System.Windows.Forms.TextBox();
       this.label9 = new System.Windows.Forms.Label();
       this.btnSelect = new System.Windows.Forms.Button();
       this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
       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(221, 280);
       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);
       //
       // txtAttachment
       //
       this.txtAttachment.Location = new System.Drawing.Point(62, 250);
       this.txtAttachment.Name = "txtAttachment";
       this.txtAttachment.Size = new System.Drawing.Size(437, 21);
       this.txtAttachment.TabIndex = 28;
       //
       // label9
       //
       this.label9.AutoSize = true;
       this.label9.Location = new System.Drawing.Point(15, 253);
       this.label9.Name = "label9";
       this.label9.Size = new System.Drawing.Size(41, 12);
       this.label9.TabIndex = 27;
       this.label9.Text = "附件:";
       //
       // btnSelect
       //
       this.btnSelect.Location = new System.Drawing.Point(505, 249);
       this.btnSelect.Name = "btnSelect";
       this.btnSelect.Size = new System.Drawing.Size(37, 23);
       this.btnSelect.TabIndex = 8;
       this.btnSelect.Text = "选择";
       this.btnSelect.UseVisualStyleBackColor = true;
       this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
       //
       // openFileDialog
       //
       this.openFileDialog.FileName = "openFileDialog1";
       //
       // frmSend
       //
       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
       this.ClientSize = new System.Drawing.Size(551, 313);
       this.Controls.Add(this.btnSelect);
       this.Controls.Add(this.txtAttachment);
       this.Controls.Add(this.label9);
       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;
   private System.Windows.Forms.TextBox txtAttachment;
   private System.Windows.Forms.Label label9;
   private System.Windows.Forms.Button btnSelect;
   private System.Windows.Forms.OpenFileDialog openFileDialog;
}

来源:https://blog.csdn.net/qq_27489007/article/details/128402636

标签:C#,SMTP,邮件,发送
0
投稿

猜你喜欢

  • java中List接口与实现类介绍

    2022-11-17 02:54:40
  • springMVC实现前台带进度条文件上传的示例代码

    2021-09-15 17:24:44
  • springboot快速集成mybatis-plus的详细教程

    2021-06-11 22:05:16
  • Java多线程编程中ThreadLocal类的用法及深入

    2022-03-17 03:21:29
  • Java微信公众平台之自定义菜单

    2021-09-10 15:36:10
  • Java多线程Callable接口实现代码示例

    2021-08-06 14:29:01
  • 深入浅析Mybatis的缺陷问题

    2023-07-19 19:39:46
  • 手写java性能测试框架第二版

    2023-03-15 14:07:31
  • Android中aapt命令实践

    2021-10-26 08:57:45
  • Java安全之Tomcat6 Filter内存马问题

    2022-11-20 07:29:23
  • Java应用开源框架实现简易web搜索引擎

    2023-08-22 20:20:54
  • JavaWeb使用POI操作Excel文件实例

    2022-06-12 18:19:30
  • 深度理解Java访问修饰符

    2021-10-26 17:16:08
  • C++实现企业职工工资管理系统

    2023-11-02 17:34:34
  • 深入剖析Android消息机制原理

    2023-09-30 01:57:11
  • Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法

    2022-05-24 17:14:13
  • Spring Boot实现热部署的实例方法

    2023-10-24 08:12:50
  • Java Swing实现简单的体重指数(BMI)计算器功能示例

    2022-12-16 09:34:56
  • 浅谈java多态的实现主要体现在哪些方面

    2023-08-17 07:41:32
  • Java 十大排序算法之归并排序刨析

    2022-03-15 17:29:20
  • asp之家 软件编程 m.aspxhome.com