EmailExport.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Text;
  3. using System.Net.Mail;
  4. using System.Net;
  5. using System.IO;
  6. using FastReport.Utils;
  7. namespace FastReport.Export.Email
  8. {
  9. /// <summary>
  10. /// Represents the email export.
  11. /// </summary>
  12. /// <remarks>
  13. /// In order to use this class, you need to set up at least the following properties:
  14. /// <see cref="Address"/>, <see cref="Subject"/>, <see cref="Account"/>. Use the <see cref="Export"/>
  15. /// property to choose the format of an attachment. If you leave it empty, the attachment will be
  16. /// in the .FRP format (FastReport prepared report). When you done with settings, call the
  17. /// <see cref="SendEmail"/> method to send an email.
  18. /// </remarks>
  19. /// <example>
  20. /// This example demonstrates the bare minimum required to send an email.
  21. /// <code>
  22. /// EmailExport export = new EmailExport();
  23. /// export.Account.Address = "my@address.net";
  24. /// export.Account.Host = "myhost";
  25. /// export.Address = "recipient@address.net";
  26. /// export.Subject = "Re: analysis report";
  27. /// // the report1 report must be prepared at this moment
  28. /// export.SendEmail(report1);
  29. /// </code>
  30. /// </example>
  31. public partial class EmailExport : IFRSerializable
  32. {
  33. #region Fields
  34. private string address;
  35. private string[] fcc;
  36. private string subject;
  37. private string messageBody;
  38. private string nameAttachmentFile;
  39. private ExportBase export;
  40. private EmailSettings account;
  41. private Report report;
  42. #endregion
  43. #region Properties
  44. /// <summary>
  45. /// Gets or sets the recipient's address.
  46. /// </summary>
  47. /// <remarks>
  48. /// This property must contain value in form "john@url.com".
  49. /// </remarks>
  50. public string Address
  51. {
  52. get { return address; }
  53. set { address = value; }
  54. }
  55. /// <summary>
  56. /// Gets or sets the carbon copy adresses.
  57. /// </summary>
  58. /// <remarks>
  59. /// This property must contain an array of values in form "john@url.com".
  60. /// </remarks>
  61. public string[] CC
  62. {
  63. get { return fcc; }
  64. set { fcc = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the subject of the message.
  68. /// </summary>
  69. public string Subject
  70. {
  71. get { return subject; }
  72. set { subject = value; }
  73. }
  74. /// <summary>
  75. /// Gets or sets the message body.
  76. /// </summary>
  77. public string MessageBody
  78. {
  79. get { return messageBody; }
  80. set { messageBody = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the name attachment file.
  84. /// </summary>
  85. public string NameAttachmentFile
  86. {
  87. get { return nameAttachmentFile; }
  88. set { nameAttachmentFile = value; }
  89. }
  90. /// <summary>
  91. /// Gets or sets the export filter which will be used to export a report.
  92. /// </summary>
  93. /// <remarks>
  94. /// Set this property to instance of any export filter. When you send the email, the report
  95. /// will be exported using that export filter.
  96. /// <para/>By default, this property is set to <b>null</b>. In this case the report will be send
  97. /// in .FRP format.
  98. /// </remarks>
  99. public ExportBase Export
  100. {
  101. get { return export; }
  102. set { export = value; }
  103. }
  104. /// <summary>
  105. /// Gets the email account settings such as host, user name, password.
  106. /// </summary>
  107. public EmailSettings Account
  108. {
  109. get { return account; }
  110. set
  111. {
  112. if (value == null)
  113. throw new ArgumentNullException("Account");
  114. account = value;
  115. }
  116. }
  117. /// <summary>
  118. /// Gets the parent Report object
  119. /// </summary>
  120. public Report Report
  121. {
  122. get { return report; }
  123. }
  124. #endregion
  125. #region Public Methods
  126. /// <summary>
  127. /// Sends an email.
  128. /// </summary>
  129. /// <param name="reports">Reports that will be sent as attachments.</param>
  130. /// <remarks>
  131. /// Before using this method, set up the following properties (it's a bare minimum):
  132. /// <see cref="Address"/>, <see cref="Subject"/>, <see cref="Account"/>.
  133. /// <para/>The report that you pass in this method must be prepared using the <b>Prepare</b> method.
  134. /// </remarks>
  135. public void SendEmail(params Report[] reports)
  136. {
  137. if (reports == null || reports.Length == 0)
  138. return;
  139. SmtpClient email = new SmtpClient();
  140. using (MailMessage message = new MailMessage(new MailAddress(Account.Address, Account.Name), new MailAddress(Address)))
  141. {
  142. if (CC != null)
  143. foreach (string cc in CC)
  144. message.CC.Add(new MailAddress(cc));
  145. message.Subject = Subject;
  146. message.SubjectEncoding = Encoding.UTF8;
  147. message.Body = MessageBody;
  148. message.BodyEncoding = Encoding.UTF8;
  149. foreach (Report report in reports)
  150. {
  151. MemoryStream attachStream = new MemoryStream();
  152. // export the report
  153. if (Export != null)
  154. {
  155. Export.OpenAfterExport = false;
  156. if (Export.HasMultipleFiles)
  157. Export.ExportAndZip(report, attachStream);
  158. else
  159. Export.Export(report, attachStream);
  160. }
  161. else
  162. {
  163. report.PreparedPages.Save(attachStream);
  164. }
  165. // form an attachment name
  166. string attachName = nameAttachmentFile != "" ? nameAttachmentFile : report.FileName;
  167. if (String.IsNullOrEmpty(attachName))
  168. attachName = "Report";
  169. else
  170. attachName = Path.GetFileNameWithoutExtension(attachName);
  171. string extension = ".fpx";
  172. if (Export != null)
  173. {
  174. extension = Export.FileFilter;
  175. extension = extension.Substring(extension.LastIndexOf('.'));
  176. if (Export.HasMultipleFiles)
  177. extension = ".zip";
  178. }
  179. attachName += extension;
  180. attachStream.Position = 0;
  181. message.Attachments.Add(new Attachment(attachStream, attachName));
  182. }
  183. email.Host = Account.Host;
  184. email.Port = Account.Port;
  185. if (!String.IsNullOrEmpty(Account.UserName) && !String.IsNullOrEmpty(Account.Password))
  186. email.Credentials = new NetworkCredential(Account.UserName, Account.Password);
  187. if (Account.EnableSSL)
  188. email.EnableSsl = true;
  189. email.Send(message);
  190. }
  191. }
  192. /// <inheritdoc/>
  193. public void Serialize(FRWriter writer)
  194. {
  195. writer.WriteStr("Address", Address);
  196. writer.WriteStr("CC", CC == null ? "" : string.Join(";", CC));
  197. writer.WriteStr("Subject", Subject);
  198. writer.WriteStr("MessageBody", MessageBody);
  199. writer.WriteStr("AccountAddress", Account.Address);
  200. writer.WriteStr("AccountName", Account.Name);
  201. writer.WriteStr("AccountHost", Account.Host);
  202. writer.WriteInt("AccountPort", Account.Port);
  203. writer.WriteStr("AccountUserName", Crypter.EncryptString(Account.UserName));
  204. writer.WriteStr("AccountPassword", Crypter.EncryptString(Account.Password));
  205. writer.WriteStr("AccountMessageTemplate", Account.MessageTemplate);
  206. writer.WriteBool("AccountEnableSSL", Account.EnableSSL);
  207. }
  208. /// <inheritdoc/>
  209. public void Deserialize(FRReader reader)
  210. {
  211. Address = reader.ReadStr("Address");
  212. CC = reader.ReadStr("CC").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
  213. Subject = reader.ReadStr("Subject");
  214. MessageBody = reader.ReadStr("MessageBody");
  215. Account.Address = reader.ReadStr("AccountAddress");
  216. Account.Name = reader.ReadStr("AccountName");
  217. Account.Host = reader.ReadStr("AccountHost");
  218. int port;
  219. if (int.TryParse(reader.ReadStr("AccountPort"), out port))
  220. Account.Port = port;
  221. Account.UserName = Crypter.DecryptString(reader.ReadStr("AccountUserName"));
  222. Account.Password = Crypter.DecryptString(reader.ReadStr("AccountPassword"));
  223. Account.MessageTemplate = reader.ReadStr("AccountMessageTemplate");
  224. Account.EnableSSL = reader.ReadBool("AccountEnableSSL");
  225. }
  226. #endregion
  227. /// <summary>
  228. /// Initializes a new instance of the <see cref="EmailExport"/> class with default settings.
  229. /// </summary>
  230. public EmailExport(Report report)
  231. {
  232. this.report = report;
  233. address = "";
  234. subject = "";
  235. messageBody = "";
  236. account = new EmailSettings();
  237. }
  238. /// <summary>
  239. /// Initializes a new instance of the <see cref="EmailExport"/> class with default settings.
  240. /// </summary>
  241. public EmailExport()
  242. {
  243. address = "";
  244. subject = "";
  245. messageBody = "";
  246. account = new EmailSettings();
  247. }
  248. }
  249. }