-1

I have created a webform in visual studio. This application is sending an email. BUT I want the FROM address to be the logged in user's email. So for example, if John is logged in with his email and password and he clicks submit, the email will send to Person X but the from address will come from John. I hope this is a clear enough explanation.

This is the code snippet of my sending mail method.

        {
        MailMessage message = new MailMessage();
        message.To.Add(nameddl.Text);
        message.From = new MailAddress("");
        message.Subject = "Notification";
        message.Body = "An new entry has been made that requires your attention.";
        message.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        }

So this line, message.From = new MailAddress(User.LoggedIn); this is what I want. BUT how do one code it to work like that, can it be made possible?

NB: This is NOT a MVC Application, just a regular ASP.NET Web form.

**EDIT Still struggling with this one, so if anyone got an idea that would be great?

starcatcher
  • 9
  • 2
  • 8
  • possible duplicate of [How to send email in asp.net C#](http://stackoverflow.com/questions/18326738/how-to-send-email-in-asp-net-c-sharp) – JimMSDN Jul 11 '14 at 12:45
  • @JimMSDN Nope, it's not the same. I'm trying to use a logged in User's email for a FROM Address. – starcatcher Jul 11 '14 at 12:59
  • Maybe I'm missing the problem - what's preventing you from simply setting the `MailMessage's` .From property to the e-mail address and display name of the user that's logged in (wherever you are storing that) using a new `MailAddress`? – JimMSDN Jul 11 '14 at 13:27
  • Ala `message.From = new MailAddress("x@y.com","John Doe");` – JimMSDN Jul 11 '14 at 13:32
  • That works fine. I've done it. But when different users are logged in and start submitting? It needs to be universal, it needs to work for all the registered users. Again how would you display this in code??? Plus the info are stored in a SQL database – starcatcher Jul 11 '14 at 13:58
  • Found my answer: http://stackoverflow.com/questions/25014630/use-email-address-that-is-stored-in-sql-table-to-use-in-difference-aspx-pages – starcatcher Aug 07 '14 at 10:40

1 Answers1

0

If you are storing email in session then you can directly pass it like.

message.From = new MailAddress(Session["Email"].Tostring);

if this is not the case and you have stored email in database then you can get it from there by simply making one method that return email address.like message.From = GetEmail();

Krunal Sisodiya
  • 11
  • 3
  • 16
  • I used both the suggestions - not going so well. In the end I created a GetEmail() method, however when try using it as message.From = GetEmail() I get an Error saying "cannot implicitly convert method ' void' to System.Net.Mail.MailAddress." Any ideas? – starcatcher Jul 11 '14 at 12:00