Reputation: 834
When I use outlook, I am able to send test email to my gmail address, however, when I do it from a console application it triggers : "The server response was: 5.7.1 Unable to relay"
using System.Net.Mail;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MailMessage mail = new MailMessage("[email protected]", "[email protected]");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "xxx.xxx.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
}
}
}
I verified that i have the correct client host through outlook. I also sent a test email to myself (from xx@mycompany to xx@mycompany) and that worked (although it sent it to the junk box). Why will it not let me send outgoing emails through this console app, but I can through the same address in outlook.
Upvotes: 0
Views: 838
Reputation: 16512
I'm pretty sure that if you have client.UseDefaultCredentials = false;
, you need to set the credentials. At least that is what I do:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(someusername, somepassword);
edit: I should clarify, client.UseDefaultCredentials = false;
, does not necessarily mean you need credentials listed, but if you are trying to send to an external domain (gmail.com), then your SMTP server will most likely require some type of SMTP Auth.
Upvotes: 1