1、前台页面 SendEmail.aspx 代码
<h2> 发送电子邮件演示 </h2> <table cellpadding="0" cellspacing="0"> <tr> <td> 邮箱地址: </td> <td> <asp:TextBox runat="server"></asp:TextBox> </td> </tr> <tr> <td> 抄送至: </td> <td> <asp:TextBox runat="server"></asp:TextBox> </td> </tr> <tr> <td> 邮件主题: </td> <td> <asp:TextBox runat="server"></asp:TextBox> </td> </tr> <tr> <td> 邮件内容: </td> <td> <asp:TextBox runat="server" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr> <td> 添加附件: </td> <td> <asp:FileUpload runat="server" /> </td> </tr> <tr> <td colspan="2"> <asp:Button runat="server" Text="发送邮件" /> </td> </tr> </table>
2、后台SendEmail.aspx.cs代码
protected void btn_SendEmail_Click(object sender, EventArgs e) { //声明一个Mail对象 MailMessage mymail = new MailMessage(); //发件人地址 //如是自己,在此输入自己的邮箱 mymail.From = new MailAddress("15510180880@163.com"); //收件人地址 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); }
3、效果如下
3.1、如下图填入各项,点击发送邮件
3.2、163邮箱内
3.3、QQ邮箱内