Send Email using Template


public void SendMail(ICrmService service,Guid ownerID, Guid fromGuid, ArrayList toGuid,Guid recordRegardingID,Guid templateId,Enum  enitityName)
{

// Create an instance of email
email myEmail = new email();
//set the owner of the mail
myEmail.ownerid = new Owner();
myEmail.ownerid.type = EntityName.systemuser.ToString();
myEmail.ownerid.Value = ownerID;
// specify the from part of the email
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
// guid of the system user who is sending the mail
from.partyid.Value = fromGuid;
myEmail.from = new activityparty[] { from };

activityparty[] apTo = new activityparty[toGuid.Count];
// creating as many activity party as the no of users or members in a team
int i = 0;
foreach (String memberID in toGuid)
{
apTo[i] = new activityparty();
apTo[i].partyid = new Lookup();
apTo[i].partyid.type = EntityName.systemuser.ToString();
apTo[i].partyid.Value = new Guid(memberID);
i++;
}

myEmail.to = apTo;
TargetSendFromTemplateEmail myTargetSendFromTemplateEmail = new TargetSendFromTemplateEmail();
myTargetSendFromTemplateEmail.Email = myEmail;
SendEmailFromTemplateRequest mySendEmailFromTmpRequest = new SendEmailFromTemplateRequest();
// Specify entity instance id for RegardingID
mySendEmailFromTmpRequest.RegardingId = recordRegardingID;
// Specify the type of entity
mySendEmailFromTmpRequest.RegardingType = enitityName.ToString();
// Specify the email instance to be sent
mySendEmailFromTmpRequest.Target = myTargetSendFromTemplateEmail;
// Specify the template to be used ( Either a global or created specfially for that entity)
mySendEmailFromTmpRequest.TemplateId = templateId;
SendEmailFromTemplateResponse mySendEmailFromTmpResponse =(SendEmailFromTemplateResponse)service.Execute(mySendEmailFromTmpRequest);

}