Send an email where sender address is pulled from database

118 Views Asked by At

I am working on a simple application where a user will authenticate and after authentication, the page will display current course information for that user. They use a form to send selected information in an email. What I am attempting to do is have the mail.From pull the email address from a database. I have looked in forums and tried a few things out. I am stuck. What I have tried is below.

[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("[email protected]");
        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;
    }
}

And this:

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

There are 1 best solutions below

4
On BEST ANSWER

I believe that with this line, in fact, I'm not sure how this line is even compiling, is Email_Addr a bool?:

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

You are trying to get the first person in the database and then select their e-mail address. All you're actually doing is selecting the People object.

Try this instead:

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