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?

1
  • 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' ? Commented Sep 11, 2016 at 6:32

1 Answer 1

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", "[email protected]")); emailMessage.To.Add(new MailboxAddress("myname", "[email protected]")); 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

Sign up to request clarification or add additional context in comments.

4 Comments

This is for a "contact me" page though, so wouldn't I want the "to" section to go to me?
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.
Awesome, thanks. So by having a host-server do you mean something like having a local server on a raspberry pi or similar?
If you have a server , connected to Internet and known as server , you can use as email server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.