Skip to content

Instantly share code, notes, and snippets.

@kiquenet
Created December 2, 2014 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiquenet/6a828ce780dc485e9a19 to your computer and use it in GitHub Desktop.
Save kiquenet/6a828ce780dc485e9a19 to your computer and use it in GitHub Desktop.
Send Email Test AlternateView
private void EnviarTest(string body, MBC_ADJUNTOSList listaAdjuntos)
{
// Montamos la estructura básica del mensaje...
MailMessage mail = new MailMessage();
mail.From = new MailAddress("xxxx@kiquenet.com");
mail.To.Add("xxxx@gmail.com");
mail.Subject = "Test";
// Creamos la vista para clientes que // sólo pueden acceder a texto plano...
string text = "Hola, ayer estuve disfrutando de " + "un paisaje estupendo.";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(text
, null /*Encoding.UTF8*/
, MediaTypeNames.Text.Plain);
// HTML VIEW
// =====================================================================
// Fill the linked resource
// Ahora creamos la vista para clientes que // pueden mostrar contenido HTML...
string html = body;
//"<h2>Hola, mira dónde estuve ayer:</h2>" + "<img src='cid:imagen1' id='foto1' />";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html
, null /*Encoding.UTF8*/
, MediaTypeNames.Text.Html);
// Creamos el recurso a incrustar. Observad // que el ID que le asignamos (arbitrario) está // referenciado desde el código HTML como origen // de la imagen (resaltado en amarillo)...
int indImg = 1; // ++++++++++++++++++++++++++++++++++++++++
foreach (var adjunto in listaAdjuntos)
{
string im1 = @"F:\cartel_grande.jpg";
Stream stream1 = new FileStream(im1, FileMode.Open, FileAccess.Read, FileShare.Read);
var strmMemory = new MemoryStream(); ;
// StreamReader sr = new StreamReader(strmMemory);
var stream1a = ConvertByteArrayToStream(adjunto.ADJUNTO);
Image img1 = ByteArrayToImage(adjunto.ADJUNTO);
var ms1 = new MemoryStream();
img1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
ms1.Position = 0;
ms1.Seek(0, SeekOrigin.Begin);
bool esJPG = (Path.GetExtension(adjunto.NOMBRE).EndsWith("jpg"));
if (esJPG)
{
var img = new LinkedResource(
//im1
ms1
, MediaTypeNames.Image.Jpeg);
//img.ContentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
string generatedName = "imagen" + indImg;
// Generate the linked resource for the image being embedded
string generatedSrc = "cid:" + generatedName;
img.ContentId = generatedName;
indImg++;
htmlView.LinkedResources.Add(img);
}
}
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//mail.Body = body;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.UTF8;
// Y lo enviamos a través del servidor SMTP...
SmtpClient smtp = new SmtpClient("smtp.xxx.com");
smtp.Credentials = new NetworkCredential("xxxx@xxx.com", "xxxxxx");
smtp.Send(mail);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment