电子邮件不在ASP.NET中发送

时间:2016-01-11 04:35:08

标签: c# asp.net email

请允许任何人在这里提供帮助:我有这个asp.net表单我试图发送电子邮件,这里是代码:

<system.net>
    <mailSettings>
    <smtp>
  <network host="smtp.iquo.com.ng" userName="williams@iquo.com.ng"    password="password />
  </smtp>
  </mailSettings>
</system.net>

有什么不对的,为什么邮件没有发送?另外,我把它放在:

''

按照服务器管理员的指示进入web.config文件。我该怎么办?

1 个答案:

答案 0 :(得分:-2)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using System.Net.Mail;

namespace asp_Semester_Project
{
    public partial class ContactUs : 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("ishahanbutt789@gmail.com");
                    mailMessage.To.Add("ishahanbutt789@gmail.com");
                    mailMessage.Subject = txtSubject.Text;
                    mailMessage.Body = "<b>Sender Name : </b>" + txtName.Text + "<br/>"
                        + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>"
                        + "<b>Comments : </b>" + txtComments.Text;
                    mailMessage.IsBodyHtml = true;


                    SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.EnableSsl = true;
                NetworkCredential nc = new NetworkCredential();
                nc.UserName = "ishahanbutt789@gmail.com";
                nc.Password = "******";
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = nc;
                smtpClient.Port = 587;
                    smtpClient.Send(mailMessage);
                    lblMessage.ForeColor = System.Drawing.Color.Blue;
                    lblMessage.Text = "Thank you for contacting us";

                    //txtName.Enabled = false;
                    //txtEmail.Enabled = false;
                    //txtComments.Enabled = false;
                    //txtSubject.Enabled = false;
                    //Button1.Enabled = false;
            }
            catch (Exception ex)
            {
                // Log the exception information to 
                // database table or event viewer
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.StackTrace;
            }
        }
    }
}