Google Mailer Class

I have an email program for Google, as shown below, how can I adapt it for other email systems? Is there a better way to do this? as well as how can I change the name from where the mail is sent (sender). All help would be much appreciated and thanks in advance

 MailMessage message = new MailMessage();
        message.From = new MailAddress(MailAddresds);
        message.Subject = messagesubject;
        message.Body = messagebody;


        message.To.Add(messageto);
        SmtpClient client = new SmtpClient();

        client.Credentials = new NetworkCredential(userName, password);
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Send(message);
+3
source share
2 answers

Just an example:

public class MailMessage
{
   public string From{get;set;}
   public string To{get;set;}
   public string Body{get;set;}
   public string Subject{get;set;}
   ....
   //other common properties you may need
}


//interface 
public interface IMailService
{
    Send(MailMessage m);
}

specific implementations:

public class GoogleMail : IMailService
{
    public Send(Message msg)
    { 
       //google mail specific code
    }
}


public class YahooMail : IMailService
{
    public Send(Message msg)
    { 
       //yahoo mail specific code
    }
}

.... Hotmail,...

somewhere in the code, create a collection of supported mail services

var mailservices = new List<IMailService>();
mailservices.Add(new GoogleMail ());
mailservice.Add(new YahooMail ());

after starting the program, select the appropriate service to continue the user’s request.

+1
source

simple example:

using System.Web.Mail;

MailMessage objMessage = new MailMessage();
objMessage.From = "from";
objMessage.To = "to";
objMessage.Subject = "subject";
objMessage.BodyFormat = MailFormat.Text;
objMessage.Body = "body";
SmtpMail.SmtpServer = "SmtpServer";
SmtpMail.Send(objMessage);
+1
source

All Articles