I'm new to web programming and I am making a website using ASP.NET Core. I am trying to create a standard "contact me" page where the user enters in a name, email, subject and message. ASP.NET Core has yet to have System.Net.Mail
, so I can't use that.
I saw that MailKit could be used to send emails, but am unable to figure out how to use it for a contact page. I know using this code
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove ("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
I can send an email using my SMTP server, but obviously I want the functionality of users using the site to be able to send an email to me. Is there a way to use MailKit for this, or do I need to find another solution? Thanks.
Edit: This is the code I have that successfully sends an email, but it always says that it was sent from me to me.
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");
}