Hola trataré de ser súper claro en lo que explicaré. No es un tema complicado pero quizás le sirva a alguien y bueno es el motivo por el cuál lo escribo…
Instrucciones Principales:
- Usaré Visual Studio 2008 bajo Framework 2.0, sobre Windows 7.
Primeramente creamos el proyecto en el lenguaje que quieras en mi caso estoy usando C#… y creamos las carpetas para tener todo mas ordenado como lo muestro en la imagen siguiente:
- Carpeta1.- Clases
- Carpeta2.- Acciones
- Carpeta3.- GUI
(La Carpeta Resources y otros, no la tomen en cuenta por ahora)
Ahora comenzaremos a implementar las clases dentro de la carpeta “Clases”
Agregamos 3 clases con los siguientes nombres (Dentro de Carpeta Clases)
- Clase1.- Configuraciones.cs
- Clase2.- Destino.cs
- Clase3.- Usuario.cs
Debería quedar algo parecido a esto:
Implementación de Clases sobre carpeta “Clases”
Clase Usuario.cs
¿Para qué?
Esta clase contendrá el usuario y la contraseña de tu correo(‘Nombre’,’Password’ respectivamente)
|
Modificador |
Tipo de Dato |
Identificador |
|
private |
string |
nombre |
|
private |
string |
password |
Cada atributo con sus respectivos GET y SET
acá el código debería ser algo así:
public class Usuario
{
private string nombre;
private string password;public Usuario(string _nombre, string _password) {
this.nombre = _nombre;
this.password = _password;
}public string Nombre
{
get { return nombre; }
set { nombre = value; }
}public string Password
{
get { return password; }
set { password = value; }
}
}
Clase Destino.cs
¿Para qué?
Contendrá el origen del correo, el destinatario y el asunto (‘de’, ‘para’,’asunto’ respectivamente)
|
Modificador |
Tipo de Dato |
Identificador |
|
private |
string |
de |
|
private |
string |
para |
|
private |
string |
asunto |
Cada atributo con sus respectivos GET y SET
acá el código debería ser algo así:
using System;
using System.Collections.Generic;
using System.Text;namespace Lisend.Clases
{
public class Destino
{
private string de;
private string para;
private string asunto;public Destino(string _de, string _para, string _asunto) {
this.de = _de;
this.para = _para;
this.asunto = _asunto;
}public string De
{
get { return de; }
set { de = value; }
}public string Para
{
get { return para; }
set { para = value; }
}public string Asunto
{
get { return asunto; }
set { asunto = value; }
}
}
}
Clase Configuracion.cs
¿Para qué?
Contendrá el host y el puerto como configuración avanzada dependiendo el correo que quieras usar.
|
Modificador |
Tipo de Dato |
Identificador |
|
private |
string |
host |
|
private |
string |
puerto |
Cada atributo con sus respectivos GET y SET
acá el código debería ser algo así:
using System;
using System.Collections.Generic;
using System.Text;namespace Lisend.Clases
{
public class Configuracion
{
private string host;
private string puerto;public Configuracion(string _host, string _puerto) {
this.host = _host;
this.puerto = _puerto;
}public string Host
{
get { return host; }
set { host = value; }
}public string Puerto
{
get { return puerto; }
set { puerto = value; }
}
}
}
Si todo marcha bien y aún no te aburres sigamos con la carpeta número dos “Acciones”
Implementación de clases sobre carpeta “Acciones”
Clase Mensaje.cs
¿Para qué?
Esta clase contendrá las acciones que realizará nuestro programa en éste caso enviar un mensaje
|
Modificador |
Tipo de Dato |
Identificador |
|
private |
MailMessage |
correo |
|
private |
SmtpClient |
smtp |
y Ahora el método que enviará un Mensaje
acá el código:
public void nuevoMensaje(Usuario u, Destino d, Configuracion c, string mensaje, bool conexionSegura) {
correo.From = new MailAddress(u.Nombre, d.De);
correo.To.Add(d.Para);
correo.Subject = d.Asunto;
correo.BodyEncoding = System.Text.Encoding.UTF8;
correo.Body = mensaje;
correo.IsBodyHtml = false;smtp.Credentials = new NetworkCredential(u.Nombre, u.Password);
smtp.Port = int.Parse(c.Puerto);
smtp.Host = c.Host; smtp.EnableSsl = conexionSegura;smtp.Send(correo);
}
y Ahora el método que adjuntará un archivo
acá el código:
public void adjuntar(OpenFileDialog openFileDialog1)
{
openFileDialog1.Filter = "All Files (*.*)|*.*|Comprimidos (*.rar)|*.rar|imagenes (*.jpg)|*.jpg";
openFileDialog1.FilterIndex = 1;
openFileDialog1.ShowDialog();
Attachment data = new Attachment(openFileDialog1.FileName, MediaTypeNames.Application.Octet);
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(openFileDialog1.FileName);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(openFileDialog1.FileName);
disposition.ReadDate = System.IO.File.GetLastAccessTime(openFileDialog1.FileName);
correo.Attachments.Add(data);
smtp.Timeout = 50000000;
//label12.Text = openFileDialog1.FileName;
}
Creando Nuestra Interfaz
Acá es cosa de cada uno elegir el modelo que quiere crear para su programa en éste caso dejo una imagen del que yo creé…
Ahora vamos al código de la interfaz que creamos y acá les dejo el código de cada evento dependiendo del nombre de cada uno de sus botones y otros…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lisend.Clases;
using Lisend.Acciones;namespace Lisend
{
public partial class Principal : Form
{
Mensaje nuevo = new Mensaje();
public Principal()
{
InitializeComponent();
ToolTip ToolTip1 = new ToolTip();
ToolTip1.SetToolTip(this.txtAsunto, "Asunto del Mensaje");
ToolTip1.SetToolTip(this.txtDestino, "Correo del Destino");
ToolTip1.SetToolTip(this.txtHost, "Ingresa Host EJ:’smtp.gmail.com’");
ToolTip1.SetToolTip(this.txtOrigen, "Ingrese nombre del Origen");
ToolTip1.SetToolTip(this.txtPassword, "Ingresa Tu Password");
ToolTip1.SetToolTip(this.txtPuerto, "Ingresa el Puerto EJ:’587′");
ToolTip1.SetToolTip(this.txtUsuario, "Ingresa tu correo");
ToolTip1.SetToolTip(this.rtMensaje, "Redacta el Mensaje");
ToolTip1.SetToolTip(this.btAdjuntar, "Adjunta tus Archivos");
ToolTip1.SetToolTip(this.btEnviar, "Envia tu Mensaje");
ToolTip1.SetToolTip(this.btLimpiar, "Limpiar Los Formularios");
ToolTip1.SetToolTip(this.menuStrip1, "Gracias a PabloRuiz");
}private void btN_Click(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void btS_Click(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Italic == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Italic;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void btS_Click_1(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Underline;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void btInsertarImagen_Click(object sender, EventArgs e)
{
fontDialog1.Font = rtMensaje.Font;
fontDialog1.ShowDialog();
rtMensaje.Font = fontDialog1.Font;
}private void btEnviar_Click(object sender, EventArgs e)
{
try
{
Usuario u = new Usuario(txtUsuario.Text, txtPassword.Text);
Destino d = new Destino(txtOrigen.Text, txtDestino.Text, txtAsunto.Text);if (txtHost.Text == "" && txtPuerto.Text == "")
{
Configuracion c = new Configuracion("smtp.gmail.com", "587");
if (chConexionSegura.Checked)
{
nuevo.nuevoMensaje(u, d, c, rtMensaje.Text, true);
MessageBox.Show("Enviado Correctamente", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
else {
nuevo.nuevoMensaje(u, d, c, rtMensaje.Text,false);
MessageBox.Show("Enviado Correctamente", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
}
else
{
Configuracion c = new Configuracion(txtHost.Text, txtPuerto.Text);
if (chConexionSegura.Checked)
{
nuevo.nuevoMensaje(u, d, c, rtMensaje.Text, true);
}
else {
nuevo.nuevoMensaje(u, d, c, rtMensaje.Text,false);
}
}
}
catch (Exception ex) {
MessageBox.Show("Error" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}private void btAdjuntar_Click(object sender, EventArgs e)
{
try
{
nuevo.adjuntar(openFileDialog1);
}
catch (System.IO.FileNotFoundException ex)
{}catch(Exception ex){
MessageBox.Show("Error"+ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}private void acercaDeLissendToolStripMenuItem_Click(object sender, EventArgs e)
{
new AcercaDeLissend().Show();
}private void salirToolStripMenuItem1_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}private void btLimpiar_Click(object sender, EventArgs e)
{
txtAsunto.Text = "";
txtDestino.Text = "";
txtHost.Text = "";
txtOrigen.Text = "";
txtPassword.Text = "";
txtPuerto.Text = "";
txtUsuario.Text = "";
rtMensaje.Text = "";}
private void btN_Click_1(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void button1_Click(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Underline;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void btC_Click(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Italic;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}private void btT_Click(object sender, EventArgs e)
{
if (rtMensaje.SelectionFont != null)
{
System.Drawing.Font currentFont = rtMensaje.SelectionFont;
System.Drawing.FontStyle newFontStyle;if (rtMensaje.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Strikeout;
}rtMensaje.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
}
}
Ahora la interfaz de la ventana AcercaDe “Típica”
Ahora vamos al código de la interfaz que creamos y acá les dejo el código de cada evento dependiendo del nombre de cada uno de sus botones y otros…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;namespace Lisend
{
partial class AcercaDeLissend : Form
{
public AcercaDeLissend()
{
InitializeComponent();
this.Text = String.Format("About {0} {0}", AssemblyTitle);
this.labelProductName.Text = "Lissend";
this.labelVersion.Text = String.Format("Version {0} {0}", AssemblyVersion);
this.labelCopyright.Text = AssemblyCopyright;
this.labelCompanyName.Text = "Personal";
}#region Assembly Attribute Accessors
public string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title != "")
{
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}public string AssemblyDescription
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyDescriptionAttribute)attributes[0]).Description;
}
}public string AssemblyProduct
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}public string AssemblyCopyright
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregionprivate void okButton_Click(object sender, EventArgs e)
{
this.Visible = false;
}}
}
Fin! jojo…
Acá le Adjunto el código Completo para que lo descarguen y lo prueben:
- Espero que les guste el programillas no es complicado y muy útil