'Cannot load System.Web.HttpContext error with razor pages and WebMail
I've been having trouble getting WebMail to work sending an email using a contact form using This Official Microsoft Tutorial. My current issue is that I'm getting the error:
Could not load type 'System.Web.HttpContext' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
After searching online I'm finding a lot of answers saying that I simply cant use System.Web, HttpContext, or add the necessary assemblies which I find hard to believe considering I have it set up exactly as the tutorial provides.Any and all help is greatly appreciated.
My usings:
@using System.Web;
@using System.Web.Helpers;
My code which runs after submitting my form:
@{
ViewData["Title"] = "Email Confirmation";
var clientEmail = Request.Form["clientEmail"];
var clientName = Request.Form["clientName"];
var clientPhone = Request.Form["clientPhone"];
var clientMessage = Request.Form["clientMessage"];
var errorMessage = "";
var debuggingFlag = false;
try
{
// Initialize WebMail helper
WebMail.SmtpServer = "smtp.myserver.com";
WebMail.SmtpPort = 587;
WebMail.UserName = "MYEMAIL";
WebMail.Password = "MYPASSWORD";
WebMail.From = "MYEMAIL";
WebMail.EnableSsl = true;
// Send email
WebMail.Send(to: "MYEMAIL",
subject: $"Message from {clientName}",
body: $"{clientMessage}\nPhone: {clientPhone}\nEmail: {clientEmail}"
);
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
EDIT I've also tried adding the System.Web.dll assembly but I'm getting the message that The reference is invalid or unsupported Here is my csproj if it helps:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MYRAZORSITE</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="bootstrap" Version="5.1.3" />
<PackageReference Include="Microsoft.AspNet.WebPages" Version="3.2.8" />
</ItemGroup>
</Project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|