/////////////////////////////////////////////////////////////////////// private static string convert_uploaded_blob_to_flat_file(string upload_folder, int attachment_bpid, Dictionary<string, int> files_to_delete) { byte[] buffer = new byte[16 * 1024]; string dest_path_and_filename; Bug.BugPostAttachment bpa = Bug.get_bug_post_attachment(attachment_bpid); using (bpa.content) { dest_path_and_filename = Path.Combine(upload_folder, bpa.file); // logic to rename in case of dupes. MS Outlook embeds images all with the same filename int suffix = 0; string renamed_to_prevent_dupe = dest_path_and_filename; while (files_to_delete.ContainsKey(renamed_to_prevent_dupe)) { suffix++; renamed_to_prevent_dupe = Path.Combine(upload_folder, Path.GetFileNameWithoutExtension(bpa.file) + Convert.ToString(suffix) + Path.GetExtension(bpa.file)); } dest_path_and_filename = renamed_to_prevent_dupe; // Save to disk using (FileStream out_stream = new FileStream( dest_path_and_filename, FileMode.CreateNew, FileAccess.Write, FileShare.Delete)) { int bytes_read = bpa.content.Read(buffer, 0, buffer.Length); while (bytes_read != 0) { out_stream.Write(buffer, 0, bytes_read); bytes_read = bpa.content.Read(buffer, 0, buffer.Length); } out_stream.Close(); } } files_to_delete[dest_path_and_filename] = 1; return dest_path_and_filename; }
/////////////////////////////////////////////////////////////////////// public static string send_email( string to, string from, string cc, string subject, string body, System.Web.Mail.MailFormat body_format, System.Web.Mail.MailPriority priority, int[] attachment_bpids, bool return_receipt) { Dictionary<string,int> files_to_delete = new Dictionary<string,int>(); ArrayList directories_to_delete = new ArrayList(); System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage(); msg.To = to; msg.From = from; if (!string.IsNullOrEmpty(cc.Trim())) { msg.Cc = cc; } msg.Subject = subject; msg.Priority = priority; // 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.BodyFormat = body_format; string smtp_server = Util.get_setting("SmtpServer", ""); if (smtp_server != "") { System.Web.Mail.SmtpMail.SmtpServer = smtp_server; } string smtp_password = Util.get_setting("SmtpServerAuthenticatePassword", ""); if (smtp_password != "") { msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = smtp_password; msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = Util.get_setting("SmtpServerAuthenticateUser", ""); } string smtp_pickup = Util.get_setting("SmtpServerPickupDirectory", ""); if (smtp_pickup != "") { msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory"] = smtp_pickup; } string send_using = Util.get_setting("SmtpSendUsing", ""); if (send_using != "") { msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = send_using; } string smtp_use_ssl = Util.get_setting("SmtpUseSSL", ""); if (smtp_use_ssl == "1") { msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true"; } string smtp_server_port = Util.get_setting("SmtpServerPort", ""); if (smtp_server_port != "") { msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = smtp_server_port; } if (attachment_bpids != null && attachment_bpids.Length > 0) { 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); directories_to_delete.Add(upload_folder); } foreach (int attachment_bpid in attachment_bpids) { byte[] buffer = new byte[16 * 1024]; string dest_path_and_filename; Bug.BugPostAttachment bpa = Bug.get_bug_post_attachment(attachment_bpid); using (bpa.content) { dest_path_and_filename = Path.Combine(upload_folder, bpa.file); // logic to rename in case of dupes. MS Outlook embeds images all with the same filename int suffix = 0; string renamed_to_prevent_dupe = dest_path_and_filename; while (files_to_delete.ContainsKey(renamed_to_prevent_dupe)) { suffix++; renamed_to_prevent_dupe = dest_path_and_filename + Convert.ToString(suffix); } dest_path_and_filename = renamed_to_prevent_dupe; // Save to disk using (FileStream out_stream = new FileStream( dest_path_and_filename, FileMode.CreateNew, FileAccess.Write, FileShare.None)) { int bytes_read = bpa.content.Read(buffer, 0, buffer.Length); while (bytes_read != 0) { out_stream.Write(buffer, 0, bytes_read); bytes_read = bpa.content.Read(buffer, 0, buffer.Length); } } } // Add saved file as attachment System.Web.Mail.MailAttachment mail_attachment = new System.Web.Mail.MailAttachment( dest_path_and_filename, System.Web.Mail.MailEncoding.Base64); msg.Attachments.Add(mail_attachment); files_to_delete[dest_path_and_filename] = 1; } } try { // This fixes a bug for some people. Not sure how it happens.... msg.Body = msg.Body.Replace(Convert.ToChar(0), ' ').Trim(); System.Web.Mail.SmtpMail.Send(msg); // We delete late here because testing showed that SmtpMail class // got confused when we deleted too soon. foreach (string file in files_to_delete.Keys) { File.Delete(file); } foreach (string directory in directories_to_delete) { Directory.Delete(directory); } 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()); return (e.GetBaseException().Message); } }