SMTP setup is timing out the connection

21 Views Asked by At

I have added this functionality of taking response from Contact form and sent it to the email I have provided and then again send the email to the client of thankyou but when I am trying to send the email its taking too much time like 2 minutes and at last it is showing me that the connection has been timed out. As a beginner this my first project to add such functionality so please assist me how can i overcome this issue?

using BlueDot_Website.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace BlueDot_Website.Controllers
{
    public class ContactController : Controller
    {
        [HttpPost]
        public ActionResult SendEmail(ContactViewModel model)
        {
            // Send email to business email address
            SendBusinessEmail(model);

            // Send thank you email to the customer
            SendThankYouEmail(model);

            // Optionally, you can return a different view to display a confirmation message
            return RedirectToAction("Index", "Home");
        }

        private void SendBusinessEmail(ContactViewModel model)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("[email protected]"); // Replace with your business email address
            message.To.Add("[email protected]"); // Replace with your business email address
            message.Subject = "New Contact Form Submission";
            message.Body = $"Name: {model.Name}\r\nEmail: {model.Email}\r\nContact Number: {model.ContactNumber}\r\nCompany: {model.Company}\r\nAddress: {model.Address}\r\nCity: {model.City}\r\nCountry: {model.Country}\r\nSubject: {model.Subject}\r\nMessage: {model.Message}";

            // Configure your SMTP settings
            string smtpServer = "mail.bluedotme.com";
            int smtpPort = 465; // Change to the appropriate port for your SMTP server
            string username = "[email protected]"; // Replace with your email address
            string password = "*********"; // Replace with your email password

            // Create an instance of SmtpClient
            using (SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort))
            {
                smtpClient.EnableSsl = true; // Enable SSL/TLS
                smtpClient.UseDefaultCredentials = false; // Disable default credentials

                // Set your authentication details
                smtpClient.Credentials = new NetworkCredential(username, password);

                // Send the email
                smtpClient.Send(message);
            }
        }


        private void SendThankYouEmail(ContactViewModel model)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("[email protected]"); // Replace with your business email address
            message.To.Add(model.Email);
            message.Subject = "Thank You for Contacting Us";
            message.Body = "Thank you for contacting us. We will get back to you soon.";

           // Configure your SMTP settings
            string smtpServer = "smtpout.secureserver.net";
            int smtpPort = 465; // Change to the appropriate port for your SMTP server
            string username = "[email protected]"; // Replace with your email address
            string password = "*********"; // Replace with your email password

            // Create an instance of SmtpClient
            using (SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort))
            {
                smtpClient.EnableSsl = true; // Enable SSL/TLS
                smtpClient.UseDefaultCredentials = false; // Disable default credentials

                // Set your authentication details
                smtpClient.Credentials = new NetworkCredential(username, password);

                // Send the email
                smtpClient.Send(message);
            }
        }
    }
}

I have added port settings as such:

Outgoing Server:mail.bluedotme.com
SMTP Port: 465

Please let me know how can I overcome such an issue?

I had tried to do the best as in already posted response but I still cant figure out the problem

0

There are 0 best solutions below