Que tal, espero que todos se encuentren bien, en esta ocación les tengo una .dll que hice cuando mis inicios en Sharp.
Es algo muy sencillo y básico que, como el título indica, permite enviar mensajes vía SMTP, los correos electrónicos soportados son los de Microsoft, Google y Yahoo!
Repito, la hice en mis inicios y estoy conciente de que se puede mejorar y simplificar el código así que me excuso ante los "masters" si por ahí ven alguna discrepancia.
Sin más, espero que a ti que comienzas en este rollo del código te ilumine un poco este ejemplo y te de rienda suelta para que te den más ganas de seguir aprendiendo que el código es una carajada.
Saludos a todos y...
Si tienes dudas al respecto pregunta
Es algo muy sencillo y básico que, como el título indica, permite enviar mensajes vía SMTP, los correos electrónicos soportados son los de Microsoft, Google y Yahoo!
Imagen:

Source:
namespace ut
{
public class EmailSender : System.IDisposable
{
#region Enum
private enum HostType
{
GOOGLE,
MICROSOFT,
YAHOO,
NONE
}
#endregion Enum
#region Variables
private HostType hostType = HostType.NONE;
private System.Boolean _Disposed = false;
private volatile System.Boolean bIsWorking = false;
public volatile System.Boolean bSuccess = false;
private System.Net.Mail.SmtpClient _SmtpClient = null;
private System.Net.Mail.MailMessage _MailMessage = null;
public System.String From = null;
public System.String Password = null;
public System.String To = null;
public System.String Subject = null;
public System.String Body = null;
public int TimeOut = 10000;
#endregion Variables
#region Constructor
public EmailSender(System.String _From, System.String _Password, System.String _To, System.String _Subject, System.String _Body, int _TimeOut)
{
this.From = _From;
this.Password = _Password;
this.To = _To;
this.Subject = _Subject;
this.Body = _Body;
this.TimeOut = _TimeOut;
if (From.Contains("gmail.com"))
{
hostType = HostType.GOOGLE;
}
if (From.Contains("live.com") || From.Contains("hotmail.com") || From.Contains("outlook.com"))
{
hostType = HostType.MICROSOFT;
}
if (From.Contains("yahoo.com"))
{
hostType = HostType.YAHOO;
}
}
#endregion Constructor
#region Methods
#region Send
public async void Send()
{
try
{
if (_SmtpClient == null) _SmtpClient = new System.Net.Mail.SmtpClient();
switch(hostType)
{
case HostType.GOOGLE:
_SmtpClient.Host = "smtp.gmail.com";
_SmtpClient.Port = 587;
break;
case HostType.MICROSOFT:
_SmtpClient.Host = "smtp-mail.outlook.com";
_SmtpClient.Port = 587;
break;
case HostType.YAHOO:
_SmtpClient.Host = "plus.smtp.mail.yahoo.com";
_SmtpClient.Port = 465;
break;
}
_SmtpClient.EnableSsl = true;
_SmtpClient.Timeout = TimeOut;
_SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
_SmtpClient.UseDefaultCredentials = false;
_SmtpClient.Credentials = new System.Net.NetworkCredential(From, Password);
_SmtpClient.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(this.client_SendCompleted);
await _SmtpClient.SendMailAsync(From, To, Subject, Body);
while (_SmtpClient.Timeout > 0)
{
bIsWorking = true;
}
}
catch (System.Exception)
{
bIsWorking = false;
}
return;
}
#endregion Send
#region IsSuccess
public System.Boolean SenderIsSuccess()
{
return bSuccess;
}
#endregion IsSuccess
#region IsWorking
public System.Boolean SenderIsWorking()
{
return bIsWorking;
}
#endregion IsWorking
#region Cancel
public void Cancel()
{
if (bIsWorking)
{
_SmtpClient.SendAsyncCancel();
}
}
#endregion Cancel
#endregion Methods
#region Events
private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
bSuccess = false;
bIsWorking = false;
throw new System.Exception(e.Error.Message);
}
if (e.Cancelled)
{
bSuccess = false;
}
bSuccess = true;
bIsWorking = false;
}
#endregion Events
#region Dispose
~EmailSender()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
if (!_Disposed)
{
From = null;
Password = null;
To = null;
Subject = null;
Body = null;
try { if (_SmtpClient != null) _SmtpClient.Dispose(); } catch (System.Exception) { };
try { if (_MailMessage != null) _MailMessage.Dispose(); } catch (System.Exception) { };
}
_Disposed = true;
}
}
#endregion Dispose
}
}
Ficha Técnica:
Código: Seleccionar todo
Ficheros: ut.dll (12.0 KB), EmailSender.exe (1.96 MB)
Compilador (IDE): MVS 2013
.NET Framework: 4.51 (actualizado)
Analisis Virus Total:
ut.dll
https://www.virustotal.com/es/file/157b8560a435b62d4193c0ed2df0bd4a43a4e161428108202a70932f62bdc66e/analysis/1419898445/
EmailSender.exe
https://www.virustotal.com/es/file/f9b4ea94d0d9dc6005faebd400b38a49e5e97f0b32884c372a57c113c4bb8f12/analysis/1419898509/
Comprimido: Si (1.25 MB en total)
Formato: .RAR
Contraseña: meta
Servidor: Mega
Licencia: Creative Metefunken Commons Loling (libre totalmente y sin restricciones de modificación lol)
Demo (mejorado):
Descarga:
Repito, la hice en mis inicios y estoy conciente de que se puede mejorar y simplificar el código así que me excuso ante los "masters" si por ahí ven alguna discrepancia.
Sin más, espero que a ti que comienzas en este rollo del código te ilumine un poco este ejemplo y te de rienda suelta para que te den más ganas de seguir aprendiendo que el código es una carajada.
Saludos a todos y...
Si tienes dudas al respecto pregunta
