Default2.aspx页面上的代码<table align="center" border="0" cellpadding="0" cellspacing="0" width="776"> <tr> <td> <table align="center" border="0" cellpadding="4" cellspacing="1" width="600" bgcolor="#cccccc"> <tr> <td colspan="2" bgcolor="#f0f0f0" align="center"> 电子邮件发送测试程序</td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px">发送人:</td> <td bgcolor="#ffffff" align="left"><asp:TextBox ID="fromMail" runat="server" Width="300" /></td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px; height: 32px">收件人:</td> <td bgcolor="#ffffff" align="left" style="height: 32px"><asp:TextBox ID="toMail" runat="server" Width="300" /></td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px">主 题:</td> <td bgcolor="#ffffff" align="left"><asp:TextBox ID="subject" runat="server" Width="300" /></td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px">附 件:</td> <td bgcolor="#ffffff" align="left"><input type="file" id="upfile" runat="server" style="width: 297px" /></td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px">内 容:</td> <td bgcolor="#ffffff" align="left"><asp:TextBox ID="body" TextMode="multiLine" runat="server" Width="500px" Height="200" /></td> </tr> <tr> <td bgcolor="#f0f0f0" align="right" style="width: 73px">格 式:</td> <td bgcolor="#ffffff" align="left"><asp:RadioButtonList ID="format" runat="server"/></td> </tr> <tr> <td bgcolor="#f0f0f0" colspan="2" align="center"> <asp:Button ID="send" runat="server" Text="发送" OnClick="send_Click" /> <asp:Button ID="reset" runat="server" Text="重置" /> </td> </tr> </table> </td> </tr> </table> CS文件: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { format.Items.Add(new ListItem("文本", "0")); format.Items.Add(new ListItem("HTML", "1")); format.Items[0].Selected = true; fromMail.Text = "gguozhenqian@sina.com"; //发送方邮件 fromMail.Enabled = false; } } public bool SendMail() { MailMessage myEmail = new MailMessage(); myEmail.From = new MailAddress(fromMail.Text.Trim()); myEmail.To.Add(toMail.Text.Trim()); myEmail.Subject = subject.Text.Trim(); myEmail.IsBodyHtml = format.SelectedItem.Value == "0" ? false : true; //附件 string ServerFileName = ""; if (this.upfile.PostedFile.ContentLength != 0) { string upFileName = this.upfile.PostedFile.FileName; string[] strTemp = upFileName.Split('.'); string upFileExp = strTemp[strTemp.Length - 1].ToString(); ServerFileName = Server.MapPath("upload/"+DateTime.Now.ToString("yyyyMMddHHmmss") + "." + upFileExp); this.upfile.PostedFile.SaveAs(ServerFileName); myEmail.Attachments.Add(new Attachment(ServerFileName)); } myEmail.Body = body.Text.Trim(); myEmail.BodyEncoding = Encoding.UTF8; myEmail.Priority = MailPriority.High; SmtpClient smtp = new SmtpClient(); smtp.Credentials = new NetworkCredential("gguozhenqian@sina.com", "邮件密码"); smtp.Port = 25; smtp.Host = "smtp.sina.com"; smtp.EnableSsl = false; try { smtp.Send(myEmail); File.Delete(ServerFileName); } catch (Exception e) { File.Delete(ServerFileName); throw; } return true; } protected void send_Click(object obj, EventArgs e) { bool flag = SendMail(); if (flag == true) { Response.Write("<mce:script type="text/javascript"><!-- alert('发送成功!'); // --></mce:script>"); } else { Response.Write("<mce:script type="text/javascript"><!-- alert('发送失败!'); // --></mce:script>"); } }