发送电子邮件,其中发件人地址从数据库中提取

时间:2015-06-25 16:12:21

标签: c# asp.net-mvc .net-4.0

我正在开发一个用户将进行身份验证的简单应用程序,在身份验证后,该页面将显示该用户的当前课程信息。他们使用表单在电子邮件中发送所选信息。我试图做的是拥有mail.From从数据库中提取电子邮件地址。我在论坛上看了一下并试了几件事。我被卡住了。我试过的是下面的内容。

[CAS.CasAuth]
public class HomeController : Controller
{
    private readonly PeopleEntities _db = new PeopleEntities();

    [Authentication.Authorize]
    public ActionResult Index()
    {
        var currentUser = Authentication.CurrentUser.user;
        var classes = _db.vw_People.ToList();

        return View(classes.Where(x => x.user == currentUser));
    }

    [HttpPost]
    public ActionResult Index(string[] courseid)
    {
        SendEmail(null, null, null, null, null, null);

        return View();
    }

    private bool SendEmail(string from, string to, string cc, string subject, string body)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("test@test.com");
        mail.From = new MailAddress(_db.vw_People.FirstOrDefault(f => f.Email_Addr.ToString()));
        mail.Subject = "Test Test test";
        mail.Body = "Test";

        SmtpClient smtp = new SmtpClient("smtp-settings", 25);
        // smtp.Host = "smtp-settings";
        //smtp.Port = 25;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("test", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);

        return true;
    }
}

而且:

     var sender = _db.vw_People.FirstOrDefault(s => s.Email_Addr);
     mail.From = new MailAddress(sender);

1 个答案:

答案 0 :(得分:0)

我相信,对于这一行,事实上,我不确定这一行是如何编译的,Email_Addrbool?:

_db.vw_People.FirstOrDefault(s => s.Email_Addr);

您正试图让数据库中的第一个人,然后选择他们的电子邮件地址。您实际所做的只是选择People对象。

请改为尝试:

var sender = _db.vw_People.First().Email_Addr