I have created a web form that is meant to email information back to my email address when submitted. This works fine as expected when I run it locally but not after I upload it to the server. I’m using prositehosting to host my website so I can’t really configure any server settings. Is it a setting on the server that is blocking the emails from going through or is something missing from my code. Here’s some of the code:
default.aspx file (just using one input box for testing purposes)
<div id="contactForm">
<asp:TextBox ID="senderNameTextBox" placeholder="Name*" runat="server" Style="width: 250px;" ClientIDMode="Static" />
<asp:Button ID="Button1" type="button" Text="Submit →" runat="server" OnClick="sendMessage_Click"
Style="" />
</div>
default.aspx.cs file (I don’t even see any exceptions logged in the console on server but don’t receive any emails either) – emails been removed for privacy purposes
protected void sendMessage_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.CC.Add("[email protected]");
mail.Subject = "Customer - MedicalAuditsSoftware Website";
mail.Priority = MailPriority.Normal;
mail.Body = "<html><body>"
+ "Sender's Name: " + Server.HtmlEncode(senderNameTextBox.Text) +
"<br><br>";
mail.IsBodyHtml = true;
smtpClient.Send(mail);
Console.WriteLine("Email sent successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error sending email: " + ex.Message);
throw;
}
}
Tests tried on contact form:
- Trying with port 465 then port 993
- Logging errors in console
- Commenting out smtp details on web.config file (works locally but not on when uploaded)
- Putting all smtp details in web.config file instead of default.aspx.cs file – works locally but not when uploaded
web synergi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.