电子邮件不发送

时间:2015-01-11 10:21:35

标签: c# validation email smtp contact-form

我创建了一个联系表单,用户可以输入他/她的姓名和他们的电子邮件地址,除了单击提交按钮时我在某个时刻收到错误,一切正常,下面我突出显示了哪里那个错误是。我出于安全目的,我已经取出了密码。

 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";
        }

1 个答案:

答案 0 :(得分:0)

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

 mailMessage.Subject = "EmailSubject";

我也建议将“IsBodyHtml”设为false,因为这里的html代码可能有问题。因此,您应该为正文提供以下代码:

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