Search code examples
asp.net-mvcwindows-authentication

How to retrieve current logged in user credentials and use to send an email


I have a function for sending email.

My requirement is, I need to send the email using the credentials of currently logged in windows user (Active Directory).

I have used this code.

 public ActionResult Mail2()
    {

        string From = "[email protected]";
        string To = Request.Form["TotxtBox"];
        string CC = Request.Form["CCtxtBox"];
        string Subject = Request.Form["Subject"];
        string body = Request.Form["BodytxtArea"];

        MailMessage objMailMessage = new MailMessage(From,To);
        objMailMessage.Subject = Subject;
        objMailMessage.Body = body;
        objMailMessage.IsBodyHtml = false;

        SmtpClient objSmtpClient = new SmtpClient();
        objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        objSmtpClient.Host = "[email protected]";
        objSmtpClient.Port = 25;
        objSmtpClient.UseDefaultCredentials = false;
        System.Net.NetworkCredential credential = new System.Net.NetworkCredential("Username", "Password");
        objSmtpClient.Credentials = credential;
        objSmtpClient.Send(objMailMessage);
        Return view();
}

I just want to pass currently logged in user's windows username and password in the given field. Is there a way to retrieve those. Can anyone Help?


Solution

  • Why don't you just put the SMTP details in your web.config file like so:

      <system.net>
        <mailSettings>
          <smtp from="[email protected]">
                <!--<network host="smtp.gmail.com" port="587" enableSsl="true" userName="[email protected]" password="Abc.1234" />-->
    
                <network host="[email protected]" port="25" userName="" password="" />
          </smtp>
        </mailSettings>
      </system.net>
    

    And then from code, you can just use the SmtpClient class to send the email. The code may look like:

    private void SendMessage(string emailId, string subject, string body)
    {
        var msg = new MailMessage();
        msg.To.Add(new MailAddress(emailId));
        msg.Subject = subject;
        msg.IsBodyHtml = true;
        msg.Body = HttpUtility.HtmlDecode(body);
        smtpClient.Send(msg);
    }
    

    You may want to use a google account for testing and once email testing is done, switch to your company server details as shown in above web.config.

    Update

    OP says

    I want who ever login to this software, their system username password will go instead of this unique id and password.

    You can get username using WindowsIdentity.GetCurrent().Name but not password. You will have to ask application user to provide one. In that case you will have to capture the username and password while they login and use it in your Mail2() method. Whoever calling this method should supply that information.

    I am not sure if this is the route you should go. The email should go from logged in user email ID but the SMTP account details typically stays the same. You care retrieve email ID of current logged in user as suggested in this question.