user601496
user601496

Reputation: 85

SMTP relay - send email from console application

Is it possible to send email from a server that uses smtp-relay through a .net application.

I'm using app.config to get the actual values ex server IP, and the fromadress that the email should use.

According to the IT-technician the username and password to authorize should not be needed because it uses smtp-relay. The computer that are going to send the email is on the smtp-servers list of valid computers.

Can this actually work, don't I need to specify the username/pwd?

try
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient(_smtpserver);
    mail.From = new MailAddress(_fromAdress);
    mail.To.Add(_toAdress);
    mail.Subject = _subject;
    mail.Body = _body;
    mail.Priority = MailPriority.High;
    SmtpServer.Port = Convert.ToInt32(_port);
    SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

Upvotes: 5

Views: 16713

Answers (3)

muzaffer
muzaffer

Reputation: 1

I had same situation. I got same error code. I noticed , I had used system.web.mail. But my application is a console application. I changed library, I used system.net.mail. And it works now.

Upvotes: 0

user1102001
user1102001

Reputation: 707

in your case,if you are seting your username and password in webconfig even then you need to call it like above. but if you using your default mail credentials then it will pick automatically.. and if you are using diiferent mail client for sender then you have to pass credentials..

Upvotes: 0

Anri
Anri

Reputation: 6265

In your snippet you are specifying credentials

SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);

You can remove that line and it still will work if SMTP relay is configured as you said.

Upvotes: 3

Related Questions