Создание Windows-приложений на основе Visual C#


           

Clean up any resources being


using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Web.Mail; using System.Threading;
namespace MailApplication { /// <summary> /// Summary description for SendMessage. /// </summary> public class SendMessage : System.Windows.Forms.Form { private MailMessage message; private System.Windows.Forms.Label lblTo; private System.Windows.Forms.TextBox txbTo; private System.Windows.Forms.TextBox txbCopy; private System.Windows.Forms.Label lblCopy; private System.Windows.Forms.TextBox txbBlindCopy; private System.Windows.Forms.Label lblBlindCopy; private System.Windows.Forms.TextBox txbSubject; private System.Windows.Forms.Label lblSubject; private System.Windows.Forms.TextBox txbBody; private System.Windows.Forms.Label lblBody; private System.Windows.Forms.Button btnSend; private System.Windows.Forms.Panel pblAttachments; private System.Windows.Forms.Label lblAttachments; private System.Windows.Forms.Button btnAddAttach; private System.Windows.Forms.Button btnViewAttach; private System.Windows.Forms.TextBox txbAttach; private System.Windows.Forms.Label lblAttachNumber; private System.Windows.Forms.OpenFileDialog oFDAttach; private System.Windows.Forms.ContextMenu contextMenuDeleteAttach; private System.Windows.Forms.MenuItem itemDeleteAttach; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;
public SendMessage() { InitializeComponent(); message = new MailMessage(); }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// Required method for Designer support — do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SendMessage)); this.lblTo = new System.Windows.Forms.Label(); this.txbTo = new System.Windows.Forms.TextBox(); this.txbCopy = new System.Windows.Forms.TextBox(); this.lblCopy = new System.Windows.Forms.Label(); this.txbBlindCopy = new System.Windows.Forms.TextBox(); this.lblBlindCopy = new System.Windows.Forms.Label(); this.txbSubject = new System.Windows.Forms.TextBox(); this.lblSubject = new System.Windows.Forms.Label(); this.txbBody = new System.Windows.Forms.TextBox(); this.lblBody = new System.Windows.Forms.Label(); this.btnSend = new System.Windows.Forms.Button(); this.pblAttachments = new System.Windows.Forms.Panel(); this.lblAttachments = new System.Windows.Forms.Label(); this.btnAddAttach = new System.Windows.Forms.Button(); this.btnViewAttach = new System.Windows.Forms.Button(); this.txbAttach = new System.Windows.Forms.TextBox(); this.lblAttachNumber = new System.Windows.Forms.Label(); this.oFDAttach = new System.Windows.Forms.OpenFileDialog(); this.contextMenuDeleteAttach = new System.Windows.Forms.ContextMenu(); this.itemDeleteAttach = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // lblTo // this.lblTo.Location = new System.Drawing.Point(32, 16); this.lblTo.Name = "lblTo"; this.lblTo.Size = new System.Drawing.Size(56, 23); this.lblTo.TabIndex = 0; this.lblTo.Text = "Кому:"; this.lblTo.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // txbTo // this.txbTo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbTo.Location = new System.Drawing.Point(112, 16); this.txbTo.Name = "txbTo"; this.txbTo.Size = new System.Drawing.Size(560, 20); this.txbTo.TabIndex = 1; this.txbTo.Text = ""; // // txbCopy // this.txbCopy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbCopy.Location = new System.Drawing.Point(112, 48); this.txbCopy.Name = "txbCopy"; this.txbCopy.Size = new System.Drawing.Size(560, 20); this.txbCopy.TabIndex = 3; this.txbCopy.Text = ""; // // lblCopy // this.lblCopy.Location = new System.Drawing.Point(32, 48); this.lblCopy.Name = "lblCopy"; this.lblCopy.Size = new System.Drawing.Size(56, 23); this.lblCopy.TabIndex = 2; this.lblCopy.Text = "Копия:"; this.lblCopy.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // txbBlindCopy // this.txbBlindCopy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbBlindCopy.Location = new System.Drawing.Point(112, 80); this.txbBlindCopy.Name = "txbBlindCopy"; this.txbBlindCopy.Size = new System.Drawing.Size(560, 20); this.txbBlindCopy.TabIndex = 5; this.txbBlindCopy.Text = ""; // // lblBlindCopy // this.lblBlindCopy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lblBlindCopy.Location = new System.Drawing.Point(8, 80); this.lblBlindCopy.Name = "lblBlindCopy"; this.lblBlindCopy.Size = new System.Drawing.Size(88, 23); this.lblBlindCopy.TabIndex = 4; this.lblBlindCopy.Text = "Скрытая копия:"; this.lblBlindCopy.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // txbSubject // this.txbSubject.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbSubject.Location = new System.Drawing.Point(112, 112); this.txbSubject.Name = "txbSubject"; this.txbSubject.Size = new System.Drawing.Size(560, 20); this.txbSubject.TabIndex = 7; this.txbSubject.Text = ""; // // lblSubject // this.lblSubject.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lblSubject.Location = new System.Drawing.Point(32, 112); this.lblSubject.Name = "lblSubject"; this.lblSubject.Size = new System.Drawing.Size(72, 23); this.lblSubject.TabIndex = 6; this.lblSubject.Text = "Тема:"; this.lblSubject.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // txbBody // this.txbBody.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbBody.Location = new System.Drawing.Point(16, 168); this.txbBody.Multiline = true; this.txbBody.Name = "txbBody"; this.txbBody.Size = new System.Drawing.Size(712, 192); this.txbBody.TabIndex = 9; this.txbBody.Text = ""; // // lblBody // this.lblBody.Location = new System.Drawing.Point(16, 144); this.lblBody.Name = "lblBody"; this.lblBody.TabIndex = 8; this.lblBody.Text = "Текст сообщения:"; this.lblBody.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // btnSend // this.btnSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnSend.Location = new System.Drawing.Point(648, 440); this.btnSend.Name = "btnSend"; this.btnSend.TabIndex = 10; this.btnSend.Text = "Отправить"; this.btnSend.Click += new System.EventHandler(this.btnSend_Click); // // pblAttachments // this.pblAttachments.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.pblAttachments.AutoScroll = true; this.pblAttachments.Location = new System.Drawing.Point(16, 416); this.pblAttachments.Name = "pblAttachments"; this.pblAttachments.Size = new System.Drawing.Size(608, 56); this.pblAttachments.TabIndex = 12; // // lblAttachments // this.lblAttachments.Location = new System.Drawing.Point(16, 368); this.lblAttachments.Name = "lblAttachments"; this.lblAttachments.Size = new System.Drawing.Size(64, 23); this.lblAttachments.TabIndex = 13; this.lblAttachments.Text = "Вложения:"; this.lblAttachments.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // btnAddAttach // this.btnAddAttach.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnAddAttach.Location = new System.Drawing.Point(648, 368); this.btnAddAttach.Name = "btnAddAttach"; this.btnAddAttach.TabIndex = 17; this.btnAddAttach.Text = "Добавить"; this.btnAddAttach.Click += new System.EventHandler(this.btnAddAttach_Click); // // btnViewAttach // this.btnViewAttach.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnViewAttach.Location = new System.Drawing.Point(568, 368); this.btnViewAttach.Name = "btnViewAttach"; this.btnViewAttach.TabIndex = 16; this.btnViewAttach.Text = "Обзор"; this.btnViewAttach.Click += new System.EventHandler(this.btnViewAttach_Click); // // txbAttach // this.txbAttach.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txbAttach.Location = new System.Drawing.Point(176, 368); this.txbAttach.Name = "txbAttach"; this.txbAttach.Size = new System.Drawing.Size(384, 20); this.txbAttach.TabIndex = 15; this.txbAttach.Text = ""; // // lblAttachNumber // this.lblAttachNumber.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lblAttachNumber.Location = new System.Drawing.Point(120, 368); this.lblAttachNumber.Name = "lblAttachNumber"; this.lblAttachNumber.Size = new System.Drawing.Size(48, 23); this.lblAttachNumber.TabIndex = 14; this.lblAttachNumber.Text = "#"; this.lblAttachNumber.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // oFDAttach // this.oFDAttach.Title = "Выбор вложения"; // // contextMenuDeleteAttach // this.contextMenuDeleteAttach.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.itemDeleteAttach}); // // itemDeleteAttach // this.itemDeleteAttach.Index = 0; this.itemDeleteAttach.Text = "Удалить"; this.itemDeleteAttach.Click += new System.EventHandler(this.itemDeleteAttach_Click); // // SendMessage // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(744, 478); this.Controls.Add(this.btnAddAttach); this.Controls.Add(this.btnViewAttach); this.Controls.Add(this.txbAttach); this.Controls.Add(this.txbBody); this.Controls.Add(this.txbSubject); this.Controls.Add(this.txbBlindCopy); this.Controls.Add(this.txbCopy); this.Controls.Add(this.txbTo); this.Controls.Add(this.lblAttachNumber); this.Controls.Add(this.lblAttachments); this.Controls.Add(this.pblAttachments); this.Controls.Add(this.btnSend); this.Controls.Add(this.lblBody); this.Controls.Add(this.lblSubject); this.Controls.Add(this.lblBlindCopy); this.Controls.Add(this.lblCopy); this.Controls.Add(this.lblTo); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "SendMessage"; this.Text = "Отправка сообщения"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.ResumeLayout(false);
} #endregion
private void btnViewAttach_Click(object sender, System.EventArgs e) { if(oFDAttach.ShowDialog() != DialogResult.OK) return; txbAttach.Text = oFDAttach.FileName; }
private void btnAddAttach_Click(object sender, System.EventArgs e) { MailAttachment attach = new MailAttachment(oFDAttach.FileName); message.Attachments.Add(attach); txbAttach.Text = ""; oFDAttach.FileName = String.Empty; this.AddAttachmentsToPanel(); }
private void AddAttachmentsToPanel() { pblAttachments.Controls.Clear(); for(int i = message.Attachments.Count -1; i >= 0; i--) { MailAttachment attach = (MailAttachment)message.Attachments[i]; Label lblNumber = new Label(); Label lblAttachName = new Label(); lblNumber.Text = String.Format("#{0}", i + 1); lblAttachName.Text = attach.Filename; lblAttachName.TextAlign = lblNumber.TextAlign = ContentAlignment.MiddleLeft; lblAttachName.Anchor = lblNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left; lblNumber.Location = new Point(15, i*25); lblAttachName.Location = new Point(50, i*25); lblNumber.Size = new Size(20, 20); lblAttachName.Size = new Size(500, 20); lblNumber.ContextMenu = lblAttachName.ContextMenu = contextMenuDeleteAttach; lblNumber.Tag = lblAttachName.Tag = i; pblAttachments.Controls.Add(lblNumber); pblAttachments.Controls.Add(lblAttachName); } }
private void itemDeleteAttach_Click(object sender, System.EventArgs e) { MenuItem item = (MenuItem)sender; ContextMenu menu = (ContextMenu)item.Parent; Label source = (Label)menu.SourceControl; object o = source.Tag; int i = Int32.Parse(o.ToString()); message.Attachments.RemoveAt(i); this.AddAttachmentsToPanel(); }
private void btnSend_Click(object sender, System.EventArgs e) { message.BodyFormat = MailFormat.Text; message.Body = txbBody.Text; message.Cc = txbCopy.Text; message.Bcc = txbBlindCopy.Text; message.Subject = txbSubject.Text; message.To = txbTo.Text; message.From = ((UserIdentity)Thread.CurrentPrincipal.Identity).Mail; Mail.MailSender mailSender = new Mail.MailSender(((UserIdentity)Thread.CurrentPrincipal.Identity).Smtp); mailSender.Send(message, ((UserIdentity)Thread.CurrentPrincipal.Identity).Password); MessageBox.Show("Ваше сообщение отправлено."); this.Close(); } } }
Листинг 3.26.
Закрыть окно
Содержание  Назад  Вперед