电子邮件未发送

本文关键字:电子邮件 | 更新日期: 2023-09-27 18:37:01

我创建了一个联系表单,用户可以输入他/她的姓名和电子邮件地址,除了单击提交按钮时我在某个时候遇到错误之外,一切都很好,下面我突出显示了该错误的位置。当然,出于安全考虑,我已经取出了密码。

 using System;
 using System.Net.Mail;
 namespace WhiteLabelConProject
 {
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("martinhall1390@gmail.com");
            mailMessage.To.Add("martinhall1390@gmail.com");
            mailMessage.Body = "<b>SenderName : </b>" + txtName.Text + "<br/>"
            + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>";
            mailMessage.IsBodyHtml = true;
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new     System.Net.NetworkCredential("martinhall1390@gmail.com","");
            **smtpClient.Send(mailMessage); <<<<<<<< ERROR WITH THIS BIT OF CODE**
            Label1.ForeColor = System.Drawing.Color.Blue;
            Label1.Text = "Thank you for registering";
            txtName.Enabled = false;
            txtEmail.Enabled = false;
        }
        catch(Exception ex)
        {
            //Log - Event Viewer or table
            Label1.ForeColor = System.Drawing.Color.Blue;
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "There is an unknown problem. Please try later";
        }

电子邮件未发送

我认为

这是因为代码没有主题,例如:

 mailMessage.Subject = "EmailSubject";

我还建议将"IsBodyHtml"设置为错误,因为这里的html代码可能存在错误。因此,您应该具有以下正文代码:

 mailMessage.Body = "Sender name  :" + txtName.Text + "'n Sender email  :" + txtEmail.Text;