Torna al Thread

private string MITTENTE = "rossi.paolo@gmail.com"; private string DESTINATARIO = "verdi.maria@gmail.com"; private string PERCORSO_LOGO = @"C:\img\logo.gif"; private string USERNAME = "username@gmail.com"; private string PASSWORD = "password@gmail.com"; private string OGGETTO_EMAIL = "INVIO EMAIL IN HTML CON IMMAGINE"; private void email() { // Creare il messagio dell'email MailMessage email = new MailMessage(MITTENTE, DESTINATARIO); // Informazioni email.Subject = OGGETTO_EMAIL ; email.IsBodyHtml = true; email.Body = "<div style=\"font-family:Arial\">Messaggio prima di includere l'immagine:<br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /> Descrizione dell'immagine sopra inserita.</div>"; // Creare il INLINE string attachmentPath = PERCORSO_LOGO; // generate the contentID string using the datetime string contentID = Path.GetFileName(attachmentPath).Replace(".", ""); Attachment inline = new Attachment(attachmentPath); inline.ContentDisposition.Inline = true; inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; inline.ContentId = contentID; inline.ContentType.MediaType = "image/gif"; // "image/png" ; "image/jpg" inline.ContentType.Name = Path.GetFileName(attachmentPath); email.Attachments.Add(inline); // replace the tag with the correct content ID email.Body = email.Body.Replace("@@IMAGE@@", "cid:" + contentID); //Invio email con GMAIL SMTP System.Net.NetworkCredential credenziali = new System.Net.NetworkCredential(USERNAME.ToString(), PASSWORD.ToString()); SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); //Se è necessario ed è supportata la cifratura SSL smtpClient.EnableSsl = true; smtpClient.Credentials = credenziali; try { smtpClient.Send(email); email.Dispose(); } catch (SmtpException smtpException) { MessageBox.Show(String.Format("SmtpException : {0}", smtpException.Message)); } catch (Exception ex) { MessageBox.Show(string.Format("Exception: {0}", ex.Message)); } }
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5