Problem in sending email using api

hi aspose

i am using following line of code to send email using api but the problem is that is does not work , kindly check if there is any error.



MailMessage message = new MailMessage();
message.Subject = "Test Message";
message.To.Add(new MailAddress("example1@gmail.com"));
message.From = new MailAddress("example2@gmail.com");
message.TextBody = "Test successful";
message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Username = "example2@gmail.com";
client.Password = "12345";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
client.AuthenticationMethod = SmtpAuthentication.Auto;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(message);
Console.WriteLine("Message sent");
}

catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
Console.WriteLine("Message not sent");
}

Console.WriteLine("Press enter to quit");
Console.ReadKey();
}

Hi Muhammad,

Thank you for writing to Aspose support team.

In order to use Gmail, you don’t need to specify the authentication and delivery method as you have done above. The following sample code works fine at my end. Please try it at your end and let us know if we can be of help to you in this regard.

Sample Code:

SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587, “username”, “password”, SecurityOptions.Auto);


MailMessage eml = new MailMessage("from@gmai.com", "to@gmail.com", “Subject”, “Body”);


client.Send(eml);