0

I'm new to web programming and am using ASP.NET core to make a website. I'm trying to create a standard "contact me" page where the user enters in a name, email, subject and username. I'm using the MailKit library to send the emails:

public IActionResult SendEmail(Contact contact)
{
    var emailMessage = new MimeMessage();

    emailMessage.From.Add(new MailboxAddress(contact.Name, contact.Email));
    emailMessage.To.Add(new MailboxAddress("myname", "myemail"));
    emailMessage.Subject = contact.Subject;
    emailMessage.Body = new TextPart("plain") { Text = contact.Message };

    using (var client = new SmtpClient())
    {
        client.Connect("smtp-mail.outlook.com", 587); 
        client.Authenticate("myemail", "myemailpassword");
        client.Send(emailMessage);
        client.Disconnect(true);
    }
    return RedirectToAction("Index", "Home");
}

My issue is that whenever I send the email the SMTP server just replaces my "from" header with my SMTP account information. This seems to be the case with not just outlook, but with every major SMTP server I've tried, including gmail. Is there an SMTP server that will not have this issue, or do I need to find another way of sending the emails?

ecain
  • 1,282
  • 5
  • 23
  • 54
  • Spoofing the 'from' address seems like a security thing.. I'm not surprised. Check any public documentation for your email provider to see if they provide a way to accomplish it : is it so bad to include the email addr in the 'subject' field instead of 'from' ? – ABuckau Sep 11 '16 at 06:32

1 Answers1

1

First of all there is a problem with your code

    public IActionResult SendEmail(Contact contact)
    {
        var emailMessage = new MimeMessage();
        emailMessage.From.Add(new MailboxAddress("myname", "mymail@mail.com"));
        emailMessage.To.Add(new MailboxAddress("myname", "mymail@mail.com"));
        emailMessage.Subject = contact.Subject;
        emailMessage.Body = new TextPart("plain") 
        { 
         Text = String.Format("This visitor:{0} with this email:{1} Send this message:{2}", 
         contact.Name, contact.Email, contact.Message) 
        };

        using (var client = new SmtpClient())
        {
            client.Connect("smtp-mail.outlook.com", 587);
            client.Authenticate("myemail", "myemailpassword");
            client.Send(emailMessage);
            client.Disconnect(true);
        }
        return RedirectToAction("Index", "Home");
    }

Also visit the following question this may help you:

How to send an e-mail with C# through Gmail

Community
  • 1
  • 1
Eqbal Sohrabi
  • 826
  • 6
  • 23
  • This is for a "contact me" page though, so wouldn't I want the "to" section to go to me? – ecain Sep 11 '16 at 06:16
  • I edited my code above, if you have a host-server, you can simply use your host Email server instead of Gmail or other mail servers. – Eqbal Sohrabi Sep 11 '16 at 06:29
  • Awesome, thanks. So by having a host-server do you mean something like having a local server on a raspberry pi or similar? – ecain Sep 11 '16 at 06:42
  • If you have a server , connected to Internet and known as server , you can use as email server. – Eqbal Sohrabi Sep 11 '16 at 06:52