try { // Retrieve SMTP configuration from web.config string smtpHost = ConfigurationManager.AppSettings["SMTPHost"]; int smtpPort = int.Parse(ConfigurationManager.AppSettings["SMTPPort"]); string senderEmail = ConfigurationManager.AppSettings["SenderEmail"]; string senderID = ConfigurationManager.AppSettings["SenderUser"]; string senderPassword = ConfigurationManager.AppSettings["SenderPwd"]; // Define the recipient, subject, and body of the email string recipientEmail = "akki@gmail.com"; string subject = "Test Email From New Details"; string body = "This is a test email sent from ASP.NET MVC."; // Create a new MailMessage object using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress(senderEmail); mail.To.Add(recipientEmail); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = true; // Create a new SmtpClient object using (SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort)) { smtpClient.UseDefaultCredentials = false; //smtpClient.Credentials = new NetworkCredential(senderEmail, senderPassword); smtpClient.Credentials = new NetworkCredential(senderID, senderPassword); smtpClient.EnableSsl = true; // Ensure this matches the server's SSL requirements // Send the email smtpClient.Send(mail); //ViewBag.Message = "Email sent successfully!"; } } } catch (SmtpException smtpEx) { // Log SMTP specific errors //ViewBag.Message = $"SMTP Exception: {smtpEx.Message}"; Console.WriteLine(smtpEx.Message); } catch (Exception ex) { // Log general errors //ViewBag.Message = $"General Exception: {ex.Message}"; Console.WriteLine(ex.Message); }
<pre><add key="SMTPHost" value="smtp.email.ap-mumbai-1.oci.oraclecloud.com" />
<add key="SMTPPort" value="587" />
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)