C# Evolution

Send Mail


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Threading;

// code by Agni Campos - 2014/2015
namespace mailer
{
    public class Program
    {

        static void Main(string[] args)
        {

            var fromAddress = new MailAddress("xxxxx@yyyyy.com", "Your Name"); 
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader(Environment.CurrentDirectory + "\\" + "emailtxt"); //full txt file name
            while ((line = file.ReadLine()) != null)
            {
                Console.WriteLine(line);
                counter++;
                mailer.Security x = new mailer.Security();
                string fromPassword = x.Password;
                var toAddress = new MailAddress(line, line);

                string subject = "Test ";
                string body = "<h1>";

                string titulo = "TEST";
                string subTitulo = "";
                string abrirsite = ""; 
                string texto = "test email";

                Console.Write("... begin ... ");

                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com", //example smtp 
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                    Timeout = 20000
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Priority = MailPriority.High,
                    Subject = subject,
                    Body = body + titulo + "</h1>" + subTitulo + "<br/>" + texto + "<br/><br/>" + abrirsite
                })
                {
                    message.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    message.IsBodyHtml = true;
                    message.Bcc.Add(line);
                    AlternateView plainView = AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(body, @"<(.|\n)*?>", string.Empty), null, "text/plain");
                    System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    smtp.Send(message);
                    Console.Write("... end ... ");
                }
            }

            file.Close();

            Console.Write("Total :" + counter.ToString());
            // Suspend the screen.
            Console.ReadLine();

        }
    }

}

//========================== Security.cs ==========================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mailer
{
    public class Security
    {
        private string strPassword = "YourPassword"; //
        public string Password { get { return strPassword;}
        }
    }
}


Comments