-2

I tried to send email via c# by following:

  try {
                MailMessage message = new MailMessage();
                message.To.Add("receiver@mail.com");
                message.Subject = "This is the Subject line";
                message.From = new MailAddress("sender@online.microsoft.com");
                message.Body = "This is the message body";
                SmtpClient smtp = new SmtpClient("http://schemas.microsoft.com/cdo/configuration/smtpserver");
                smtp.Send(message);
            } catch (Exception ex) {
                Response.Write(ex.ToString());
            }

I get the error message:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'http://schemas.microsoft.com/cdo/configuration/smtpserver' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at......

I've never done this before. Any help would be much appreciated.

ipid
  • 253
  • 1
  • 4
  • 16
  • 1
    Why do you use that URL then? Did you try looking up the SmtpClient documentation to see what that constructor parameter does? – CodeCaster Nov 01 '17 at 11:17
  • That URL points to an XML document's *schema*. URLs aren't server addresses, they point to something on a server. A server address (web, email, FTP, whatever) is either an IP address or a fully qualified name. – Panagiotis Kanavos Nov 01 '17 at 11:24

1 Answers1

1

According to this, the SmtpClient constructor takes a mail server host name as an argument, not a schema address. Try changing your argument to whatever SMTP server you will be using to send your email.

SmtpClient smtp = new SmtpClient("my.mailserver.com");

Or, as CodeCaster pointed out, use the parameterless constructor which will then use your app or machine config settings instead (also detailed in the linked documentation)

Diado
  • 2,229
  • 3
  • 18
  • 21
  • 1
    Rather don't hardcode a sever address but use the parameterless constructor so the sever from your app's or machine's configuration will be used. – CodeCaster Nov 01 '17 at 11:19
  • Fair point, edited accordingly – Diado Nov 01 '17 at 11:30
  • Thanks, I changed to a proper smtp host name. But now I get: " `System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at..`". Any idea what that means? – ipid Nov 01 '17 at 11:57
  • Sounds like relaying is disabled on your SMTP server. This might help: https://stackoverflow.com/questions/3165721/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-for-abcxyz – Diado Nov 01 '17 at 12:13