Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.573
  • Views Posts: 542.166
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

C# - Inviare Email in formato HTML con l'aggiunta di Immagini (Codice)

C# - Inviare Email in formato HTML con l'aggiunta di Immagini (Codice)


  

Oggi voglio mettervi ha disposizione una funzione che vi permette di inviare una Email da C# e (VB.NET) in formato HTML aggiungendo le immagini nell'email. 

Esempio volete inviare una email con la fine i vostri dati e il vostro logo della società o quello che volete. Ora usando la funzione che vi metto a disposizione lo potete fare.

Passiamo hai parametri da configurare : 

CODICE VISUAL STUDIO 2010 - C#

        

        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";


Ora passiamo alla funzione : 

CODICE VISUAL STUDIO 2010 - C#

           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));

            }

        }

Ho deciso di inviare l'email utilizzando Google ma lo potete modificare come volete a vostro piacimento.

Ecco cosa potete ottenere in C# con un progetto di Esempio :

CODICE VISUAL STUDIO 2010 - C#

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Net.Mail;

using System.Net.Mime;


namespace InvioEmailHtmlImg

{

    public partial class Form1 : Form

    {


        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";


        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            email();

        }


        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));

            }

        }

    }

}


Ecco cosa potete ottenere in VB.NET con un progetto di Esempio :

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET

Imports System.Net

Imports System.IO

Imports System.Net.Mail

Imports System.Net.Mime


Public Class Form1


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        email()

    End Sub


    Private MITTENTE As String = "rossi.paolo@gmail.com"

    Private DESTINATARIO As String = "verdi.maria@gmail.com"

    Private PERCORSO_LOGO As String = "C:\img\logo.gif"

    Private USERNAME As String = "username@gmail.com"

    Private PASSWORD As String = "password@gmail.com"

    Private OGGETTO_EMAIL As String = "INVIO EMAIL IN HTML CON IMMAGINE"


    Private Sub email()

        ' Creare il messagio dell'email

        Dim email As 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 

        Dim attachmentPath As String = PERCORSO_LOGO


        ' generate the contentID string using the datetime

        Dim contentID As String = Path.GetFileName(attachmentPath).Replace(".", "")


        Dim inline As 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

        Dim credenziali As New System.Net.NetworkCredential(USERNAME.ToString(), PASSWORD.ToString())

        Dim smtpClient As 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 As SmtpException

            MessageBox.Show([String].Format("SmtpException : {0}", smtpException.Message))

        Catch ex As Exception

            MessageBox.Show(String.Format("Exception: {0}", ex.Message))

        End Try


    End Sub

End Class


 
Potete scaricare il file d'esempio :

Password :  "ImaginSystem" 


By ImaginSystems & Queen Gin   


Categoria: C#
lunedì, 25 nov 2013 Ore. 12.58

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003