mar 21, 2010

Enviado por en .NET, C/C++, Programacion

Enviar emails con Visual C++ .NET

Despues de tiempo sin escribir (ya se me acabaron las excusas asi que no dire otra), pues retomo los menesteres del blog. Realmente el post es bastante sencillo, es una pequeña clase para enviar emails a través de .NET especificamente de visual c++ la cual me toco hacer para un proyecto de la empresa donde trabajo, puede ser portado sin mayor dificultad a c#, vb.NET o cualquier lenguaje que este soportado por el framework.

easysendmail.h

// ===============================================================================
// Leonardo B.
// Filename: easysendmail.h
// Description: A simple class to send mails
// ================================================================================

#pragma once

using namespace System;
using namespace System::Net;
using namespace System::Net::Mail;

ref class Easysendmail{

private:
	System::Net::Mail::MailMessage^ message;
	System::Net::Mail::SmtpClient^ smtpclient;
	System::Net::NetworkCredential^ credentials;

public:
	Easysendmail(String^, String^, String^);
	void add_recipient(String^);
	void add_subject(String^);
	void add_body(String^);
	void add_server(String^, size_t);
	void add_attachments(String^);
	void add_attachments(array<String^>^);
	void send_mail();

};

easysendmail.cpp

// ===============================================================================
// Filename: easysendmail.cpp
// Description: Implementation of Easysendmail class
// ===============================================================================

#include "stdafx.h"
#include "easysendmail.h"

Easysendmail::Easysendmail(String^ nombre, String^ from, String^ autentification){

	message = gcnew MailMessage();
	smtpclient = gcnew SmtpClient();
	credentials = gcnew NetworkCredential(from, autentification);
	message->From = gcnew MailAddress(from, nombre, System::Text::Encoding::UTF8);
}

void Easysendmail::add_recipient(String^ to){

	message->To->Add(to);
}

void Easysendmail::add_subject(String^ subject){

	message->Subject = subject;
}

void Easysendmail::add_body(System::String ^body){

	message->Body = body;
}

void Easysendmail::add_server(String^ host, size_t port){

	smtpclient->Port = port;
	smtpclient->Host = host;
}

void Easysendmail::add_attachments(String^ attach){

	message->Attachments->Add(gcnew Attachment(attach));
}

void Easysendmail::add_attachments(array<String^>^ attach){

	for (int i = 0; i < attach->Length; ++i)
		message->Attachments->Add(gcnew Attachment(attach[i]));
}

void Easysendmail::send_mail(){

	smtpclient->Credentials = credentials;
	smtpclient->EnableSsl = true;
	smtpclient->Send(message);
}

    Posts Relacionados

    1. Mmm, waow el .cpp se esta convirtiendo en .net. :O esta evolucionando al lado oscuro. Cada ves eres mas apellido Puertas que antes. Imperialista mismo

      PD: El post esta muy bueno, gracias :D

      Usando Firefox 3.6 Firefox 3.6 en Windows 7 Windows 7
      • jajaj debo confesar que tengo cierta debilidad por las tecnologías de microsoft.

        Usando Firefox 3.6 Firefox 3.6 en Windows Vista Windows Vista
    2. Buen post :D me agrado

      Usando Firefox 3.6 Firefox 3.6 en Windows 7 Windows 7

    Menciones/Notificaciones

    1. how to become a stripper - if you go randomly into a crowded... street and start shouting about how great your it services are, people will almost ...

    Dejar una respuesta

    Debes ser Alojarse para enviar un comentario.