Diamond Stone
Diamond Stone

Reputation: 115

How to send email to current logged in user

I am storing the email of the user in session
var v = //login query
Session["LoggedUserEmail"] = v.email.ToString();

Then after login I want to send an email to the current logged in user and for that purpose I am passing Session["LoggedUserEmail"] in Msg.To.Add but its not working.
This is what I am doing

 public void Execute(IJobExecutionContext context)
        {

            System.Net.Mail.MailMessage Msg = new System.Net.Mail.MailMessage();

            Msg.From = new MailAddress("[email protected]");

            Msg.To.Add(Session["LoggedUserEmail"].ToString());
            Msg.Subject = "Email";
            Msg.Body = "Hi";
            Msg.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587; 
            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxxxx");
            smtp.EnableSsl = true; 
            smtp.Send(Msg);
            Response.Write("Email Sent");
        }

Am I doing something wrong? if yes, then is there any other way to get the job done?

I am using quartz.net and implemented IJob interface in my mvc controller.

Upvotes: 0

Views: 472

Answers (1)

jvilalta
jvilalta

Reputation: 6799

The job executes on a different thread than the one that schedules it. You must pass in any data you need by using the JobDataMap when creating/scheduling the job.

Upvotes: 1

Related Questions