Having trouble with C# email sending functionality
I’m trying to create a simple email sender using C# that can send messages from one Gmail account to another. I’ve looked at several tutorials online but keep running into the same error message.
Here’s my current implementation:
using System;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms;
namespace EmailSender
{
public class MailService
{
public void SendMessage()
{
// Create email message
MailMessage email = new MailMessage();
email.From = new MailAddress("[email protected]");
email.To.Add("[email protected]");
email.Subject = "Test Email";
email.Body = "This is a test message from C# application";
email.CC.Add("[email protected]");
// Configure SMTP client
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential("[email protected]", "password123");
smtpServer.EnableSsl = true;
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
// Send email
try
{
smtpServer.Send(email);
MessageBox.Show("Email sent successfully!");
}
catch (Exception error)
{
MessageBox.Show("Failed to send: " + error.Message);
}
}
}
}
I’ve tried different port numbers like 25, 465, and 587 but nothing works. The error message I keep getting says the server doesn’t support secure connections. Has anyone else faced this problem? What am I missing in my configuration?