public static void SetBodyEncoding(this SendEMailCommand command, Encoding encoding)
        {
            Guard.AgainstNull(command, "command");
            Guard.AgainstNull(encoding, "encoding");

            command.BodyEncoding = encoding.HeaderName;
        }
        public static Encoding GetSubjectEncoding(this SendEMailCommand command)
        {
            Guard.AgainstNull(command, "command");

            if (string.IsNullOrEmpty(command.SubjectEncoding)
                ||
                command.SubjectEncoding.Equals("default", StringComparison.InvariantCultureIgnoreCase)
                ||
                command.SubjectEncoding.Equals("none", StringComparison.InvariantCultureIgnoreCase)
                ||
                command.SubjectEncoding.Equals("empty", StringComparison.InvariantCultureIgnoreCase)
                ||
                command.SubjectEncoding.Equals("unknown", StringComparison.InvariantCultureIgnoreCase))
            {
                return(null);
            }

            try
            {
                return(Encoding.GetEncoding(command.SubjectEncoding));
            }
            catch
            {
                return(null);
            }
        }
        public static MailPriority GetMailPriority(this SendEMailCommand command)
        {
            Guard.AgainstNull(command, "command");

            try
            {
                return((MailPriority)Enum.Parse(typeof(MailPriority), command.Priority));
            }
            catch (Exception)
            {
                return(MailPriority.Normal);
            }
        }
Ejemplo n.º 4
0
        public string AddAttachment(SendEMailCommand command, string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(string.Format("File '{0}' does not exist.  Could not add e-mail attachment.", path));
            }

            var name = Guid.NewGuid().ToString("n");
            var file = string.Format("{0}{1}", name, Path.GetExtension(path));
            var targetPath = Path.Combine(configuration.AttachmentFolder, file);

            try
            {
                File.Copy(path, targetPath, true);
            }
            catch (Exception ex)
            {
                throw new EMailException(string.Format("File '{0}' not added as attachment.  Exception reported: {1}", path, ex.CompactMessages()));
            }

            var log = new StringBuilder();

            log.AppendFormat("Date created\t{0}", DateTime.Now.ToString("dd MMM yyyy HH:mm:ss"));
            log.AppendLine();
            log.AppendFormat("Source file path:\t{0}", path);
            log.AppendLine();
            log.AppendFormat("Source machine\t{0}", Environment.MachineName);
            log.AppendLine();
            log.AppendFormat("Source identity\t{0}", string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName));
            log.AppendLine();

            File.WriteAllText(Path.Combine(configuration.AttachmentFolder, string.Format("{0}.log", name)), log.ToString());

            command.Attachments.Add(file);

            return file;
        }
        public static bool HasSubjectEncoding(this SendEMailCommand command)
        {
            Guard.AgainstNull(command, "command");

            return(command.GetSubjectEncoding() != null);
        }
        public static bool HasBodyEncoding(this SendEMailCommand command)
        {
            Guard.AgainstNull(command, "command");

            return(command.BodyEncoding != null);
        }
        public static void SetMailPriority(this SendEMailCommand command, MailPriority priority)
        {
            Guard.AgainstNull(command, "command");

            command.Priority = priority.ToString();
        }