We can use the following code for creating and sending an email in CRM
/// <summary>
/// Creating and Sending mail in CRM
/// </summary>
/// <param name=”crmService”>an instance of crmService (CrmService for CRM 3.0)</param>
/// <param name=”members”>ArrayList containing guids of the members</param>
/// <param name=”msg”>The message body</param>
/// <param name=”ownerID”>Owner of the record</param>
/// <param name=”userID”>User under whose context the callout or plugin is running </param>
private void SendMailToMembers(ICrmService crmService, ArrayList members, String msg, String ownerID, String userID)
{
// create an email
email emailCreate = new email();
emailCreate.subject = “MySubject”;
emailCreate.description = msg;
//specify the owner for the mail
emailCreate.ownerid = new Owner();
emailCreate.ownerid.type = EntityName.systemuser.ToString();
emailCreate.ownerid.Value = new Guid(ownerID);
//create an activityparty array holding all the guids specified in the members array list
activityparty[] ap = new activityparty[members.Count];
// creating as many activity party as the no of users or members in a team
int i = 0;
foreach (String memberID in members)
{
ap[i] = new activityparty();
ap[i].partyid = new Lookup();
ap[i].partyid.type = EntityName.systemuser.ToString();
ap[i].partyid.Value = new Guid(memberID);
i++;
}
// specify to part of the email
emailCreate.to = ap;
// specify the from part of the email
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
from.partyid.Value = new Guid(userID);
emailCreate.from = new activityparty[] { from };
// finally create the email and get the guid of the email
Guid emailId = crmService.Create(emailCreate);
// FOR CRM 3.0
// Specify the system user who is sending the message.
//crmService.CallerIdValue = new CallerId();
//crmService.CallerIdValue.CallerGuid = new Guid(userID);
//
// Create an SendEmailRequest object
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailId;
req.TrackingToken = “”;
req.IssueSend = true;
// Finally Send the email message.
SendEmailResponse res = (SendEmailResponse)crmService.Execute(req);
}
Bye
