Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
5
Answers
send email using Javascript and MVC
Mark Tabor
5y
3.1k
1
Reply
I follow an article from code project but when i submit the form for email it says 404 bad request
below is my code I have method and model class in home controller as shown below .
[HttpPost]
public
ActionResult SendNewMessage()
{
try
{
Response.StatusCode = 200;
//getting useful configuration
string
smtpAddress = ConfigurationSMTP.smtpAdress;
//it can be a "smtp.office365.com" or whatever,
//it depends on smtp server of your sender email.
int
portNumber = ConfigurationSMTP.portNumber;
//Smtp port
bool
enableSSL = ConfigurationSMTP.enableSSL;
//SSL enable
string
emailTo = Request.Params[
"to"
];
string
subject = Request.Params[
"subject"
];
StringBuilder body =
new
StringBuilder();
//building the body of our email
body.Append(
"<html><head> </head><body>"
);
body.Append(
"<div style=' font-family: Arial; font-size: 14px; color: black;'>Hi,<br><br>"
);
body.Append(Request.Params[
"message"
]);
body.Append(
"</div><br>"
);
//Mail signature
body.Append(
string
.Format(
"<span style='font-size:11px;font-family: Arial;color:#40411E;'>{0} - {1} {2}</span><br>"
, MessageModel.adress, MessageModel.zip, MessageModel.city));
body.Append(
string
.Format(
"<span style='font-size:11px;font-family: Arial;color:#40411E;'>Mail: <a href=\"mailto:{0}\">{0}</a></span><br>"
, ConfigurationSMTP.from));
body.Append(
string
.Format(
"<span style='font-size:11px;font-family: Arial;color:#40411E;'>Tel: {0}</span><br>"
, MessageModel.phone));
body.Append(
string
.Format(
"<span style='font-size:11px;font-family: Arial;'><a href=\"web site\">{0}</a></span><br><br>"
, MessageModel.link));
body.Append(
string
.Format(
"<span style='font-size:11px; font-family: Arial;color:#40411E;'>{0}</span><br>"
, MessageModel.details));
body.Append(
"</body></html>"
);
using
(MailMessage mail =
new
MailMessage())
{
mail.From =
new
MailAddress(ConfigurationSMTP.from);
//destination adress
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body.ToString();
//set to true, to specify that we are sending html text.
mail.IsBodyHtml =
true
;
// Can set to false, if you are sending pure text.
string
localFileName =
"~/Content/TestAttachement.txt"
;
//to send a file in attachment.
mail.Attachments.Add(
new
Attachment(Server.MapPath(localFileName),
"application/pdf"
));
//Specify the smtp Server and port number to create a new instance of SmtpClient.
using
(SmtpClient smtp =
new
SmtpClient(smtpAddress, portNumber))
{
//passing the credentials for authentification
smtp.Credentials =
new
NetworkCredential(ConfigurationSMTP.from, ConfigurationSMTP.password);
//Authentification required
smtp.EnableSsl = enableSSL;
//sending email.
smtp.Send(mail);
}
}
}
catch
(Exception ex)
{
//Error response
Response.StatusCode = 400;
}
return
null
;
}
}
Model Class:
public
class
ConfigurationSMTP
{
//SMTP parameters
public
static
string
smtpAdress =
"smtp.gmail.com"
;
public
static
int
portNumber = 587;
public
static
bool
enableSSL =
true
;
//need it for the secured connection
public
static
string
from =
"
[email protected]
"
;
public
static
string
password =
"Pakistan_123"
;
}
public
class
MessageModel
{
public
static
string
adress =
"full adress of sender"
;
public
static
string
link =
"link for your website"
;
public
static
string
zip =
"90"
;
public
static
string
city =
"paris"
;
public
static
string
phone =
"(+33) 06 xxxxxxxx"
;
public
static
string
details =
"detail detail detail detail detail"
;
}
Index View:
@{
ViewBag.Title =
"Index"
;
}
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$(
"#idFormContact"
).on(
"submit"
,
function
(e) {
e.preventDefault();
//call external service
var
url =
"/Home/SendNewMessage"
;
var
formdata = (window.FormData) ?
new
FormData(
this
) :
null
;
var
fdata = (formdata !==
null
) ? formdata : $form.serialize();
$(
"#idSubmitMvt"
).attr(
"disabled"
,
true
);
$(
"#idNotifSuccess"
).hide();
$(
"#idNotifError"
).hide();
//get authorization keys.
$.ajax({
type:
"POST"
,
url: url,
data: fdata,
processData:
false
,
contentType:
false
,
success:
function
(data) {
$(
"#idNotifSuccess"
).show();
},
error:
function
(xhr, ajaxOptions, thrownError) {
console.log(
"Error"
);
$(
"#idNotifError"
).show();
}
});
});
});
</script>
<div
class
=
"row"
>
<h2>Contact Form</h2>
<form
class
=
"col col-xs-6"
id=
"idFormContact"
>
<div
class
=
"form-group"
>
<label>Destination</label>
<input type=
"email"
class
=
"form-control"
name=
"to"
value=
""
placeholder=
"Destination Email"
>
</div>
<div
class
=
"form-group"
>
<label>Subject</label>
<input type=
"text"
class
=
"form-control"
value=
"Test subject"
name=
"subject"
placeholder=
"Subject"
>
</div>
<div
class
=
"form-group"
>
<label>Body</label>
<textarea
class
=
"form-control"
name=
"message"
>Test Message</textarea>
</div>
<button type=
"submit"
class
=
"btn btn-primary"
>Submit</button>
<br>
<br>
<div id=
"idNotifError"
style=
"display:none"
class
=
"alert alert-danger"
>Fail to send a message</div>
<div id=
"idNotifSuccess"
style=
"display:none"
class
=
"alert alert-success"
>Message is sent</div>
</form>
</div>
Error screen
Post
Reset
Cancel
Answers (
5
)
Next Recommended Forum
Select, Add, Update, and Delete Data in a ASP.NET GridView
Pass string from Global.asax to Controller