Envio de Correo “System.Net.Mail.MailMessage”

en este código fuente muestro como enviar un Correo por el Servidor SMTP del cliente Windows NT (2000/XP/2003)

From: Es Correo de la persona que Envia el Mail (Origen)

To:

Destino del Mail

Cc:

Copia del Mail

Subject: Es el "Asunto" del Mail

Body:Es el cuerpo del Mail

Attachments: Lista de Paths con las direcciones de los archivos que se necesitan vincular al Mail

los valores del archivo de configuración SMTPServer  para el ejemplo

mail.Empresa.com 

Cambie este valor por el del servidor SMTPServer.

public void EnviarCorreo(string From, string To, string Cc, string Subject, string Body, params object[] Attachments)

{

System.Net.Mail.MailMessage mailSend = new System.Net.Mail.MailMessage();

try

{

mailSend.From = new System.Net.Mail.MailAddress(From); 

string[] strTo = To.Split(‘,’);

//Recorre la cadena buscando los destinatarios del Mail

for (int i = 0; i < strTo.Length; i++)

    if (strTo[i].Trim() != "")

        mailSend.To.Add(strTo[i]);

    if (Cc.Trim() != "")

        mailSend.CC.Add(Cc);

    mailSend.Body = Body;

    mailSend.Subject = "Avianca"+ " " + Subject;

//archivos anexos del Mail

for (int i = 0; i < Attachments.Length; i++)

   mailSend.Attachments.Add(new System.Net.Mail.Attachment(Attachments[i].ToString()));

 

System.Net.Mail.SmtpClient objSmtpClient = new System.Net.Mail.SmtpClient();

objSmtpClient.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(objSmtpClient_SendCompleted);

objSmtpClient.Host = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();

objSmtpClient.SendAsync(mailSend, new object());

}

catch (Exception ex)

{

  throw new Exception(ex.Message);

}

}

    

void objSmtpClient_SendCompleted(object sender, stem.ComponentModel.AsyncCompletedEventArgs e)

{

// Get the unique identifier for this asynchronous operation.

String token = e.UserState.ToString();

if (e.Cancelled)           

   Console.WriteLine("[{0}] Send canceled.", token);           

if (e.Error != null)           

   Console.WriteLine("[{0}] {1}", token, e.Error.ToString());           

else           

   Console.WriteLine("Message sent.");           

} 

About omaracostacasas

ING SOFTWARE
This entry was posted in Microsoft .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s