protected static void actually_delete_stuff(StuffToDelete stuff_to_delete) { if (stuff_to_delete == null) // not sure how this could happen, but it fixed a bug for one guy return; stuff_to_delete.msg.Dispose(); // if we don't do this, the delete tends not to work. foreach (string file in stuff_to_delete.files_to_delete.Keys) { File.Delete(file); } foreach (string directory in stuff_to_delete.directories_to_delete) { Directory.Delete(directory); } }
protected static void delete_stuff(StuffToDelete stuff_to_delete) { System.Threading.Thread thread = new System.Threading.Thread(threadproc_delete_stuff); thread.Start(stuff_to_delete); }
/////////////////////////////////////////////////////////////////////// public static string send_email( string to, string from, string cc, string subject, string body, MailFormat body_format, MailPriority priority, int[] attachment_bpids, bool return_receipt) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(from); Email.add_addresses_to_email(msg, to, AddrType.to); if (!string.IsNullOrEmpty(cc.Trim())) { Email.add_addresses_to_email(msg, cc, AddrType.cc); } msg.Subject = subject; if (priority == MailPriority.Normal) { msg.Priority = System.Net.Mail.MailPriority.Normal; } else if (priority == MailPriority.High) { msg.Priority = System.Net.Mail.MailPriority.High; } else { priority = MailPriority.Low; } // This fixes a bug for a couple people, but make it configurable, just in case. if (Util.get_setting("BodyEncodingUTF8", "1") == "1") { msg.BodyEncoding = Encoding.UTF8; } if (return_receipt) { msg.Headers.Add("Disposition-Notification-To", from); } // workaround for a bug I don't understand... if (Util.get_setting("SmtpForceReplaceOfBareLineFeeds", "0") == "1") { body = body.Replace("\n", "\r\n"); } msg.Body = body; msg.IsBodyHtml = body_format == MailFormat.Html; StuffToDelete stuff_to_delete = null; if (attachment_bpids != null && attachment_bpids.Length > 0) { stuff_to_delete = new StuffToDelete(); string upload_folder = btnet.Util.get_upload_folder(); if (string.IsNullOrEmpty(upload_folder)) { upload_folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(upload_folder); stuff_to_delete.directories_to_delete.Add(upload_folder); } foreach (int attachment_bpid in attachment_bpids) { string dest_path_and_filename = convert_uploaded_blob_to_flat_file(upload_folder, attachment_bpid, stuff_to_delete.files_to_delete); // Add saved file as attachment System.Net.Mail.Attachment mail_attachment = new System.Net.Mail.Attachment( dest_path_and_filename); msg.Attachments.Add(mail_attachment); } } try { // This fixes a bug for some people. Not sure how it happens.... msg.Body = msg.Body.Replace(Convert.ToChar(0), ' ').Trim(); System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); // SSL or not string force_ssl = Util.get_setting("SmtpForceSsl", ""); if (force_ssl == "") { // get the port so that we can guess whether SSL or not System.Net.Configuration.SmtpSection smtpSec = (System.Net.Configuration.SmtpSection) System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp"); if (smtpSec.Network.Port == 465 || smtpSec.Network.Port == 587) { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } else { if (force_ssl == "1") { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } // Ignore certificate errors if (smtpClient.EnableSsl) { ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; } smtpClient.Send(msg); if (stuff_to_delete != null) { stuff_to_delete.msg = msg; delete_stuff(stuff_to_delete); } return(""); } catch (Exception e) { Util.write_to_log("There was a problem sending email. Check settings in Web.config."); Util.write_to_log("TO:" + to); Util.write_to_log("FROM:" + from); Util.write_to_log("SUBJECT:" + subject); Util.write_to_log(e.GetBaseException().Message.ToString()); delete_stuff(stuff_to_delete); return(e.GetBaseException().Message); } }
/////////////////////////////////////////////////////////////////////// public static string send_email( string to, string from, string cc, string subject, string body, BtnetMailFormat body_format, BtnetMailPriority priority, int[] attachment_bpids, bool return_receipt) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(from); Email.add_addresses_to_email(msg, to, AddrType.to); if (!string.IsNullOrEmpty(cc.Trim())) { Email.add_addresses_to_email(msg, cc, AddrType.cc); } msg.Subject = subject; if (priority == BtnetMailPriority.Normal) msg.Priority = System.Net.Mail.MailPriority.Normal; else if (priority == BtnetMailPriority.High) msg.Priority = System.Net.Mail.MailPriority.High; else priority = BtnetMailPriority.Low; // This fixes a bug for a couple people, but make it configurable, just in case. if (Util.get_setting("BodyEncodingUTF8", "1") == "1") { msg.BodyEncoding = Encoding.UTF8; } if (return_receipt) { msg.Headers.Add("Disposition-Notification-To", from); } // workaround for a bug I don't understand... if (Util.get_setting("SmtpForceReplaceOfBareLineFeeds", "0") == "1") { body = body.Replace("\n", "\r\n"); } msg.Body = body; msg.IsBodyHtml = body_format == BtnetMailFormat.Html; StuffToDelete stuff_to_delete = null; if (attachment_bpids != null && attachment_bpids.Length > 0) { stuff_to_delete = new StuffToDelete(); string upload_folder = btnet.Util.get_upload_folder(); if (string.IsNullOrEmpty(upload_folder)) { upload_folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(upload_folder); stuff_to_delete.directories_to_delete.Add(upload_folder); } foreach (int attachment_bpid in attachment_bpids) { string dest_path_and_filename = convert_uploaded_blob_to_flat_file(upload_folder, attachment_bpid, stuff_to_delete.files_to_delete); // Add saved file as attachment System.Net.Mail.Attachment mail_attachment = new System.Net.Mail.Attachment( dest_path_and_filename); msg.Attachments.Add(mail_attachment); } } try { // This fixes a bug for some people. Not sure how it happens.... msg.Body = msg.Body.Replace(Convert.ToChar(0), ' ').Trim(); System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); // SSL or not string force_ssl = Util.get_setting("SmtpForceSsl", ""); if (force_ssl == "") { // get the port so that we can guess whether SSL or not System.Net.Configuration.SmtpSection smtpSec = (System.Net.Configuration.SmtpSection) System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp"); if (smtpSec.Network.Port == 465 || smtpSec.Network.Port == 587) { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } else { if (force_ssl == "1") { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } // Ignore certificate errors ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; smtpClient.Send(msg); if (stuff_to_delete != null) { stuff_to_delete.msg = msg; delete_stuff(stuff_to_delete); } return ""; } catch (Exception e) { Util.write_to_log("There was a problem sending email. Check settings in Web.config."); Util.write_to_log("TO:" + to); Util.write_to_log("FROM:" + from); Util.write_to_log("SUBJECT:" + subject); Util.write_to_log(e.GetBaseException().Message.ToString()); delete_stuff(stuff_to_delete); return (e.GetBaseException().Message); } }