Exemplo n.º 1
0
        public static ResultadoTransaccion MailEnBorrador(string toValue, string subjectValue, string bodyValue)
        {
            ResultadoTransaccion res = new ResultadoTransaccion();

            try
            {
                oApp          = new Application();
                oNameSpace    = oApp.GetNamespace("MAPI");
                oOutboxFolder = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderDrafts);
                _MailItem oMailItem = (_MailItem)oApp.CreateItem(OlItemType.olMailItem);
                oMailItem.To      = toValue;
                oMailItem.Subject = subjectValue;
                oMailItem.Body    = bodyValue;
                //oMailItem.SaveSentMessageFolder = oOutboxFolder;

                oMailItem.BodyFormat = OlBodyFormat.olFormatRichText;

                oMailItem.Save();

                res.Estado = Enums.EstadoTransaccion.Aceptada;
            }
            catch (Exception ex)
            {
                res.Descripcion = ex.Message;
                res.Estado      = Enums.EstadoTransaccion.Rechazada;

                Log.EscribirLog(ex.Message);
            }

            return(res);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var parser   = new CommandLineParser.CommandLineParser();
            var send     = new SwitchArgument('s', "send", "Whether to automatically send the messages or not", false);
            var template = new ValueArgument <string>('t', "template", "The path to the html template (title = subject)");
            var csv      = new ValueArgument <string>('c', "csv", "The path to the CSV file");

            parser.Arguments.Add(template);
            parser.Arguments.Add(csv);
            parser.Arguments.Add(send);

            try
            {
                parser.ParseCommandLine(args);
                parser.ShowParsedArguments();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine(exc.Message);
                return;
            }


            var reader  = new CsvReader(new StreamReader(new FileStream(csv.Value, FileMode.Open)));
            var content = File.ReadAllText(template.Value);

            var title = XElement.Parse(content).Descendants("title").First().Value;

            while (reader.Read())
            {
                var body = content;
                foreach (var f in reader.FieldHeaders)
                {
                    body = body.Replace("{" + f.ToLower() + "}", reader.GetField <string>(f));
                }

                var app = new Application();
                var ns  = app.GetNamespace("MAPI");
                ns.Logon(null, null, false, false);
                var       outbox  = ns.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
                _MailItem message = app.CreateItem(OlItemType.olMailItem);
                message.To                    = reader.GetField <string>(reader.FieldHeaders.Single(fh => string.Equals(fh, "email", StringComparison.InvariantCultureIgnoreCase)));
                message.Subject               = title;
                message.BodyFormat            = OlBodyFormat.olFormatHTML;
                message.HTMLBody              = body;
                message.SaveSentMessageFolder = outbox;
                if (send.Value)
                {
                    message.Send();
                }
                else
                {
                    message.Save();
                }
            }
        }