Beispiel #1
0
        // Settings
        public static EmailToSend LoadOrNew()
        {
            if (File.Exists(Const.SETTINGS_FILENAME))
            {
                try
                {
                    OutputMessage("Loading settings");
                    XmlSerializer serializer = new XmlSerializer(typeof(EmailToSend));
                    FileStream    fs         = new FileStream(Const.SETTINGS_FILENAME, FileMode.Open);
                    EmailToSend   ets        = (EmailToSend)serializer.Deserialize(fs);
                    fs.Close();
                    return(ets);
                }
                catch (Exception ex)
                {
                    OutputMessage("Error loading settings");
                    OutputMessage(ex.GetType());
                    OutputMessage(ex.Message);
                }
            }

            // Couldn't load settings, init new
            OutputMessage("No settings to load");
            return(new EmailToSend());
        }
Beispiel #2
0
        public static void SaveSettings(EmailToSend ets)
        {
            OutputMessage("Saving settings");
            XmlSerializer serializer = new XmlSerializer(typeof(EmailToSend));
            StreamWriter  sw         = new StreamWriter(Const.SETTINGS_FILENAME);

            serializer.Serialize(sw, ets);
            sw.Close();
        }
Beispiel #3
0
        public MainWindow()
        {
            InitializeComponent();
            InitSmtpAuthOptions();

            // Capture output messages
            MyApp.OnOutputMessage += new MyApp.OutputMessageHandler(OnOutputMessage);

            //Try loading settings
            ets = MyApp.LoadOrNew();
            this.DataContext = ets;

            // Update the SMTP Auth field visibility
            UpdateSmtpAuthVisibility();

            MyApp.OutputMessage("Program started");
        }
Beispiel #4
0
        public void SendEmail(EmailToSend ets)
        {
            Msg($"=== Starting email send {DateTime.Now} ===");

            string            _messageContent = ets.MessageContent; // TODO: Just read the content provided right now, I'll do the import thing later.
            List <Attachment> attachments;

            Msg("Assembling message");
            MailMessage email = new MailMessage(
                new MailAddress(ets.MessageFrom),
                new MailAddress(ets.MessageTo)
                );

            if (!string.IsNullOrWhiteSpace(ets.MessageTo))
            {
                foreach (string t in ets.MessageTo.Split((char)';'))
                {
                    email.To.Add(t);
                }
            }
            if (!string.IsNullOrWhiteSpace(ets.MessageCC))
            {
                foreach (string t in ets.MessageCC.Split((char)';'))
                {
                    email.CC.Add(t);
                }
            }
            if (!string.IsNullOrWhiteSpace(ets.MessageBCC))
            {
                foreach (string t in ets.MessageBCC.Split((char)';'))
                {
                    email.Bcc.Add(t);
                }
            }

            email.Subject = ets.MessageSubject;

            // TODO: Is this really nessessary? If importing from an HTML file, you'd think it wouldn't be needed
            email.Body       = string.Format("<html><body>\r\n{0}\r\n</body></head>", _messageContent);
            email.IsBodyHtml = true;

            Msg("Grabbing attachments, if any");

            attachments = LoadAttachments(ets.MessageAttachmentsToLoad);

            if (attachments != null)
            {
                foreach (Attachment att in attachments)
                {
                    email.Attachments.Add(att);                                     // Can't AddRange and can't construct your own AttachmentCollection, gotta add them 1 by 1
                }
            }

            //It's go time!!!
            Msg("Setting up the SMTP Client");
            SmtpClient smtp = new SmtpClient(ets.SmtpHost, ets.SmtpPort);

            smtp.EnableSsl = ets.SmtpUseSsl;

            // SMTP auth
            switch (ets.SmtpAuthMethod)
            {
            case SmtpAuthMethod.BasicAuthentication:
                Msg("Using basic SMTP auth");
                smtp.Credentials = new NetworkCredential(ets.SmtpAuthUsername, ets.SmtpAuthPassword);
                break;

            case SmtpAuthMethod.DefaultAuthentication:
                Msg("Using default / current user credentials for SMTP auth");
                smtp.UseDefaultCredentials = true;
                break;

            default:
                Msg("Not using SMTP authentication");
                break;
            }

            Msg("Sending email");
            smtp.Send(email);

            Msg("Email has been sent!");
        }