Vamos crear el servicio de correo del modo SOAP : CorreoServices
En su interfaz ICorreos colocamos el siguiente codigo:
namespace CorreoServices
{
[ServiceContract]
public interface ICorreos
{
[OperationContract]
int SendEmail(string gmailUserAddress, string gmailUserPassword, string[] emailTo,string[] ccTo, string subject, string body, bool isBodyHtml);
}
}
Ahora implementemos esa interfaz en la clase Correos e importamos:
using System.Net.Mail;
using System.Net;
namespace CorreoServices
{
public class Correos : ICorreos
{
private static string SMTP_SERVER = "smtp.gmail.com";
private static int SMTP_PORT = 587;
public int SendEmail(string gmailUserAddress, string gmailUserPassword, string[] emailTo, string[] ccTo, string subject, string body, bool isBodyHtml)
{
int result = -100;
if (gmailUserAddress == null || gmailUserAddress.Trim().Length == 0)
{
return 10;
}
if (gmailUserPassword == null || gmailUserPassword.Trim().Length == 0)
{
return 20;
}
if (emailTo == null || emailTo.Length == 0)
{
return 30;
}
SmtpClient smtpClient = new SmtpClient(SMTP_SERVER, SMTP_PORT);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(gmailUserAddress, gmailUserPassword);
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress(gmailUserAddress);
message.Subject = subject == null ? "" : subject;
message.Body = body == null ? "" : body;
message.IsBodyHtml = isBodyHtml;
foreach (string email in emailTo)
{
message.To.Add(email);
}
if (ccTo != null && ccTo.Length > 0)
{
foreach (string emailCc in ccTo)
{
message.CC.Add(emailCc);
}
}
try
{
smtpClient.Send(message);
result = 0;
}
catch
{
result = 60;
}
}
return result;
}
}
}
Ahora nos apoyaremos en una consola de aplicación para la prueba: CorreoPrueba
namespace PruebaCorreo
{
class Program
{
static void Main(string[] args)
{
string emailFrom = "devsoft.distribuidos@gmail.com"; ---->Este es el correo que cree en SOMME
string password = "123456"; -------->Esta es la contraseña respectiva
string emailTo = "luegmy@gmail.com";------------>El destinatario
int result = -1;
try
{
using (CorreoServicioReferencia.CorreosClient cliente = new CorreoServicioReferencia.CorreosClient())
{
result = cliente.SendEmail(emailFrom, password, new string[] { emailTo }, null,"It works!!!", "Hola amigo prueba", false);
Console.WriteLine("Result: " + result);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
Luego debemos referenciar el CorreoServices.
Lo nombraremos CorreoServicioReferencia
Ejecutamos PruebaCorreo y se observa que llego el mensaje de SOMEE a mi correo.
No hay comentarios:
Publicar un comentario