1 public bool SendMail()
2 {
3 MailMessage myEmail = new MailMessage();
4 myEmail.From = new MailAddress(fromMail.Text.Trim());
5 myEmail.To.Add(toMail.Text.Trim());
6 myEmail.Subject = subject.Text.Trim();
7 myEmail.IsBodyHtml = format.SelectedItem.Value == "0" ? false : true;
8 //附件
9 string ServerFileName = "";
10 if (this.upfile.PostedFile.ContentLength != 0)
11 {
12 string upFileName = this.upfile.PostedFile.FileName;
13 string[] strTemp = upFileName.Split('.');
14 string upFileExp = strTemp[strTemp.Length - 1].ToString();
15 ServerFileName = Server.MapPath(@"/upload/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + upFileExp);
16 this.upfile.PostedFile.SaveAs(ServerFileName);
17 myEmail.Attachments.Add(new Attachment(ServerFileName));
18
19 }
20
21 myEmail.Body = body.Text.Trim();
22 myEmail.BodyEncoding = Encoding.UTF8;
23 myEmail.Priority = MailPriority.High;
24 SmtpClient smtp = new SmtpClient();
25 smtp.Credentials = new NetworkCredential("daishuguang4461@126.com", "12619890219");
26 smtp.Port = 25;
27 smtp.Host = "smtp.126.com";
28 smtp.EnableSsl = false;
29 try
30 {
31 smtp.Send(myEmail);
32 myEmail.Dispose();
33 File.Delete(ServerFileName);
34 }
35 catch (Exception e)
36 {
37 myEmail.Dispose();
38 File.Delete(ServerFileName);
39 throw;
40 }
41 return true;
42 }
需要释放资源。http://bbs.csdn.net/topics/390074326