【DevExpress】邮箱制作小结
利用DevExpress的RichEditControl控件可以发送包含图片的邮件。但存在一个问题。RichEdit直接将图片解析成base64码包含在RichEdit的HtmlText中,这导致客户端发送到服务器时,邮件内容串行化超过容量导致发送失败。
解决的办法参考帖子:https://www.cnblogs.com/wuhuacong/archive/2013/01/27/2878368.html
按照该帖子的方法将图片提取出来,分别发送邮件内容和图片。
Program.MainForm.poxy.AddMailIns(inMails.ToArray()); Program.MainForm.poxy.AddMailOuts(outMails.ToArray()); Program.MainForm.poxy.InsertMailImages(mailImages.ToArray()); Program.MainForm.poxy.InsertMailImageRelations(mailImageRelations.ToArray());
上面分别存入数据库的四个表:收件表、发件表、图片表、图片邮件关联表。
但是参考帖子只给了发送邮件的解决思路,没有给出接收邮件和显示邮件的方法
具体思路是,给每个邮件对象添加一个IncludeImage属性,如果该属性为false,直接接收邮件并显示即可。
如果为true,在按照邮件ID去邮件图像关联表中找出包含图像的ImageId,再去图像表找出图像,读到本地的图像为byte[]类型,将其转换为base64码,再在邮件中,替换即可。类似这样:
protected string ParseRichEditContent(frmComMail.BoxMailItem boxMailItem) { bool isInMail = boxMailItem.IsInMail; string mailContent = ""; bool isExistRelation = false; bool isExistMailImage = false; string mailId = ""; if (isInMail) { mailContent = boxMailItem.InMail.MailContent; if ((bool)boxMailItem.InMail.IsIncludeImage) mailId = boxMailItem.InMail.MailId; } else { mailContent = boxMailItem.OutMail.MailContent; if ((bool)boxMailItem.OutMail.IsIncludeImage) mailId = boxMailItem.OutMail.MailId; } if (!mailId.Equals(""))//处理有图像文件的显示 { for (int jj = 0; jj < _frmComMail.mailImageRelations.Count; jj++) { if (_frmComMail.mailImageRelations[jj].MailId.Equals(mailId)) { isExistRelation = true; break; } } if (!isExistRelation) _frmComMail = _frmComMail.RefreshMails(0x01000, mailId);//增加本地内存中的mailImageRelation(即_frmComMail.mailImageRelations) for (int jj = 0; jj < _frmComMail.mailImageRelations.Count; jj++) { if (_frmComMail.mailImageRelations[jj].MailId.Equals(mailId)) { for (int kk = 0; kk < _frmComMail.mailImages.Count; kk++) { if (_frmComMail.mailImages[kk].ImageId.Equals(_frmComMail.mailImageRelations[jj].ImageId)) { isExistMailImage = true; break; } } } } if (!isExistMailImage) _frmComMail = _frmComMail.RefreshMails(0x00100, mailId);//增加本地内存中的mailImage(即_frmComMail.mailImages) for (int jj = 0; jj < _frmComMail.mailImageRelations.Count; jj++) { if (_frmComMail.mailImageRelations[jj].MailId.Equals(mailId)) { for (int kk = 0; kk < _frmComMail.mailImages.Count; kk++) { if (_frmComMail.mailImages[kk].ImageId.Equals(_frmComMail.mailImageRelations[jj].ImageId)) { string base64String = Convert.ToBase64String(_frmComMail.mailImages[kk].Image); string replaceString = "data:image/" + _frmComMail.mailImages[kk].ImageType + "; base64," + base64String; mailContent = mailContent.Replace(_frmComMail.mailImages[kk].ImageId, replaceString); } } } } } return mailContent; }
上述代码段核心内容为:
string base64String = Convert.ToBase64String(_frmComMail.mailImages[kk].Image); string replaceString = "data:image/" + _frmComMail.mailImages[kk].ImageType + "; base64," + base64String; mailContent = mailContent.Replace(_frmComMail.mailImages[kk].ImageId, replaceString);