简体   繁体   English

带有ASP.NET Core的“联系我们”页面-SMTP服务器问题

[英]“Contact us” page with ASP.NET Core - SMTP server issue

I'm new to web programming and am using ASP.NET core to make a website. 我是Web编程的新手,正在使用ASP.NET核心创建网站。 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: 我正在使用MailKit库发送电子邮件:

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. 我的问题是,每当我发送电子邮件时,SMTP服务器就会用我的SMTP帐户信息替换“发件人”标头。 This seems to be the case with not just outlook, but with every major SMTP server I've tried, including gmail. 不仅是Outlook,而且我尝试使用的每个主要SMTP服务器(包括gmail)似乎都是这种情况。 Is there an SMTP server that will not have this issue, or do I need to find another way of sending the emails? 是否有不会出现此问题的SMTP服务器,还是我需要找到另一种发送电子邮件的方式?

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 如何通过Gmail使用C#发送电子邮件

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

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