示例#1
0
        //attachment image.
        public BinaryResponse File(ApiCall call)
        {
            string filename = call.Command.Value;

            if (EmailForwardManager.RequireForward(call.Context))
            {
                var method = nameof(EmailAttachmentApi.MsgFile) + "/" + filename;
                if (!string.IsNullOrEmpty(filename))
                {
                    method += "/" + filename;
                }

                var filebytes = EmailForwardManager.Post(this.ModelName, method, call.Context.User, null, null);
                var response  = new BinaryResponse();
                response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                response.BinaryBytes = filebytes;
                return(response);
            }

            var bytes = Kooboo.Mail.MultiPart.FileService.Get(call.Context.User, filename);

            if (bytes != null && bytes.Length > 0)
            {
                var response = new BinaryResponse();

                response.ContentType = Kooboo.Lib.Compatible.CompatibleManager.Instance.Framework.GetMimeMapping(filename);
                response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                response.BinaryBytes = bytes;
                return(response);
            }
            return(null);
        }
示例#2
0
        public object MemberPost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("addressId", call.GetValue("addressId"));
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(call.Context.Request.Model);
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAddressApi.MemberPost), call.Context.User, json, dic));
            }

            int addressid = call.GetValue <int>("addressId");
            var orgdb     = Kooboo.Mail.Factory.DBFactory.OrgDb(call.Context.User.CurrentOrgId);

            var member = call.Context.Request.Model as ListMemberModel;

            member.MemberAddress = member.MemberAddress.ToLower();

            var address = orgdb.EmailAddress.Get(addressid);

            if (Lib.Helper.StringHelper.IsSameValue(member.MemberAddress, address.Address))
            {
                throw new Exception(Data.Language.Hardcoded.GetValue("Member address should not be same as group address", call.Context));
            }

            orgdb.EmailAddress.AddMember(addressid, member.MemberAddress);

            return(member);
        }
示例#3
0
        public int Post(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <int>(this.ModelName, nameof(EmailDraft.Post), apiCall.Context.User, json, null));
            }

            var user  = apiCall.Context.User;
            var model = apiCall.Context.Request.Model as Mail.ViewModel.ComposeViewModel;
            var msg   = Kooboo.Mail.Utility.ComposeUtility.FromComposeViewModel(model, user);

            msg.FolderId = Folder.ToId(Folder.Drafts);

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(user);

            if (msg.Id > 0)
            {
                maildb.Messages.Update(msg, Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user));
            }
            else
            {
                maildb.Messages.Add(msg, Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user));
            }

            return(msg.Id);
        }
示例#4
0
        public void Send(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("messageId", call.GetValue("messageId"));

                var json    = Kooboo.Lib.Helper.JsonHelper.Serialize(call.Context.Request.Model);
                var message = EmailForwardManager.Post <string>(this.ModelName, nameof(EmailMessageApi.Send), call.Context.User, json, dic);
                if (!string.IsNullOrEmpty(message))
                {
                    throw new Exception(message);
                }
                return;
            }

            var user  = call.Context.User;
            var model = call.Context.Request.Model as Mail.ViewModel.ComposeViewModel;
            var msg   = Kooboo.Mail.Utility.ComposeUtility.FromComposeViewModel(model, user);

            string messagebody = Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user);

            List <string> rcpttos = new List <string>(model.To);

            rcpttos.AddRange(model.Cc);
            rcpttos.AddRange(model.Bcc);

            string fromaddress = Mail.Utility.AddressUtility.GetAddress(msg.From);

            var msginfo = Kooboo.Mail.Utility.MessageUtility.ParseMeta(messagebody);

            if (rcpttos.Count > 0)
            {
                // verify sending quota.
                if (!Kooboo.Data.Infrastructure.InfraManager.Test(call.Context.User.CurrentOrgId, Data.Infrastructure.InfraType.Email, rcpttos.Count))
                {
                    throw new Exception(Data.Language.Hardcoded.GetValue("you have no enough credit to send emails", call.Context));
                }

                // save sent..
                Kooboo.Mail.Transport.Incoming.SaveSent(fromaddress, msginfo, messagebody);

                Kooboo.Mail.Transport.Incoming.Receive(fromaddress, rcpttos, messagebody, msginfo);

                // draft message id.
                var messageid = call.GetValue <int>("messageId");
                if (messageid > 0)
                {
                    var usermaildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);
                    usermaildb.Messages.Delete(messageid);
                }

                Kooboo.Data.Infrastructure.InfraManager.Add(call.Context.User.CurrentOrgId, Data.Infrastructure.InfraType.Email, rcpttos.Count, string.Join(",", rcpttos));
            }
        }
示例#5
0
 public void DeleteAttachment(string filename, ApiCall call)
 {
     if (EmailForwardManager.RequireForward(call.Context))
     {
         var dic = new Dictionary <string, string>();
         dic.Add("filename", filename);
         EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailAttachmentApi.DeleteAttachment), call.Context.User, dic);
         return;
     }
     Kooboo.Mail.MultiPart.FileService.DeleteFile(call.Context.User, filename);
 }
示例#6
0
        public bool IsUniqueName(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailAddressApi.IsUniqueName), apiCall.Context.User, json, null));
            }
            var model = apiCall.Context.Request.Model as AddressModel;
            var orgdb = Mail.Factory.DBFactory.OrgDb(apiCall.Context.User.CurrentOrgId);

            return(orgdb.EmailAddress.Get(model.ToEmail()) == null);
        }
示例#7
0
        public object Post(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAddressApi.Post), apiCall.Context.User, json, null));
            }

            var model   = apiCall.Context.Request.Model as AddressModel;
            var user    = apiCall.Context.User;
            var orgdb   = Kooboo.Mail.Factory.DBFactory.OrgDb(user.CurrentOrgId);
            var address = new EmailAddress
            {
                Address        = model.ToEmail().ToLower(),
                ForwardAddress = model.ForwardAddress,
                AddressType    = model.AddressType,
                UserId         = user.Id
            };

            if (!Kooboo.Mail.Utility.AddressUtility.IsValidEmailAddress(address.Address))
            {
                return(new Kooboo.Api.ApiResponse.JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        Kooboo.Data.Language.Hardcoded.GetValue("Invalid address format", apiCall.Context)
                    }
                });
            }

            if (address.AddressType == EmailAddressType.Forward)
            {
                address.ForwardAddress = address.ForwardAddress.ToLower();
                if (address.Address == address.ForwardAddress)
                {
                    return(new Kooboo.Api.ApiResponse.JsonResponse
                    {
                        Success = false,
                        Messages = new List <string> {
                            "Forward to address should not be same as forward from"
                        }
                    });
                }
            }

            orgdb.EmailAddress.AddOrUpdate(address);

            return(AddressItemModel.FromAddress(address));
        }
示例#8
0
        public object ImagePost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                //string convert to object will throw exception
                var url = EmailForwardManager.Post <string>(this.ModelName, nameof(EmailAttachmentApi.ImagePost), call.Context.User, call.Context.Request.PostData, null);
                if (!string.IsNullOrEmpty(url))
                {
                    return(url);
                }

                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxImageSize / 1024 / 1024}"
                    }
                });
            }

            var form = Kooboo.Lib.NETMultiplePart.FormReader.ReadForm(call.Context.Request.PostData);

            if (!form.Files.Any())
            {
                return(null);
            }

            var file = form.Files[0];

            if (file.Bytes.Length > MaxImageSize)
            {
                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxImageSize / 1024 / 1024}"
                    }
                });
            }

            var fileName = form.FormData["fileName"];

            fileName = Lib.Helper.StringHelper.ToValidFileName(fileName);

            Kooboo.Mail.MultiPart.FileService.Upload(call.Context.User, fileName, file.Bytes);

            return($"/_api/emailattachment/file/" + System.Web.HttpUtility.UrlEncode(fileName));
        }
示例#9
0
        public void Deletes(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("ids", call.GetValue("ids"));
                EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailMessageApi.Deletes), call.Context.User, dic);
                return;
            }

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var idsJson = call.GetValue("ids");
            var ids     = Lib.Helper.JsonHelper.Deserialize <List <int> >(idsJson);

            foreach (var id in ids)
            {
                maildb.Messages.Delete(id);
            }
        }
示例#10
0
        public object AttachmentPost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAttachmentApi.AttachmentPost), call.Context.User, call.Context.Request.PostData, null));
            }

            var form = Kooboo.Lib.NETMultiplePart.FormReader.ReadForm(call.Context.Request.PostData);

            if (!form.Files.Any())
            {
                return(null);
            }

            var file = form.Files[0];

            if (file.Bytes.Length > MaxAttachmentSize)
            {
                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxAttachmentSize / 1024 / 1024}"
                    }
                });
            }

            var fileName = form.FormData["filename"];

            fileName = Lib.Helper.StringHelper.ToValidFileName(fileName);

            Kooboo.Mail.MultiPart.FileService.Upload(call.Context.User, fileName, file.Bytes);
            return(new Mail.Models.Attachment()
            {
                FileName = fileName
            });
        }
示例#11
0
        // formate. msgfile/{messageid}/filename.
        public BinaryResponse MsgFile(ApiCall call)
        {
            string filename  = null;
            int    messageid = 0;

            var para = call.Command.Parameters;

            if (para.Count() == 2)
            {
                messageid = Convert.ToInt32(para[0]);
                filename  = System.Web.HttpUtility.UrlDecode(para[1]);
            }
            else
            {
                messageid = Convert.ToInt32(call.Command.Value);
            }

            if (EmailForwardManager.RequireForward(call.Context))
            {
                var method = nameof(EmailAttachmentApi.MsgFile) + "/" + messageid;
                if (!string.IsNullOrEmpty(filename))
                {
                    method += "/" + filename;
                }

                var bytes = EmailForwardManager.Post(this.ModelName, method, call.Context.User, null, null);

                filename = !string.IsNullOrEmpty(filename) ? System.Web.HttpUtility.UrlEncode(filename) : "attachment.zip";
                var response = new BinaryResponse();
                response.ContentType = "application/zip";
                response.Headers.Add("Content-Disposition", $"filename={filename}");
                response.BinaryBytes = bytes;
                return(response);
            }
            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var content = maildb.Messages.GetContent(messageid);


            if (!string.IsNullOrEmpty(content))
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    var bytes = Kooboo.Mail.Utility.MessageUtility.GetFileBinary(content, filename);
                    if (bytes != null && bytes.Length > 0)
                    {
                        var response = new BinaryResponse();

                        response.ContentType = Kooboo.Lib.Compatible.CompatibleManager.Instance.Framework.GetMimeMapping(filename);
                        response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                        response.BinaryBytes = bytes;
                        return(response);
                    }
                }
                else
                {
                    var bytes    = Mail.Utility.MessageUtility.GenerateAllAttachmentZip(content);
                    var response = new BinaryResponse();
                    response.ContentType = "application/zip";
                    response.Headers.Add("Content-Disposition", $"filename=attachment.zip");
                    response.BinaryBytes = bytes;
                    return(response);
                }
            }

            return(null);
        }