0

In my search for a way to send email on my webapp through a form, the only solution I got working was an old MSDN solution for a WebMail helper where it had me hardcode the credentials for an email to send through.

Right, off the bat, I know this is not ideal. Is there an easy way to send this mail out from a WebMail default so I can remove the login credentials and not look so amateurish???

@{
var customerName = Request["customerName"];
var customerEmail = Request["customerEmail"];
var customerPhone = Request["customerPhone"];
var customerRequest = Request["customerRequest"];
var errorMessage = "";
var toMail = "user@mail.com";
var debuggingFlag = false;
try
{
    // Initialize WebMail helper
    WebMail.SmtpServer = "smtp.gmail.com";
    WebMail.SmtpPort = 587;
    WebMail.UserName = "user@mail.com";
    WebMail.Password = "Password!";
    WebMail.From = "user@mail.com";
    WebMail.EnableSsl = true;

    // Send email
    WebMail.Send(to: toMail,
        subject: "Quote/Info request from - " + customerName,
        body: customerPhone + customerRequest
    );
}
catch (Exception ex)
{
    errorMessage = ex.Message;
}
Jeff Longo
  • 191
  • 1
  • 1
  • 13
  • A lot of info here: https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail?rq=1 – Daniel Stackenland Sep 21 '17 at 12:34
  • At first glance a lot of those solutions look a lot like mine and hardcode the credentials...There are a lot of solutions on there...Will have to dig through them and hopefully find what I am looking for. Thanks for your help. – Jeff Longo Sep 21 '17 at 12:43
  • what's wrong with authenticating with the mailserver, exactly? – ADyson Sep 21 '17 at 12:50

1 Answers1

1

See the mailSettings element of you web.config file.

For some reason the msdn page on the WebMail class recommends the _AppStart.cshtml file as a suitable place to configure these settings.

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
  • Perfect...so in essence I should be able to move these settings here and leave the WebMail.Send() where it is and it should work. Thanks! – Jeff Longo Sep 21 '17 at 12:59