コード例 #1
0
ファイル: Helper.cs プロジェクト: Chrullbo/SignMiApp
        public Person Send(Person p, Enums.MailType type, string additionalinfo)
        {
            if (!IsApproved(p))
                return p;
            PersonPersons = new List<Person>(p.Persons.Select(x => new Person { Name = !string.IsNullOrEmpty(x.Nickname) ? x.Nickname : x.Person.Name, ExternalID = x.Person.ExternalID, ID = x.Person.ID })) { new Person { Name = p.Name, ExternalID = p.ExternalID, ID = p.ID } };
            var body = string.Empty;
            var subject = string.Empty;
            string name;
            //bool includeimages = false;
            var mailHelper = new MailHelper();
            switch (type)
            {
                case Enums.MailType.EventInvitation:
                    name =
                        (PersonPersons.SingleOrDefault(x => x.ID == EventTriggerPerson.ID) ??
                         new Person { Name = EventTriggerPerson.Name }).Name;
                    subject = "This is an invitation from " + name;

                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader("This is an invitation from " + name));
                    
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(true));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", Event.OptionalMessage);
                    if (p.Status == (byte) Enums.PersonStatus.Inaktiv)
                    {
                        body = body.Replace("[sma:firsttime]", mailHelper.GetFirstTimeInfo(EventTriggerPerson));
                        body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, false));
                        body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, false, PersonPersons, Event));
                        body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    }
                    else
                    {
                        body = body.Replace("[sma:firsttime]", string.Empty);
                        body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, false));
                        body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, false, PersonPersons, Event));
                        body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    }
                    

                    break;
                case Enums.MailType.AccountActivated:
                    subject = "Accout activation success, SignMiApp.com";
                    var b = "<h1>Your Activation Succeed!!</h1>";
                    b += "<h2>Welcome " + p.Name + "</h2>";
                    b += "<p><b>Yoyr credentials</b></p>";
                    b += "<p>Username: "******"</p>";
                    b += "<p>Password: "******"</p>";
                    b +=
                        "<br /><br /><p>Change yor password to something more easy to remember, click the cogwheel to change your password</p>";
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader("Activation succeed, welcome "+p.Name));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", string.Empty);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, false));
                    body = body.Replace("[sma:optional]", b);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p,  false));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, false,  PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));

                    break;
                //case Enums.MailType.AccountStart:
                //    subject = "Accout activation one step away, SignMiApp.com";
                //    body += "<h1>Click the link below to activate your account!!</h1>";
                //    body += "<a href='http://www.signmiapp.com/signmiappservice/account/activateaccount/" + p.ExternalID + "'>Activate account</a>";
                //    body = MailTop() + body;

                //    break;
                case Enums.MailType.AccountReset:
                    subject = "Password reset success, SignMiApp.com";
                    var y = "<h1>Your new password!!</h1>";
                    y += "<p>" + p.Password + "</p>";
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader("Your new password"));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", string.Empty);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, false));
                    body = body.Replace("[sma:optional]", y);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, false));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, false, PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    break;
                case Enums.MailType.PersonlInvitation:
                    subject = "Invitation from SignMiApp.com";
                    body += "<h1>Invitation from SignMiApp</h1>";
                    body += "<h2>Welcome " + p.Name + "</h2><p>You are welcome to use this service to create all types of events and happenings for you and your people.</p>";

                    break;
                case Enums.MailType.EventMessCreated:
                    name =
                            (PersonPersons.SingleOrDefault(x => x.ID == EventTriggerPerson.ID) ??
                             new Person { Name = EventTriggerPerson.Name }).Name;
                    subject = name + " has created a new wallpost for " + Event.Name;
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader("New wallpost: "+ Event.Wallposts.OrderByDescending(x=>x.Date).First().Message));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", string.Empty);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, true));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, true, PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    //body = MailTop() + body;
                    //includeimages = true;
                    break;
                case Enums.MailType.EventSignMiAppStatusChanged:
                    name =
                                (PersonPersons.SingleOrDefault(x => x.ID == EventTriggerPerson.ID) ??
                                 new Person { Name = EventTriggerPerson.Name }).Name;
                    Enums.Attending stat;
                    Enum.TryParse(additionalinfo, out stat);
                    subject = name + "'s new SignMiAppstatus is " + stat;
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader(name + "'s new SignMiAppstatus is " + stat));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", string.Empty);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, true));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, true, PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    //includeimages = true;
                    break;
                case Enums.MailType.EventCancellation:

                    subject = Event.Name + "is canceled";
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader(Event.Name + "is canceled by "+EventTriggerPerson.Name));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", additionalinfo);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p, true));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, true, PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    //includeimages = true;
                    break;

                case Enums.MailType.EventReminder:

                    subject = "This is a reminder of " + Event.Name;
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader("This is a reminder of " + Event.Name));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", additionalinfo);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p,  true));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, true,  PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    //includeimages = true;
                    break;
                case Enums.MailType.EventConfirmation:

                    subject = "Event confirmation, " + Event.Name;
                    body = mailHelper.MailTemplate();
                    body = body.Replace("[sma:preheader]", CreatePreHeader(Event.Name+ " is confirmed by " + EventTriggerPerson.Name));
                    body = body.Replace("[sma:invitation]", mailHelper.SmaInvitation(false));
                    body = body.Replace("[sma:eventname]", Event.Name);
                    body = body.Replace("[sma:event]", mailHelper.GetWhenWhereTable(Event, true));
                    body = body.Replace("[sma:optional]", additionalinfo);
                    body = body.Replace("[sma:firsttime]", string.Empty);
                    body = body.Replace("[sma:Wall]", mailHelper.SmaWall(Wallposts, p,  true));
                    body = body.Replace("[sma:attending]", mailHelper.SmaAttending(EventPersons, p, true,  PersonPersons, Event));
                    body = body.Replace("[sma:login]", mailHelper.SmaLink(p, Event, true));
                    //includeimages = true;
                    break;
            }

            //body += MailFooter();
            var m = new MailMessage
            {
                Body = body,
                From = new MailAddress("*****@*****.**", "SignMiApp.com"),
                BodyEncoding = System.Text.Encoding.UTF8,
                IsBodyHtml = true,
                Subject = subject,
                SubjectEncoding = System.Text.Encoding.UTF8,


            };
            var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

            //if (_attachments != null && includeimages)
            //{
            //    foreach (var attachment in _attachments)
            //    {
            //        view.LinkedResources.Add(attachment);
            //    }
            //}
            m.AlternateViews.Add(view);
            m.Bcc.Add(type + "@signmiapp.com");
            try
            {
                m.To.Add(p.Email);
            }
            catch (Exception ex)
            {
                p.Loggs.Add(new Logg { Date = DateTime.Now, Message = ex.Message });
                return p;

            }


            //p.Mails.Add(new Mail
            //{
            //    ExternalID = mailId,
            //    PersonID = p.ID,
            //    Message = mailBody,
            //    Type = (int)type,
            //    Date = DateTime.Now

            //});

            try
            {
                _smtpServer.Send(m);
            }
            catch (Exception ex)
            {
                p.Loggs.Add(new Logg { Date = DateTime.Now, Message = ex.Message });
                return p;
            }
            return p;
        }