Пример #1
0
        private void ConvertImage(ReplyMailInput input)
        {
            //如果是图片的话,必须使用CID
            if (string.IsNullOrEmpty(input.Content))
            {
                input.Content = "";
            }
            if (input.Attachments == null)
            {
                input.Attachments = new List <AttachmentInput>();
            }
            Regex regImg  = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            var   matches = regImg.Matches(input.Content);

            if (matches.Count == 0)
            {
                return;
            }
            foreach (Match match in matches)
            {
                var imgSrc       = match.Groups[1].Value;
                var imgBase64    = imgSrc.Split(',')[1];
                var imgBytes     = Convert.FromBase64String(imgBase64);
                var attachmentId = Guid.NewGuid().ToString("N").ToLower() + ".jpg";
                input.Content = input.Content.Replace(imgSrc, "cid:" + attachmentId);

                input.Attachments.Add(new AttachmentInput()
                {
                    Id    = attachmentId,
                    Name  = attachmentId,
                    Bytes = imgBytes
                });
            }
        }
Пример #2
0
        [RequestSizeLimit(100_000_000)] //最大100m左右
        public async Task <IActionResult> Reply([FromForm] ReplyMailInput input)
        {
            var userName = this.GetUserName();

            this.ConvertImage(input);
            if (input.Attachment != null)
            {
                foreach (var it in input.Attachment)
                {
                    var stream = it.OpenReadStream();
                    var bytes  = new byte[it.Length];
                    await stream.ReadAsync(bytes, 0, bytes.Length);

                    var name = Guid.NewGuid().ToString("N").ToLower() + "-" + it.FileName;
                    input.Attachments.Add(new AttachmentInput()
                    {
                        Bytes     = bytes,
                        Name      = name,
                        Id        = name,
                        IsPackage = true,
                    });
                }
            }
            if (input.CopyTo == null)
            {
                input.CopyTo = new string[0];
            }
            if (input.Attachments == null)
            {
                input.Attachments = new List <AttachmentInput>();
            }
            if (!string.IsNullOrEmpty(input.MailId))
            {
                await this._mailService.ReplyAsync(userName, input.MailId, input.Content, input.CopyTo, input.Attachments);

                return(Json(new { success = true }));
            }
            else
            {
                await this._mailService.Send(userName, input.Title, input.Content, input.Reciver, input.CopyTo, input.Attachments);

                return(Json(new { success = true }));
            }
        }