MailMessage不工作,甚至与system.net.mail引用

本文关键字:system net mail 引用 工作 MailMessage | 更新日期: 2023-09-27 17:51:19

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

MailMessage不工作,甚至与system.net.mail引用

问题在于你引用了

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

所以在System.Web.Mail.MailMessageSystem.Net.Mail.MailMessage之间存在模糊的参考

删除using System.Web.Mail;引用