目录
实践过程
效果
代码
public partial class frmSend : Form
{
public frmSend()
{
InitializeComponent();
}
private static string BaseEncode(string str)
{
return Convert.ToBaseString(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 = BaseEncode(txtSubject.Text);
message.Body = BaseEncode(txtContent.Text);
if (txtAttachment.Text != "")
{
if (txtAttachment.Text.IndexOf(",") !=)
{
string[] strAttachment = txtAttachment.Text.Split(',');
for (int i =; i < strAttachment.Length; i++)
{
AddFile(strAttachment[i], message);
}
}
else
{
AddFile(txtAttachment.Text, message);
}
}
SmtpClient client = new SmtpClient(txtServer.Text, Convert.ToInt(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(",") != -)
{
string[] strEmail = txtTo.Text.Split(',');
string sumEmail = "";
for (int i =; 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
public static bool validateEmail(string str)
{
return Regex.IsMatch(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
#endregion
}
partial class frmSend
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
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.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.btnSend = new System.Windows.Forms.Button();
this.txtAttachment = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.btnSelect = new System.Windows.Forms.Button();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
this.txtContent.Location = new System.Drawing.Point(, 143);
this.txtContent.Multiline = true;
this.txtContent.Name = "txtContent";
this.txtContent.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtContent.Size = new System.Drawing.Size(, 100);
this.txtContent.TabIndex =;
this.txtSubject.Location = new System.Drawing.Point(, 94);
this.txtSubject.Name = "txtSubject";
this.txtSubject.Size = new System.Drawing.Size(, 21);
this.txtSubject.TabIndex =;
this.txtPort.Location = new System.Drawing.Point(, 62);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(, 21);
this.txtPort.TabIndex =;
this.txtServer.Location = new System.Drawing.Point(, 63);
this.txtServer.Name = "txtServer";
this.txtServer.Size = new System.Drawing.Size(, 21);
this.txtServer.TabIndex =;
this.txtPwd.Location = new System.Drawing.Point(, 34);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(, 21);
this.txtPwd.TabIndex =;
this.txtName.Location = new System.Drawing.Point(, 35);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(, 21);
this.txtName.TabIndex =;
this.txtTo.Location = new System.Drawing.Point(, 6);
this.txtTo.Name = "txtTo";
this.txtTo.Size = new System.Drawing.Size(, 21);
this.txtTo.TabIndex =;
this.txtSend.Location = new System.Drawing.Point(, 6);
this.txtSend.Name = "txtSend";
this.txtSend.Size = new System.Drawing.Size(, 21);
this.txtSend.TabIndex =;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(15, 121);
this.label.Name = "label8";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 12;
this.label.Text = "邮件内容:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(15, 93);
this.label.Name = "label7";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 11;
this.label.Text = "邮件主题:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(290, 65);
this.label.Name = "label6";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 14;
this.label.Text = "端口号:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(21, 65);
this.label.Name = "label5";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 17;
this.label.Text = "服务器:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(278, 37);
this.label.Name = "label4";
this.label.Size = new System.Drawing.Size(77, 12);
this.label.TabIndex = 16;
this.label.Text = "发件人密码:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(9, 37);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(77, 12);
this.label.TabIndex = 15;
this.label.Text = "发件人名称:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(290, 9);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 13;
this.label.Text = "收件人:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(21, 9);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 18;
this.label.Text = "发件人:";
this.btnSend.Location = new System.Drawing.Point(, 280);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(, 23);
this.btnSend.TabIndex =;
this.btnSend.Text = "发送";
this.btnSend.UseVisualStyleBackColor = true;
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.txtAttachment.Location = new System.Drawing.Point(, 250);
this.txtAttachment.Name = "txtAttachment";
this.txtAttachment.Size = new System.Drawing.Size(, 21);
this.txtAttachment.TabIndex =;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(15, 253);
this.label.Name = "label9";
this.label.Size = new System.Drawing.Size(41, 12);
this.label.TabIndex = 27;
this.label.Text = "附件:";
this.btnSelect.Location = new System.Drawing.Point(, 249);
this.btnSelect.Name = "btnSelect";
this.btnSelect.Size = new System.Drawing.Size(, 23);
this.btnSelect.TabIndex =;
this.btnSelect.Text = "选择";
this.btnSelect.UseVisualStyleBackColor = true;
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
this.openFileDialog.FileName = "openFileDialog";
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 313);
this.Controls.Add(this.btnSelect);
this.Controls.Add(this.txtAttachment);
this.Controls.Add(this.label);
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.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
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 label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.TextBox txtAttachment;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button btnSelect;
private System.Windows.Forms.OpenFileDialog openFileDialog;
}