/// <summary> /// Sends the auto reply. /// </summary> /// <param name="IncidentId">The incident id.</param> /// <param name="From">From.</param> /// <param name="To">To.</param> internal static void SendAutoReply(int IncidentId, string From, string To) { IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId)); if (!incidentBox.Document.EMailRouterBlock.SendAutoReply) { return; } Alerts2.Message msg = Alerts2.GetMessage(incidentBox.Document.GeneralBlock.AutoReplyEMailSubjectTemplate, incidentBox.Document.GeneralBlock.AutoReplyEMailBodyTemplate, SystemEventTypes.Issue_Created, IncidentId, null, -1, Security.CurrentUser.UserID); string subject = msg.Subject, body = msg.Body; MemoryStream emlMessage = new MemoryStream(); byte[] tmpBuffer = null; #region Fill Pop3 Message Stream // Create Pop3 Message Headers StringBuilder sbHeaders = new StringBuilder(); sbHeaders.AppendFormat("Date: {0}", GetEMailCreationDate()).Append("\r\n"); sbHeaders.Append("From: [email protected]").Append("\r\n"); sbHeaders.Append("To: [email protected]").Append("\r\n"); sbHeaders.AppendFormat("Subject: {0}", Rfc822HeaderCollection.Encode2AsciiString(subject)).Append("\r\n"); sbHeaders.Append("MIME-Version: 1.0").Append("\r\n"); sbHeaders.Append("Content-Type: multipart/mixed; boundary=\"----------7E143249668A83E\"").Append("\r\n"); sbHeaders.Append("\r\n"); tmpBuffer = Encoding.ASCII.GetBytes(sbHeaders.ToString()); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); // Create Pop3 Message Entry StringBuilder sbMessage = new StringBuilder(); sbMessage.Append("------------7E143249668A83E").Append("\r\n"); // IF MESSAGE IS PLAIN TEXT //sbMessage.Append("Content-Type: text/plain; charset=utf-8").Append("\r\n"); // IF MESSAGE IS HTML TEXT sbMessage.Append("Content-Type: text/html; charset=utf-8").Append("\r\n"); sbMessage.Append("Content-Transfer-Encoding: base64").Append("\r\n"); sbMessage.Append("\r\n"); // OZ Fixed 500 5.3.3 Line too long (in reply to end of DATA command) sbMessage.Append(Convert.ToBase64String(Encoding.UTF8.GetBytes(body), Base64FormattingOptions.InsertLineBreaks)).Append("\r\n"); tmpBuffer = Encoding.ASCII.GetBytes(sbMessage.ToString()); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); // Add Final Line tmpBuffer = Encoding.ASCII.GetBytes("------------7E143249668A83E--\r\n\r\n"); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); #endregion Pop3Message InMsg = new Pop3Message(emlMessage); OutputMessageCreator outputAutoReply = new OutputMessageCreator(InMsg, IncidentId, From, From); outputAutoReply.AddRecipient(To); int emailBoxId = EMail.EMailRouterOutputMessage.FindEMailRouterPublicId(IncidentId); foreach (OutputMessage outputMsg in outputAutoReply.Create()) { try { SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, emailBoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); Log.WriteError(ex.ToString()); } } }
internal static Pop3Message Create(string From, string[] To, string Subject, string Body, Mediachase.IBN.Business.ControlSystem.DirectoryInfo Attachments) { MemoryStream emlMessage = new MemoryStream(); byte[] tmpBuffer = null; string ToEmail = string.Join(", ", To); #region Fill Pop3 Message Stream // Create Pop3 Message Headers StringBuilder sbHeaders = new StringBuilder(); sbHeaders.AppendFormat("Date: {0}", DateTime.UtcNow.ToString("r")).Append("\r\n"); sbHeaders.AppendFormat("From: {0}", Rfc822HeaderCollection.Encode2AsciiString(From)).Append("\r\n"); sbHeaders.AppendFormat("To: {0}", Rfc822HeaderCollection.Encode2AsciiString(ToEmail)).Append("\r\n"); sbHeaders.AppendFormat("Subject: {0}", Rfc822HeaderCollection.Encode2AsciiString(Subject)).Append("\r\n"); sbHeaders.Append("MIME-Version: 1.0").Append("\r\n"); sbHeaders.Append("Content-Type: multipart/mixed; boundary=\"----------7E143249668A83E\"").Append("\r\n"); sbHeaders.Append("\r\n"); tmpBuffer = Encoding.ASCII.GetBytes(sbHeaders.ToString()); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); // Create Pop3 Message Entry StringBuilder sbMessage = new StringBuilder(); sbMessage.Append("------------7E143249668A83E").Append("\r\n"); // IF MESSAGE IS PLAIN TEXT //sbMessage.Append("Content-Type: text/plain; charset=utf-8").Append("\r\n"); // IF MESSAGE IS HTML TEXT sbMessage.Append("Content-Type: text/html; charset=utf-8").Append("\r\n"); sbMessage.Append("Content-Transfer-Encoding: base64").Append("\r\n"); sbMessage.Append("\r\n"); string FullMessage = Body; sbMessage.Append(Convert.ToBase64String(Encoding.UTF8.GetBytes(FullMessage), Base64FormattingOptions.InsertLineBreaks)).Append("\r\n"); tmpBuffer = Encoding.ASCII.GetBytes(sbMessage.ToString()); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); if (Attachments != null) { Hashtable contentTypeHash = new Hashtable(); using (IDataReader reader = ContentType.GetListContentTypes()) { while (reader.Read()) { contentTypeHash.Add(((string)reader["Extension"]).ToLower(), (string)reader["ContentTypeString"]); } } // Add Pop3 Message Attachements foreach (Mediachase.IBN.Business.ControlSystem.FileInfo fileInfo in Attachments.GetFiles()) { StringBuilder sbFile = new StringBuilder(); sbFile.Append("------------7E143249668A83E").Append("\r\n"); sbFile.AppendFormat("Content-Type: {0}; name=\"{1}\"", fileInfo.FileBinaryContentType, Rfc822HeaderCollection.Encode2AsciiString(fileInfo.Name)).Append("\r\n"); sbFile.Append("Content-Transfer-Encoding: base64").Append("\r\n"); sbFile.AppendFormat("Content-Disposition: attachment; filename=\"{0}\"", Rfc822HeaderCollection.Encode2AsciiString(fileInfo.Name)).Append("\r\n"); sbFile.Append("\r\n"); using (MemoryStream fs = new MemoryStream()) { FileStorage.LightLoadFile(fileInfo, fs); fs.Capacity = (int)fs.Length; sbFile.Append(Convert.ToBase64String(fs.GetBuffer(), Base64FormattingOptions.InsertLineBreaks)); } sbFile.Append("\r\n"); tmpBuffer = Encoding.ASCII.GetBytes(sbFile.ToString()); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); } } // Add Final Line tmpBuffer = Encoding.ASCII.GetBytes("------------7E143249668A83E--\r\n\r\n"); emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length); #endregion return(new Pop3Message(emlMessage)); }