简体   繁体   English

MailKit可以用于“与我联系”表格吗?

[英]Can MailKit be used for “Contact Me” form?

I'm new to web programming and I am making a website using ASP.NET Core. 我是Web编程的新手,正在使用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. ASP.NET Core还没有System.Net.Mail ,所以我不能使用它。

I saw that MailKit could be used to send emails, but am unable to figure out how to use it for a contact page. 我看到MailKit可以用来发送电子邮件,但无法弄清楚如何将其用于联系页面。 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. 我可以使用SMTP服务器发送电子邮件,但是显然我希望使用该站点的用户的功能能够向我发送电子邮件。 Is there a way to use MailKit for this, or do I need to find another solution? 有没有办法使用MailKit来解决这个问题,还是我需要寻找其他解决方案? 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");
    }

Yes, MailKit can be used for this purpose. 是的,MailKit可以用于此目的。

The problem you are seeing is because outlook.com is replacing your From header with your email address (it's a "feature" of outlook.com when you login via SMTP). 您看到的问题是因为outlook.com用您的电子邮件地址替换了From标头(当您通过SMTP登录时,这是Outlook.com的“功能”)。

You might be able to sort of work around this by setting the ReplyTo to the user's address (so you can reply to them). 通过将ReplyTo设置为用户的地址,您也许可以解决此问题(以便您可以回复他们)。

Or you could try setting the From to the user's address and setting the Sender to your address (not sure if this will work). 或者,您可以尝试将“发件人”设置为用户的地址,然后将“发件人”设置为您的地址(不确定是否可以使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM