?? frmsendmail.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web;
using System.Web.Mail;
namespace SendMail
{
/// <summary>
/// Summary description for frmSendMail.
/// </summary>
public class frmSendMail : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnRemove;
private System.Windows.Forms.ListBox lstFiles;
private System.Windows.Forms.Label lblSMTP;
private System.Windows.Forms.Label lblSubject;
private System.Windows.Forms.Label lblBcc;
private System.Windows.Forms.Label lblCc;
private System.Windows.Forms.Label lblTo;
private System.Windows.Forms.Label lblFrom;
private System.Windows.Forms.TextBox txtSubject;
private System.Windows.Forms.TextBox txtBcc;
private System.Windows.Forms.TextBox txtCc;
private System.Windows.Forms.TextBox txtTo;
private System.Windows.Forms.TextBox txtFrom;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Button txtAttach;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.RichTextBox txtBody;
private System.Windows.Forms.OpenFileDialog ofdAttach;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
//Create a message object to contain a complete email.
private MailMessage message;
public frmSendMail()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
//Initialize the message object
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()
{
this.btnRemove = new System.Windows.Forms.Button();
this.lstFiles = new System.Windows.Forms.ListBox();
this.lblSMTP = new System.Windows.Forms.Label();
this.lblSubject = new System.Windows.Forms.Label();
this.lblBcc = new System.Windows.Forms.Label();
this.lblCc = new System.Windows.Forms.Label();
this.lblTo = new System.Windows.Forms.Label();
this.lblFrom = new System.Windows.Forms.Label();
this.txtSubject = new System.Windows.Forms.TextBox();
this.txtBcc = new System.Windows.Forms.TextBox();
this.txtCc = new System.Windows.Forms.TextBox();
this.txtTo = new System.Windows.Forms.TextBox();
this.txtFrom = new System.Windows.Forms.TextBox();
this.txtIP = new System.Windows.Forms.TextBox();
this.txtAttach = new System.Windows.Forms.Button();
this.btnSend = new System.Windows.Forms.Button();
this.txtBody = new System.Windows.Forms.RichTextBox();
this.ofdAttach = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// btnRemove
//
this.btnRemove.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnRemove.Location = new System.Drawing.Point(470, 422);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(77, 26);
this.btnRemove.TabIndex = 37;
this.btnRemove.Text = "刪除(&R)";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// lstFiles
//
this.lstFiles.ItemHeight = 12;
this.lstFiles.Location = new System.Drawing.Point(86, 388);
this.lstFiles.Name = "lstFiles";
this.lstFiles.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.lstFiles.Size = new System.Drawing.Size(375, 52);
this.lstFiles.TabIndex = 36;
//
// lblSMTP
//
this.lblSMTP.AutoSize = true;
this.lblSMTP.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblSMTP.Location = new System.Drawing.Point(269, 19);
this.lblSMTP.Name = "lblSMTP";
this.lblSMTP.Size = new System.Drawing.Size(77, 16);
this.lblSMTP.TabIndex = 30;
this.lblSMTP.Text = "SMTP 服務器";
//
// lblSubject
//
this.lblSubject.AutoSize = true;
this.lblSubject.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblSubject.Location = new System.Drawing.Point(24, 123);
this.lblSubject.Name = "lblSubject";
this.lblSubject.Size = new System.Drawing.Size(29, 16);
this.lblSubject.TabIndex = 29;
this.lblSubject.Text = "主題";
//
// lblBcc
//
this.lblBcc.AutoSize = true;
this.lblBcc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblBcc.Location = new System.Drawing.Point(24, 97);
this.lblBcc.Name = "lblBcc";
this.lblBcc.Size = new System.Drawing.Size(54, 16);
this.lblBcc.TabIndex = 28;
this.lblBcc.Text = "密件抄送";
//
// lblCc
//
this.lblCc.AutoSize = true;
this.lblCc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblCc.Location = new System.Drawing.Point(24, 71);
this.lblCc.Name = "lblCc";
this.lblCc.Size = new System.Drawing.Size(29, 16);
this.lblCc.TabIndex = 27;
this.lblCc.Text = "抄送";
//
// lblTo
//
this.lblTo.AutoSize = true;
this.lblTo.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblTo.Location = new System.Drawing.Point(24, 45);
this.lblTo.Name = "lblTo";
this.lblTo.Size = new System.Drawing.Size(42, 16);
this.lblTo.TabIndex = 26;
this.lblTo.Text = "收件人";
//
// lblFrom
//
this.lblFrom.AutoSize = true;
this.lblFrom.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblFrom.Location = new System.Drawing.Point(24, 19);
this.lblFrom.Name = "lblFrom";
this.lblFrom.Size = new System.Drawing.Size(42, 16);
this.lblFrom.TabIndex = 25;
this.lblFrom.Text = "發件人";
//
// txtSubject
//
this.txtSubject.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtSubject.Location = new System.Drawing.Point(86, 121);
this.txtSubject.Name = "txtSubject";
this.txtSubject.Size = new System.Drawing.Size(461, 20);
this.txtSubject.TabIndex = 24;
this.txtSubject.Text = "";
//
// txtBcc
//
this.txtBcc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtBcc.Location = new System.Drawing.Point(86, 95);
this.txtBcc.Name = "txtBcc";
this.txtBcc.Size = new System.Drawing.Size(461, 20);
this.txtBcc.TabIndex = 23;
this.txtBcc.Text = "";
//
// txtCc
//
this.txtCc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtCc.Location = new System.Drawing.Point(86, 69);
this.txtCc.Name = "txtCc";
this.txtCc.Size = new System.Drawing.Size(461, 20);
this.txtCc.TabIndex = 22;
this.txtCc.Text = "";
//
// txtTo
//
this.txtTo.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtTo.Location = new System.Drawing.Point(86, 43);
this.txtTo.Name = "txtTo";
this.txtTo.Size = new System.Drawing.Size(461, 20);
this.txtTo.TabIndex = 21;
this.txtTo.Text = "";
//
// txtFrom
//
this.txtFrom.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtFrom.Location = new System.Drawing.Point(86, 17);
this.txtFrom.Name = "txtFrom";
this.txtFrom.Size = new System.Drawing.Size(173, 20);
this.txtFrom.TabIndex = 20;
this.txtFrom.Text = "";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(365, 17);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(182, 21);
this.txtIP.TabIndex = 19;
this.txtIP.Text = "";
//
// txtAttach
//
this.txtAttach.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtAttach.Location = new System.Drawing.Point(470, 388);
this.txtAttach.Name = "txtAttach";
this.txtAttach.Size = new System.Drawing.Size(77, 26);
this.txtAttach.TabIndex = 33;
this.txtAttach.Text = "附件(&A)";
this.txtAttach.Click += new System.EventHandler(this.txtAttach_Click);
//
// btnSend
//
this.btnSend.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnSend.Location = new System.Drawing.Point(86, 457);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(77, 25);
this.btnSend.TabIndex = 32;
this.btnSend.Text = "發送(&S)";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// txtBody
//
this.txtBody.Location = new System.Drawing.Point(86, 155);
this.txtBody.Name = "txtBody";
this.txtBody.Size = new System.Drawing.Size(461, 224);
this.txtBody.TabIndex = 31;
this.txtBody.Text = "";
//
// ofdAttach
//
this.ofdAttach.Multiselect = true;
//
// frmSendMail
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(556, 487);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.lstFiles);
this.Controls.Add(this.lblSMTP);
this.Controls.Add(this.lblSubject);
this.Controls.Add(this.lblBcc);
this.Controls.Add(this.lblCc);
this.Controls.Add(this.lblTo);
this.Controls.Add(this.lblFrom);
this.Controls.Add(this.txtSubject);
this.Controls.Add(this.txtBcc);
this.Controls.Add(this.txtCc);
this.Controls.Add(this.txtTo);
this.Controls.Add(this.txtFrom);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.txtAttach);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.txtBody);
this.Name = "frmSendMail";
this.Text = "發送郵件";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmSendMail());
}
//Compose the email and then send it
private void btnSend_Click(object sender, System.EventArgs e)
{
//Check if SMTP Server IP address is specified.
//If yes, compose email
//else display appropriate message
if (txtIP.Text.Length >0)
{
//Specify the SMTP server to use
//to send emails
SmtpMail.SmtpServer =txtIP.Text.Trim() ;
//Compose email
message.From =txtFrom.Text ;
message.To =txtTo.Text ;
message.Cc =txtCc.Text ;
message.Bcc =txtBcc.Text ;
message.Subject =txtSubject.Text ;
message.Body=txtBody.Text;
//Add attachements to the email if any
message.Attachments.Clear();
for(int index=0;index<lstFiles.Items.Count;index++)
{
MailAttachment mAttach=new MailAttachment(lstFiles.Items[index].ToString());
message.Attachments.Add(mAttach);
}
try
{
MessageBox.Show (message.From.ToString());
MessageBox.Show (message.To.ToString());
MessageBox.Show (message.Subject.ToString());
//Send the composed email
SmtpMail.Send(message.From,message.To,message.Subject,message.Body);
//Reset the form
txtFrom.Text="";
txtTo.Text="";
txtCc.Text="";
txtBcc.Text="";
txtSubject.Text="";
txtBody.Text="";
lstFiles.Items.Clear();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message.ToString() );
MessageBox.Show(excep.StackTrace.ToString() );
//Display appropriate message if
//From address is invalid
//MessageBox.Show("Enter a valid From email address");
return;
}
//Visually indicate that the email was sent
MessageBox.Show("您的郵件已發送");
}
else
{
MessageBox.Show("請輸入 SMTP 服務器 IP 地址");
}
}
private void txtAttach_Click(object sender, System.EventArgs e)
{
//Display a dialog box to select
//files to attach to the email.
//Once selected, display the selected
//files in a listbox
if(ofdAttach.ShowDialog()==DialogResult.OK)
{
string [] files=new string[ofdAttach.FileNames.Length];
for(int index=0;index<ofdAttach.FileNames.Length;index++)
{
lstFiles.Items.Add(ofdAttach.FileNames[index].ToString());
}
}
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
//Remove the selected attachment from the list
lstFiles.Items.RemoveAt(lstFiles.SelectedIndex);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -