发送邮件
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = this.tbSmtpServer.Text.ToString(); //发送邮件所使用的Smtp事务的主机名称或IP地址
client.UseDefaultCredentials = false;
//NetworkCredential(发送者邮件地址,发送者邮箱密码)
client.Credentials = new System.Net.NetworkCredential(this.tbSender.Text.ToString(),
this.tbSenderPassWord.Text.ToString());
client.DeliveryMethod = SmtpDeliveryMethod.Network; //电子邮件通过网络发送到Smtp服务器
//MailMessage(发件人地址,收件人地址)
MailMessage message = new MailMessage(this.tbSender.Text.ToString(),
this.lbQuester.SelectedItem.ToString());
message.Subject = this.tbTitle.Text.ToString(); //邮件主题
message.Body = this.tbContext.Text.ToString(); //邮件内容
message.BodyEncoding = System.Text.Encoding.Default; //邮件正文的编码方式
message.IsBodyHtml = true;
//以下为附件处理过程
string attahmentName=this.tbAtrrac.Text.Trim().ToString();
if (!string.Equals(attahmentName, null) && !string.Equals(attahmentName, ""))
{
Attachment data = new Attachment(attahmentName, System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
//发送邮件
try
{
client.Send(message);
MessageBox.Show("E-Mail sucessfully send to:" + this.lbQuester.SelectedItem.ToString());
}
catch (Exception ex)
{
MessageBox.Show("E-Mail send fail!" + ex.ToString());
}