前台页面 SendEmail.aspx 代码
using System.Net.Mail;
using System.Net;
<h2> 发送电子邮件演示 </h2> <table cellpadding="0" cellspacing="0" border="0" style="font-family: 宋体, Arial, Helvetica, sans-serif; font-size: 15px; width: 411px;"> <tr> <td class="style5"> 邮箱地址: </td> <td class="style6"> <asp:TextBox ID="tb_Email" runat="server" Width="269px"></asp:TextBox> </td> </tr> <tr> <td class="style5"> 抄送至: </td> <td class="style6"> <asp:TextBox ID="tb_cc" runat="server" Width="268px"></asp:TextBox> </td> </tr> <tr> <td class="style5"> 邮件主题: </td> <td class="style6"> <asp:TextBox ID="tb_Subject" runat="server" Width="268px"></asp:TextBox> </td> </tr> <tr> <td class="style5"> 邮件内容: </td> <td class="style6"> <asp:TextBox ID="tb_Body" runat="server" Height="63px" TextMode="MultiLine" Width="266px"></asp:TextBox> </td> </tr> <tr> <td class="style5"> 添加附件: </td> <td class="style6"> <asp:FileUpload ID="tb_Attachment" runat="server" Width="265px" /> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="btn_SendEmail" runat="server" Text="发送邮件" OnClick="btn_SendEmail_Click" /> </td> </tr> </table>
后台SendEmail.aspx.cs代码
protected void btn_SendEmail_Click(object sender, EventArgs e) { //声明一个Mail对象 MailMessage mymail = new MailMessage(); //发件人地址 //如是自己,在此输入自己的邮箱 mymail.From = new MailAddress("[email protected]"); //收件人地址 mymail.To.Add(new MailAddress(tb_Email.Text)); //邮件主题 mymail.Subject = tb_Subject.Text; //邮件标题编码 mymail.SubjectEncoding = System.Text.Encoding.UTF8; //发送邮件的内容 mymail.Body = tb_Body.Text; //邮件内容编码 mymail.BodyEncoding = System.Text.Encoding.UTF8; //添加附件 Attachment myfiles = new Attachment(tb_Attachment.PostedFile.FileName); mymail.Attachments.Add(myfiles); //抄送到其他邮箱 mymail.CC.Add(new MailAddress(tb_cc.Text)); //是否是HTML邮件 mymail.IsBodyHtml = true; //邮件优先级 mymail.Priority = MailPriority.High; //创建一个邮件服务器类 SmtpClient myclient = new SmtpClient(); myclient.Host = "SMTP.163.com"; //SMTP服务端口 myclient.Port = 25; //验证登录 myclient.Credentials = new NetworkCredential("@@@@@@", "*****");//"@"输入有效的邮件名, "*"输入有效的密码 myclient.Send(mymail); }
时间: 2024-10-15 15:04:14