-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAjaxMethods.cs
177 lines (146 loc) · 7.68 KB
/
AjaxMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using SuperOffice.Diagnostics;
using SuperOffice.Data;
using SuperOffice.Globalization;
using SuperOffice.CRM.Services;
using SuperOffice.CRM.Web.Data;
using SuperOffice.DCF.Web;
using SuperOffice.DCF.Web.Factory;
namespace Ctse.SO.AjaxTest
{
[SoWebObject("AjaxMethods")]
public class AjaxMethods : IWebObject
{
public Response GenerateEmailWithAttachments(string mailTemplateFilename, int contactId, int personId, int projectId, int saleId, string attachmentFileNames)
{
try
{
// validate template file if it is a mime file (.eml, .som or .somail)
var allowedExt = new string[] { ".eml", ".som", ".somail" };
var ext = System.IO.Path.GetExtension(mailTemplateFilename).ToLower();
if (allowedExt.Contains(ext))
throw new Exception(mailTemplateFilename + " is propably not a valid mime file");
string cultName = ResourceManager.GetCulture(); // "nl-NL" or "en-US" or ...
using (new CultureSettingHelper(new System.Globalization.CultureInfo(cultName, false)))
{
using (DocumentAgent docAgent = new DocumentAgent())
{
// get content of mail template
var templateStream = docAgent.GetTemplateStream(mailTemplateFilename, true, cultName);
if (templateStream == null)
throw new Exception("Template " + mailTemplateFilename + " not found.");
string emlContent = string.Empty;
using (StreamReader streamReader = new StreamReader(templateStream, Encoding.Default, true))
emlContent = streamReader.ReadToEnd();
if (!string.IsNullOrWhiteSpace(emlContent))
{
// parse mail template with variables
//var tvh = new TemplateVariablesHandler(contactId, personId, 0, 0, saleId, 0, projectId, 0, 0, "");
//emlContent = tvh.SubstituteTemplateVariables(emlContent, GeneratorEncoding.Mime);
emlContent = docAgent.SubstituteTemplateVariables(emlContent, GeneratorEncoding.Mime, contactId, personId, 0, 0, saleId, 0, projectId, cultName);
// get attachment(s) to add (from template folder)
FileNameAndContent[] attachments = null;
if (!string.IsNullOrWhiteSpace(attachmentFileNames))
{
attachments = attachmentFileNames.Split(',').Select(attachmentFilename =>
{
var attachmentStream = docAgent.GetTemplateStream(attachmentFilename, true, cultName);
if (attachmentStream == null)
throw new Exception("Attachment " + attachmentFilename + " not found.");
return new FileNameAndContent
{
FileName = attachmentFilename,
Content = attachmentStream.ToByteArray()
};
}).ToArray();
}
// generate valid eml-filename (otherwise the client will not recognize the file type if it is .som or .somail)
var emlFilename = System.IO.Path.ChangeExtension(Utils.GetValidFileName(mailTemplateFilename), ".eml");
// generate eml-file with attachment(s)
var emlFile = Utils.GenerateEmlFileWithAttachment(
new FileNameAndContent { FileName = emlFilename, Content = System.Text.Encoding.UTF8.GetBytes(emlContent) },
attachments
);
// return the response to the client
return new Response { FileName = emlFile.FileName, Content = System.Convert.ToBase64String(emlFile.Content) };
}
else
{
throw new Exception("Mail template content is empty");
}
}
}
}
catch (Exception ex)
{
var ex1 = ex;
string err = ex1.Message;
while (ex1.InnerException != null)
{
ex1 = ex1.InnerException;
err = err + ";" + ex1.Message;
}
SoLogger.Logger.LogException(EventLogEntryType.Warning, new Exception(err));
return new Response { Message = ApplicationUtility.FormatExceptionMessage(err) };
}
}
}
public class Response
{
public string Message { get; set; }
public string FileName { get; set; }
public string Content { get; set; }
}
public class FileNameAndContent
{
public string FileName { get; set; }
public byte[] Content { get; set; }
}
public class Utils
{
public static string GetValidFileName(string fileName, string replChar = null)
{
if (replChar == null)
replChar = string.Empty;
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), replChar));
}
public static FileNameAndContent GenerateEmlFileWithAttachment(FileNameAndContent emlFile, FileNameAndContent[] attachments)
{
// load eml content
LumiSoft.Net.Mime.Mime m = LumiSoft.Net.Mime.Mime.Parse(emlFile.Content);
// change contenttype
var mainEntity = m.MainEntity;
mainEntity.ContentType = LumiSoft.Net.Mime.MediaType_enum.Multipart_mixed;
// add attachment(s)
if (attachments != null)
{
foreach (var attachment in attachments)
{
MemoryStream stream = new MemoryStream(attachment.Content);
LumiSoft.Net.Mime.MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
attachmentEntity.ContentType = LumiSoft.Net.Mime.MediaType_enum.Application_octet_stream;
attachmentEntity.ContentDisposition = LumiSoft.Net.Mime.ContentDisposition_enum.Attachment;
attachmentEntity.ContentTransferEncoding = LumiSoft.Net.Mime.ContentTransferEncoding_enum.Base64;
attachmentEntity.ContentDisposition_FileName = attachment.FileName;
attachmentEntity.DataFromStream(stream);
}
}
// set defaults
mainEntity.Date = DateTime.Now;
if (!mainEntity.Header.Contains("X-Unsent"))
{
mainEntity.Header.Add(new LumiSoft.Net.Mime.HeaderField("X-Unsent", "1"));
}
else
{
var unsent = mainEntity.Header.GetFirst("X-Unsent");
unsent.Value = "1";
}
return new FileNameAndContent { FileName = emlFile.FileName, Content = m.ToByteData() };
}
}
}