即使使用system.net.mail引用,MailMessage也无法正常工作

时间:2014-03-21 08:23:03

标签: c# asp.net

MailMessage无法正常工作我不知道自己错过了什么。如果缺少代码或我使用了错误的命名空间,请更正我的程序吗?

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;
using System.Web.Mail;
using System.Data.SqlClient;
using System.Data;

protected void btnsend_Click(object sender, EventArgs e)
{
   try
   {
      SmtpClient smtp=new SmtpClient("smtp.gmail.com",587);
      smtp.EnableSsl = true;
      smtp.UseDefaultCredentials = false;
      smtp.Credentials=new NetworkCredential(txtemail.Text,txtpassword.Text);
      MailMessage mail = new MailMessage(txtemail.Text, ddlemail.Text, txtsubject.Text, txtbody.Text);
      smtp.Send(mail);
   }
   catch (Exception ex)
   {
      Console.WriteLine("{0} Exception caught.", ex);
   }
}

1 个答案:

答案 0 :(得分:2)

问题是因为你引用了

using System.Net.Mail;
using System.Web.Mail;

因此System.Web.Mail.MailMessageSystem.Net.Mail.MailMessage

之间存在含糊不清的引用

删除using System.Web.Mail;参考