<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
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服務傳送郵件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45