发送邮件
来源:互联网 发布:sql取前几个字符 编辑:程序博客网 时间:2024/06/10 14:15
图1 “发送邮件”窗体 图2 提示信息using System.Web.Mail;using System.IO;using System.Net;private void button1_Click(object sender, EventArgs e){ try { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = txtFrom.Text.Trim(); mail.To = txtGet.Text.Trim(); mail.Subject = Base64Encode(txtSubject.Text); mail.Body = Base64Encode(richTextBox1.Text); mail.BodyFormat = MailFormat.Text; SmtpMail.SmtpServer = txtServer.Text.Trim(); SmtpMail.Send(mail); MessageBox.Show("邮件成功发送到" + txtGet.Text); } catch (Exception ex) { MessageBox.Show(ex.ToString()); }} 注意:MailMessage类还提供群发功能以及发送附件(通过Attachments类)的功能。using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms; using System.Net.Mail;using System.IO;using System.Net;using System.Web.Mail; namespace Ex16_08{ public partial class Send_mal : Form { public Send_mal() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } public static string Base64Encode(string str) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); } private void button1_Click(object sender, EventArgs e) { try { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = txtFrom.Text.Trim(); mail.To = txtGet.Text.Trim(); mail.Subject = Base64Encode(txtSubject.Text); mail.Body = Base64Encode(richTextBox1.Text); mail.BodyFormat = MailFormat.Text; SmtpMail.SmtpServer = txtServer.Text.Trim(); SmtpMail.Send(mail); MessageBox.Show("邮件成功发送到" + txtGet.Text); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }}namespace Ex16_08{ partial class Send_mal { /// <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.button1 = new System.Windows.Forms.Button(); this.txtFrom = new System.Windows.Forms.TextBox(); this.txtGet = new System.Windows.Forms.TextBox(); this.txtSubject = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.label4 = new System.Windows.Forms.Label(); this.txtServer = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(286, 94); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "发送"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // txtFrom // this.txtFrom.Location = new System.Drawing.Point(67, 39); this.txtFrom.Name = "txtFrom"; this.txtFrom.Size = new System.Drawing.Size(294, 21); this.txtFrom.TabIndex = 1; // // txtGet // this.txtGet.Location = new System.Drawing.Point(67, 67); this.txtGet.Name = "txtGet"; this.txtGet.Size = new System.Drawing.Size(294, 21); this.txtGet.TabIndex = 2; // // txtSubject // this.txtSubject.Location = new System.Drawing.Point(67, 94); this.txtSubject.Name = "txtSubject"; this.txtSubject.Size = new System.Drawing.Size(215, 21); this.txtSubject.TabIndex = 3; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(8, 70); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 4; this.label1.Text = "收件人:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(8, 42); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(53, 12); this.label2.TabIndex = 5; this.label2.Text = "发件人:"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(20, 97); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 6; this.label3.Text = "主题:"; // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(4, 121); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(357, 155); this.richTextBox1.TabIndex = 7; this.richTextBox1.Text = ""; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(8, 16); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 8; this.label4.Text = "服务器:"; // // txtServer // this.txtServer.Location = new System.Drawing.Point(67, 13); this.txtServer.Name = "txtServer"; this.txtServer.Size = new System.Drawing.Size(294, 21); this.txtServer.TabIndex = 9; // // Send_mal // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(364, 284); this.Controls.Add(this.txtServer); this.Controls.Add(this.label4); this.Controls.Add(this.richTextBox1); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtSubject); this.Controls.Add(this.txtGet); this.Controls.Add(this.txtFrom); this.Controls.Add(this.button1); this.Name = "Send_mal"; this.Text = "sendMail"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox txtFrom; private System.Windows.Forms.TextBox txtGet; private System.Windows.Forms.TextBox txtSubject; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtServer; }} using System;using System.Collections.Generic;using System.Windows.Forms; namespace Ex16_08{ static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Send_mal()); } }}
0 0
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 发送邮件
- 邮件发送
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 邮件发送
- 邮件发送
- 发送邮件
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 发送邮件
- 快速建立STM32F4的工程模板
- 北大POJ解题报告-1004 Financial Management
- 设计模式------模板方法模式
- 题目1061:成绩排序
- (一)REST API概念
- 发送邮件
- 从一道面试题来认识java类加载时机与过程
- Algorithms (并查集)
- 我的博客
- Android短信监听(二)——利用ContentObserver实现短信监听
- 利用 squid 反向代理提高网站性能
- 二叉树复制和左右子树互换
- 题目1185:特殊排序 考研机试教程题目3
- openlog()