Need bit help regarding Fluent style coding design

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have develop a small classes with fluent style design for sending mail. now i want to add some more functionality in my code as a result i can attach many file before sending mail.

the code i use to send mail now is like below

var template = new MailTemplate()
.WithBody(Utility.GetException(ex))
.WithSubject("Error occured in CV Downloader application")
.WithSender("bba-india@bba-reman.com")
.WithRecepient(Utility.SendErrorMailIds)
.Send();

but i want to attach file like

var template = new MailTemplate()
.Attachment.Add("c:a1.txt")
.Add("c:a2.txt")
.Add("c:a3.txt")
.WithBody(Utility.GetException(ex))
.WithSubject("Error occured in CV Downloader application")
.WithSender("bba-india@bba-reman.com")
.WithRecepient(Utility.SendErrorMailIds)
.Send();

so i need to add file like

.Attachment.Add("c:a1.txt")
.Add("c:a2.txt")
.Add("c:a3.txt")

then tell me what i need to add in my code

here is my full code for sending mail. just have look and guide me what i need to add in my code as a result i can add attachment like above one

public class MailTemplate
{
string _mailBody = "";
string _subject = "";
string _From = "";
string _To = "";
bool _status = false;
string _ErrorMessage = "";

public string Body
{
get { return _mailBody; }
set { _mailBody = value; }
}

public string Subject
{
get { return _subject; }
set { _subject = value; }
}

public string MailFrom
{
get { return _From; }
set { _From = value; }
}

public string MailTo
{
get { return _To; }
set { _To = value; }
}

public bool Status
{
get { return _status; }
set { _status = value; }
}

public string ErrorMessage
{
get { return _ErrorMessage; }
set { _ErrorMessage = value; }
}
}

public static class MailTemplateBuilder
{
public static MailTemplate WithBody(this MailTemplate item, string body)
{
item.Body = body;
return item;
}

public static MailTemplate WithSubject(this MailTemplate item, string subject)
{
item.Subject = subject;
return item;
}

public static MailTemplate WithSender(this MailTemplate item, string sender)
{
item.MailFrom = sender;
return item;
}

public static MailTemplate WithRecepient(this MailTemplate item, string recepient)
{
item.MailTo = recepient;
return item;
}

public static MailTemplate Send(this MailTemplate item)
{
MailMessage mail = new MailMessage(item.MailFrom, item.MailTo, item.Subject, item.Body);
mail.Priority = System.Net.Mail.MailPriority.Normal;
mail.IsBodyHtml = true;

try
{
SmtpClient emailClient = new SmtpClient("mail2.orcsweb.com");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("us-sales@bba-reman.com", "us450m0");
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
emailClient.Send(mail);

item.Status = true;
item.ErrorMessage = "";
}
catch (Exception ex)
{
item.Status = false;
item.ErrorMessage = ex.Message;
}

return item;
}
}

one more thing that how to iterates in attachment collection to get the file name with path for attaching before sending mail from Send() routine?

View the full article
 
Back
Top