如何向当前登录用户发送电子邮件

时间:2015-08-16 18:07:59

标签: asp.net-mvc-4 quartz.net

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("abc@gmail.com");

            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("abc@gmail.com", "xxxxxxx");
            smtp.EnableSsl = true; 
            smtp.Send(Msg);
            Response.Write("Email Sent");
        }

我做错了吗?如果有,那么还有其他方法可以完成工作吗?

我正在使用quartz.net并在我的mvc控制器中实现了IJob接口。

1 个答案:

答案 0 :(得分:1)

作业在与调度作业不同的线程上执行。在创建/安排作业时,您必须使用JobDataMap传递所需的任何数据。