13 Eylül 2012 Perşembe

CRM 2011 ile Email Atma


Bir önceki yazımda template kullanarak email atmayı görmüstük.Bu yazım da da template kullanmadan basit mail atmayı anlatacagım.
Not:Bir önceki yazımda bu classın nasıl kullanılacagı bulunmaktadır.

public static Boolean EmailAt(IOrganizationService service,Guid fromUserId,Guid toContactId)
        {
            try
            {
                Boolean atildiMi = false;
                // Create the 'From:' activity party for the email
                ActivityParty fromParty = new ActivityParty
                {
                    PartyId = new EntityReference(SystemUser.EntityLogicalName, fromUserId)
                };

                // Create the 'To:' activity party for the email
                ActivityParty toParty = new ActivityParty
                {
                    PartyId = new EntityReference(Contact.EntityLogicalName, toContactId)
                };
                //"Created activity parties."

                // Create an e-mail message.
                Email email = new Email
                {
                    To = new ActivityParty[] { toParty },
                    From = new ActivityParty[] { fromParty },
                    Subject = "Quote Ready",
                    Description = "SDK Sample for SendEmail Message.",
                    DirectionCode = true
                };
                Guid _emailId = service.Create(email);

                // Use the SendEmail message to send an e-mail message.
                SendEmailRequest sendEmailreq = new SendEmailRequest
                {
                    EmailId = _emailId,
                    TrackingToken = "",
                    IssueSend = true
                };

                //"Sent the e-mail message." 
                SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
                atildiMi = sendEmailresp !=null ? true :false;

                return atildiMi;
            }
            catch (Exception)
            {
                return false;
            }
        }

Template Kullanarak Crm 2011 ile Email Yollama


İlk önce mail atan classı paylasacagım sızlerle.Daha sonra da bu classın nasıl kullanıldıgını anlatacagım.

EmailAt Classı:
Mail atma işlemi basarılıysa geriye true,degil se false degeri dondurur.
namespace SendEmail
{
    public class EmailClass
    {
        public static Boolean EmailAt(IOrganizationService service,Guid fromUserId,Guid toContactId)
        {
            try
            {
                Boolean atildiMi = false;
                // Kimin email atılacagı tanımlanıyor.
                ActivityParty fromParty = new ActivityParty
                {
                    PartyId = new EntityReference(SystemUser.EntityLogicalName, fromUserId)
                };

                // Kime email atılacagı tanımlanıyor.
                ActivityParty toParty = new ActivityParty
                {
                    PartyId = new EntityReference(Contact.EntityLogicalName, toContactId)
                };

                // Gonderılecek  Email mesajını olusturalım
                Email email = new Email
                {
                    To = new ActivityParty[] { toParty },
                    From = new ActivityParty[] { fromParty },
                    Subject = "Quote Ready",
                    Description = "SDK Sample for SendEmail Message.",
                    DirectionCode = true
                };
                // Email mesajının sablonunu olusturalım.
                SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
                {
                    Target = email,
                    //  Crm im hazır bir template ini kullanıyoruz.
                    TemplateId = new Guid("07B94C1D-C85F-492F-B120-F0A743C540E6"),

                    // Email dekı hitap içerigini tanımlıyoruz.
                    RegardingId = toContactId,
                    RegardingType = Contact.EntityLogicalName
                };
                SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq);

                //Template ile birlikte bir email id olusturup.Emaili yaratmıs oluyoruz.
                Guid _emailId = emailUsingTemplateResp.Id;

                atildiMi = !_emailId.Equals(Guid.Empty) ? true :false;
                //atildımı true ise :Basarılı bir sekılde email yollandı.

                return atildiMi;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

Default.aspx :
Ben burada crm den javascript ile atılacak kişilerin id degerini QueryString ile aldım.Siz isterseniz direkt olarak id degerini verebilirsiniz.
namespace SendEmail
{
    public partial class Default : System.Web.UI.Page
    {
        IOrganizationService crmService;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid _firstApprovedId =!string.IsNullOrEmpty(Request.QueryString["firstApproved"] )? 
                                         Guid.Parse(Request.QueryString["firstApproved"]) :Guid.Empty;
                Guid _secondApprovedId =!string.IsNullOrEmpty(Request.QueryString["secondApproved"]) ? 
                                        Guid.Parse(Request.QueryString["secondApproved"]) : Guid.Empty;
                Guid _thirdApprovedId = !string.IsNullOrEmpty(Request.QueryString["thirdApproved"]) ? 
                                        Guid.Parse(Request.QueryString["thirdApproved"]) : Guid.Empty;

                EmailYolla(_firstApprovedId,_secondApprovedId,_thirdApprovedId);
            }
        }

        protected void EmailYolla(Guid firstApprovedId,Guid secondApprovedId,Guid thirdApprovedId)
        {
            crmService = DynamicCrmBaglanma.GetCrmService();
            Boolean onay1 = false;
            Boolean onay2 = false;
            Boolean onay3 = false;
            string DonusMesaji="";

            ColumnSet tumKolon = new ColumnSet() { AllColumns = true };

            WhoAmIRequest systemUserRequest = new WhoAmIRequest();
            WhoAmIResponse systemUserResponse = (WhoAmIResponse)crmService.Execute(systemUserRequest);
            Guid _userId = systemUserResponse.UserId;

            Contact firstApproved = (Contact)crmService.Retrieve("contact", firstApprovedId, tumKolon);
            Contact secondApproved = (Contact)crmService.Retrieve("contact", secondApprovedId, tumKolon);
            Contact thirdApproved = (Contact)crmService.Retrieve("contact", thirdApprovedId, tumKolon);

            if (firstApproved != null && firstApproved.Id != Guid.Empty && firstApproved.EMailAddress1 !=string.Empty)
                onay1 = EmailClass.EmailAt(crmService, _userId, firstApproved.Id);

            if (secondApproved != null && secondApproved.Id != Guid.Empty && secondApproved.EMailAddress1 != string.Empty)
                onay2 = EmailClass.EmailAt(crmService, _userId, secondApproved.Id);

            if (thirdApproved != null && thirdApproved.Id != Guid.Empty && thirdApproved.EMailAddress1 != string.Empty)
                onay3 = EmailClass.EmailAt(crmService, _userId, thirdApproved.Id);

            if (onay1 != true && onay2 !=true && onay3 != true)
            {
                DonusMesaji="Unsucceeded";
            }
            else
            {
                DonusMesaji = "Send Email.";
            }
        }
    }