I have a winforms app that sends an email via gmail. It works great, but in one case, the sending fails and I get the error above. It happens when my application is running on a computer that I access to via RDP connection. I have tried so far without success:
My code as follows: (Please note that this code is working in all cases but this)
string fromAddress = txtFrom.Text.Trim();
string toAddress = txtTo.Text.Trim();
string fromPassword = ePassword;
string subject = txtSubject.Text;
string body = txtMessage.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
//Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
//using (var message = new MailMessage(fromAddress, toAddress)
using (var message = new MailMessage()
{
From = new MailAddress(fromAddress, eDisplayName),
Subject = subject,
Body = body
})
{
// Check for recipiens
if (txtTo.Text.Trim() != "")
{
foreach (string email in txtTo.Text.Split(';'))
{
message.To.Add(email.Trim());
}
}
// check for copies
if (txtCopy.Text.Trim() != "")
{
foreach (string email in txtCopy.Text.Split(';'))
{
message.CC.Add(email.Trim());
}
}
// check for blind copy
if (txtBlindCopy.Text.Trim() != "")
{
foreach (string email in txtBlindCopy.Text.Split(';'))
{
message.Bcc.Add(email.Trim());
}
}
//check for attachments
for (int i = 0; i < clbAttachments.Items.Count; i++)
{
if (clbAttachments.GetItemChecked(i) == true)
{
Attachment attachment = new Attachment(clbAttachments.Items[i].ToString());
message.Attachments.Add(attachment);
}
}
smtp.Send(message);
return true;
Found an answer. I have changed the port to 25 in my code and the message was sent! I rolled back all of my changes to the OS and double checked to make sure the port changing is the only thing that needs to be done. Hope someone will find this usefull.
Edit: Port 25 is a non-secure port. In my case, I didn't care so it solved my problem.