Beispiel #1
0
        /// send an email to the applicant and the registration office
        public static bool SendEmail(Int64 APartnerKey, string ACountryCode, TApplicationFormData AData, string APDFFilename)
        {
            string FileName = TFormLettersTools.GetRoleSpecificFile(TAppSettingsManager.GetValue("Formletters.Path"),
                                                                    "ApplicationReceivedEmail",
                                                                    AData.registrationcountrycode,
                                                                    AData.formsid,
                                                                    "html");

            string HTMLText      = string.Empty;
            string SenderAddress = string.Empty;
            string BCCAddress    = string.Empty;
            string EmailSubject  = string.Empty;

            if (!File.Exists(FileName))
            {
                HTMLText = "<html><body>" + String.Format("Cannot find file {0}", FileName) + "</body></html>";
            }
            else
            {
                StreamReader r = new StreamReader(FileName);
                SenderAddress = r.ReadLine();
                BCCAddress    = r.ReadLine();
                EmailSubject  = r.ReadLine();
                HTMLText      = r.ReadToEnd();
                r.Close();
            }

            if (!SenderAddress.StartsWith("From: "))
            {
                throw new Exception("missing From: line in the Email template " + FileName);
            }

            if (!BCCAddress.StartsWith("BCC: "))
            {
                throw new Exception("missing BCC: line in the Email template " + FileName);
            }

            if (!EmailSubject.StartsWith("Subject: "))
            {
                throw new Exception("missing Subject: line in the Email template " + FileName);
            }

            SenderAddress = SenderAddress.Substring("From: ".Length);
            BCCAddress    = BCCAddress.Substring("BCC: ".Length);
            EmailSubject  = EmailSubject.Substring("Subject: ".Length);

            HTMLText = TJsonTools.ReplaceKeywordsWithData(AData.RawData, HTMLText);
            HTMLText = HTMLText.Replace("#HTMLRAWDATA", TJsonTools.DataToHTMLTable(AData.RawData));

            // load the language file for the specific country
            Catalog.Init(ACountryCode, ACountryCode);

            // send email
            TSmtpSender emailSender = new TSmtpSender();

            MailMessage msg = new MailMessage(SenderAddress,
                                              AData.email,
                                              EmailSubject,
                                              HTMLText);

            msg.Attachments.Add(new Attachment(APDFFilename, System.Net.Mime.MediaTypeNames.Application.Octet));
            msg.Bcc.Add(BCCAddress);

            if (!emailSender.SendMessage(msg))
            {
                TLogging.Log("There has been a problem sending the email to " + AData.email);
                return(false);
            }

            return(true);
        }
        public static String ExportToIntranet(Boolean AExportDonationData, Boolean AExportFieldData, Boolean AExportPersonData,
                                              String AServerEmailAddress, String APswd, Int32 ADaySpan, String AOptionalMetadata, String ReplyToEmail)
        {
            try
            {
                FZipFileNames.Clear();
                FExportFilePath = TAppSettingsManager.GetValue("OpenPetra.PathTemp") + @"\";
                FExportTrace    = "Exporting (Temporary path: " + FExportFilePath + ")\r\n";
                FTransaction    = DBAccess.GDBAccessObj.BeginTransaction(IsolationLevel.ReadCommitted);

                if (AExportDonationData)
                {
                    ExportDonations(ADaySpan);
                    AddZipFile("donor.csv");
                    AddZipFile("donation.csv");
                    AddZipFile("recipient.csv");
                }

                if (AExportFieldData)
                {
                    if (ExportField())
                    {
                        AddZipFile("field.csv");
                    }
                }

                if (AExportPersonData)
                {
                    ExportPersonnel();
                    AddZipFile("position.csv");
                    AddZipFile("person.csv");
                    AddZipFile("email.csv");
                }

                ExportMetadata(AOptionalMetadata, APswd);
                AddZipFile("metadata.csv");

                MemoryStream ZippedStream = TFileHelper.Streams.Compression.DeflateFilesIntoMemoryStream(FZipFileNames.ToArray(), false, "");
                TFileHelper.Streams.FileHandling.SaveStreamToFile(ZippedStream, FExportFilePath + "data.zip");
                FExportTrace += "\r\nFiles compressed to " + FExportFilePath + "data.zip.";

                if (EncryptUsingPublicKey("data.zip", AServerEmailAddress))
                {
                    TSmtpSender SendMail = new TSmtpSender(
                        TUserDefaults.GetStringDefault("SmtpHost"),
                        TUserDefaults.GetInt16Default("SmtpPort"),
                        TUserDefaults.GetBooleanDefault("SmtpUseSsl"),
                        TUserDefaults.GetStringDefault("SmtpUser"),
                        TUserDefaults.GetStringDefault("SmtpPassword"),
                        "");
                    String SenderAddress = ReplyToEmail;

                    MailMessage msg = new MailMessage(SenderAddress,
                                                      AServerEmailAddress,
                                                      "Data from OpenPetra",
                                                      "Here is the latest data from my field.");

                    msg.Attachments.Add(new Attachment(FExportFilePath + "data.zip.gpg"));

                    if (SendMail.SendMessage(msg))
                    {
                        FExportTrace += ("\r\nEmail sent to " + msg.To[0].Address);
                    }
                    else
                    {
                        FExportTrace += ("\r\nNo Email was sent.");
                    }

                    msg.Dispose(); // If I don't call this, the attached files are still locked!
                }
                else
                {
                    FExportTrace += "\r\nError: Data encryption failed.";
                }
            }
            catch (Exception e)
            {
                FExportTrace += ("\r\nException: " + e.Message);
            }
            finally
            {
                DBAccess.GDBAccessObj.RollbackTransaction();
                DeleteTemporaryFiles();
                DonorList.Clear();
                RecipientList.Clear();  // These lists are static so they'll stick around for ever,
                                        // but I don't need to keep the data which is taking up memory.
            }
            return(FExportTrace);
        }