Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im still very new to programming and am struggling with this task. Everything I tried from the Internet so far hasn't worked.

I need to write a Domain Service in VisualStudio in C# for sending Emails. As a Host we use Exchange. Any help would be much appreciated.

What I have tried:

using System;

using System.Net;
using System.Net.Mail; 

namespace TestClient {
  class Program
  {
    public static void Main (string[] args)
    {
      MailAddress to = new MailAddress("ToAddress");
      MailAddress from = new MailAddress("FromAddress");

      MailMessage email = new MailMessage(from, to);
      email.Subject = "Testing out email sending";
      email.Body = "Hello all the way from the land of C#";

      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.server.address";
      smtp.Port = 25;
      smtp.Credentials = new NetworkCredential("smtp_username", "smtp_password");
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
      smtp.EnableSsl = true;

      try
      {
        /* Send method called below is what will send off our email 
         * unless an exception is thrown.
         */ 
        smtp.Send(email);
      }
      catch (SmtpException ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }
  }
}
Posted
Updated 17-Jan-23 2:06am
Comments
Richard MacCutchan 17-Jan-23 7:42am    
So what exactly is the problem?
NewProgrammer321 17-Jan-23 7:43am    
Not sure what I need to modify or do to make it work. But as of right now its not working.
Richard MacCutchan 17-Jan-23 7:51am    
Sorry, but no one here can guess what "not working" means. Please use the Improve question link above, and add complete details of exactly what happens when you run the code.

1 solution

All the wrong terminology... YOu're not writing a "Domain Service". You're writing a normal application that is trying to send an email.

If this is the exact code you're using, it's no wonder it doesn't work. The smtp.Host must be set to an existing email server you can use. The address "smtp.server.address" is not a valid address. You need to set this to the DNS name of the email server you're using.

Depending on how the server is configured and it's requirements for you to use it, you may also have to change the port number.

We can't tell you exactly what these values are supposed to be because they are specific to the email server you're trying to use. Contact the people that run the server and they can tell you what the values are supposed to be.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900