locked
How to design mail sending class with abstract class RRS feed

  • Question

  • User264732274 posted

    i want to design a class which will send mail but i want to make it abstract because i want to give provision to customize or load mail body who extend this class.

    basically my class will have some abstract property like To, from, CC, BCC, Attachment and body but send function will have code . so when a user will extend this class

    user will override property called To, from, CC, BCC, Attachment and body and then user just call abstract class send function to send mail.

    thanks

    Friday, September 11, 2015 10:00 AM

Answers

  • User614698185 posted

    Hi sudip_inn,

    I modify my abstract class, code shown below:

        public abstract class SendMailClass
        {
            public SendMailClass()
            {
                // Code to initialize the class goes here.
    
            }
    
            public SendMailClass(string _Toname, string _CCname, string _Bccname, string _filepathe, string _body)
            {
                To = _Toname;
                CC = _CCname;
                Bcc = _Bccname;
                Attachment = _filepathe;
                Body = _body;
            }
    
            protected string Toname;
            protected string CCname;
            protected string Bccname;
            protected string filepathe;
            protected string body;
    
            public string To {
                get { return Toname; }
                set { Toname = value; }
            }
            public string CC
            {
                get { return CCname; }
                set { CCname = value; }
            }
            public string Bcc {
                get { return Bccname; }
                set { Bccname = value; }
            }
            public string Attachment {
                get { return filepathe; }
                set { filepathe = value; }
            }
            public string Body {
                get { return body; }
                set { body = value; }
            }
    
            public void Send()
            {
                //Do something to send e-mail using above properties: TO, CC, Bcc, Attachement, Body
                
            }
        }

    For example, I create the class inherited from the abstract class above:

      public class SendMail : SendMailClass
        {
            public SendMail() : base("Franklin", "Jeffrey" ,"Alan" ,"balabala", "Hello World")
            {
            }
        }

    Usage:

    //Usage Part
    SendMail sm = new SendMail();
    sm.Send();

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2015 9:07 AM

All replies

  • User303363814 posted

    Why?

    If you want to set the property called To then assign a value to it.  Why would you want to create a new class and override the property?

    Choosing a technology (abstract class or anything else) and then forcing your design to use it is a vey basic error.

    Sunday, September 13, 2015 3:49 AM
  • User614698185 posted

    Hi sudip_inn,

    Based on your requirement, I created the sample for you, code shown below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SmtpSender
    {
        public abstract class sendMailClass
        {
            protected string Toname;
            protected string CCname;
            protected string Bccname;
            protected string filepathe;
            protected string body;
            public abstract string To { get; set; }
            public abstract string CC { get; set; }
            public abstract string Bcc { get; set; }
            public abstract string Attachment { get; set; }
            public abstract string Body { get; set; }
    
            public abstract bool Send();
        }
        class SendMail:sendMailClass
        {
            public override string To
            {
                get
                {
                    return Toname;
                }
                set
                {
                    Toname = value;
                }
            }
    
            public override string CC
            {
                get
                {
                    return CCname;
                }
                set
                {
                    CCname = value;
                }
            }
            public override string Bcc
            {
                get
                {
                    return Bccname;
                }
                set
                {
                    Bccname = value;
                }
            }
            public override string Attachment
            {
                get
                {
                    return filepathe;
                }
                set
                {
                    filepathe = value;
                }
            }
            public override string Body
            {
                get
                {
                    return body;
                }
                set
                {
                    body = value;
                }
            }
            public override bool Send()
            {
                Toname = "111";
                CCname = "222";
                Bccname = "333";
                filepathe = @"c:\logdata.dat";
                body = "hsjkdhfkdshfk";
    
                return false;
            }
            static void Main(string[] args)
            {
                SendMail o = new SendMail();
                o.Send();
            }
        }
    }
    

    Best Regards,

    Candice Zhou

    Tuesday, September 15, 2015 8:34 AM
  • User264732274 posted

    as i follow your code that make no sense. all functions are abstract. we can do that with interface too.

    we should use abstract class to share few code with other.

    i said i like to design a abstract class where few function will be abstract but send function will have code. so who extend then he/she can simply call send method to send mail.

    Tuesday, September 15, 2015 9:19 AM
  • User303363814 posted

    i said i like to design a abstract class where few function will be abstract but send function will have code. so who extend then he/she can simply call send method to send mail.

    If you use System.Net.Mail.SmtpClient to send mail then what problems do you have?  Show us the code you have written and explain what the business requirement is that you are trying to fulfil but have difficulty with.  (Note that it is never a business requirement to create an abstract class.  An abstract class may be part of a solution but it is never a requirement)

    It is very difficult to help with a question which is basically "if I try to use a technology that I don't understand to fulfil a requirement that I can't explain then there might be a problem, please help"

    Wednesday, September 16, 2015 1:01 AM
  • User264732274 posted

    sample example attached

    namespace ConsoleApplication2
    {
        public abstract class sendMailClass
        {
            //protected string Toname;
            public abstract string To { get; set; }
    
            public void Send()
            {
                string xx = To;
            }
        }
    
        class Concrete : sendMailClass
        {
            public override string To { get; set; }
            
        }
    
        class Program 
        {
            static void Main(string[] args)
            {
                Concrete p = new Concrete();
                p.To = "Hello";
                p.Send();
            }
        }
    }
    

    Wednesday, September 16, 2015 8:38 AM
  • User303363814 posted

    Delete the Concrete class and delete the word abstract from everywhere.  Your program works exactly the same way.

    Wednesday, September 16, 2015 9:03 AM
  • User614698185 posted

    Hi sudip_inn,

    I modify my abstract class, code shown below:

        public abstract class SendMailClass
        {
            public SendMailClass()
            {
                // Code to initialize the class goes here.
    
            }
    
            public SendMailClass(string _Toname, string _CCname, string _Bccname, string _filepathe, string _body)
            {
                To = _Toname;
                CC = _CCname;
                Bcc = _Bccname;
                Attachment = _filepathe;
                Body = _body;
            }
    
            protected string Toname;
            protected string CCname;
            protected string Bccname;
            protected string filepathe;
            protected string body;
    
            public string To {
                get { return Toname; }
                set { Toname = value; }
            }
            public string CC
            {
                get { return CCname; }
                set { CCname = value; }
            }
            public string Bcc {
                get { return Bccname; }
                set { Bccname = value; }
            }
            public string Attachment {
                get { return filepathe; }
                set { filepathe = value; }
            }
            public string Body {
                get { return body; }
                set { body = value; }
            }
    
            public void Send()
            {
                //Do something to send e-mail using above properties: TO, CC, Bcc, Attachement, Body
                
            }
        }

    For example, I create the class inherited from the abstract class above:

      public class SendMail : SendMailClass
        {
            public SendMail() : base("Franklin", "Jeffrey" ,"Alan" ,"balabala", "Hello World")
            {
            }
        }

    Usage:

    //Usage Part
    SendMail sm = new SendMail();
    sm.Send();

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2015 9:07 AM
  • User264732274 posted

    @Candice see your abstract class code. i saw there is no single abstract property or function. how your class should be consider by abstract class?

    does your abstract class code will compile?

    Wednesday, September 16, 2015 9:26 AM
  • User614698185 posted

    Hi sudip_inn,

    i saw there is no single abstract property or function. how your class should be consider by abstract class?

    You can also write the property to abstract property.

    Best Regards,

    Candice Zhou

    Thursday, September 17, 2015 7:02 AM
  • User264732274 posted

    i am looking for a real world sample with abstract class and want to understand when people would use abstract class in real life instead of Interface. thanks

    Thursday, September 17, 2015 7:28 AM