Send in the body of a mail (vb.NET), the message text and the html signature

2

Hello, I have the following problem.

I need to send an email through vb.net, everything works correctly until I charge:

  

File .html (Signature with embedded images)
  Content of the textBoxMessage (It is where the user writes the message of the mail body)

Public Sub mailSinAdjunto()
        Dim messageSinAdjunto As MailMessage = New MailMessage()
        If (txtPara.Text = "" Or txtAsunto.Text = "" Or txtPara.Text = "") Then
            MsgBox(mensajeTextosVacios)
        Else

            'Configuracion del STMP
            smtp.Credentials = New Net.NetworkCredential(Login, Password)
            smtp.Port = PuertoSMTP.ToString
            smtp.Host = ServidorSMTP.ToString

            ' Configuracion del mensaje
            messageSinAdjunto.[To].Add(txtPara.Text)
            messageSinAdjunto.From = New MailAddress(DireccionEmailEnvio, NombreMostrar, System.Text.Encoding.UTF8)
            messageSinAdjunto.Subject = txtAsunto.Text.ToString
            messageSinAdjunto.SubjectEncoding = System.Text.Encoding.UTF8
            messageSinAdjunto.Priority = MailPriority.High

            If rdbSSL.Checked = True Then
                smtp.EnableSsl = True
            Else
                smtp.EnableSsl = False
            End If

            If rdbTextoPlano.Checked = True Then

                messageSinAdjunto.IsBodyHtml = False
                messageSinAdjunto.Body = txtMensaje.Text

            ElseIf rdbTextoHtml.Checked = True Then

                Dim allMensajeMail As String
                messageSinAdjunto.IsBodyHtml = True
                allMensajeMail = txtMensaje.Text.ToString

                Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(textoHtml, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)

                Dim imgCorreoBody As LinkedResource = New LinkedResource(logoCorreo, MediaTypeNames.Image.Jpeg)
                imgCorreoBody.ContentId = "logoBody"
                htmlView.LinkedResources.Add(imgCorreoBody)

                Dim imgCorreoHeader As LinkedResource = New LinkedResource(logoCorreo, MediaTypeNames.Image.Jpeg)
                imgCorreoHeader.ContentId = "logoHeader"
                htmlView.LinkedResources.Add(imgCorreoHeader)
                Dim textoMensaje As AlternateView = AlternateView.CreateAlternateViewFromString(allMensajeMail, Nothing, System.Net.Mime.MediaTypeNames.Text.Plain)

                messageSinAdjunto.AlternateViews.Add(htmlView)
                messageSinAdjunto.AlternateViews.Add(textoMensaje)

            End If
            Try
                smtp.Send(messageSinAdjunto)
                listErrores.Text = (mensajeEnviado)
            Catch ex As Exception
                listErrores.Text = (mensajeNoEnviado & ex.ToString)
                addLogArchivo(ex.Message)
            End Try
        End If
    End Sub

When using:

messageSinAdjunto.AlternateViews.Add(htmlView)
messageSinAdjunto.AlternateViews.Add(textoMensaje)

If I leave both, only the signature.html appears in the mail. If I put the text message first, only the message appears If I concatenate it, the html appears without forming (the code appears)

What could I use to send both? Thank you !!

    
asked by Sam.Gold 31.05.2017 в 17:17
source

2 answers

1

You can concatenate them before to form the message, changing:

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(textoHtml, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)

By:

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(textoHtml & allMensajeMail, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)

Then you just have to add messageSinAdjunto.AlternateViews.Add(htmlView) instead of both, since it was concatenated previously, you can also remove Dim textoMensaje As AlternateView = AlternateView.CreateAlternateViewFromString(allMensajeMail, Nothing, System.Net.Mime.MediaTypeNames.Text.Plain) since it has not been used anymore.

Result:

Public Sub mailSinAdjunto()
    Dim messageSinAdjunto As MailMessage = New MailMessage()
    If (txtPara.Text = "" Or txtAsunto.Text = "" Or txtPara.Text = "") Then
        MsgBox(mensajeTextosVacios)
    Else

        'Configuracion del STMP
        smtp.Credentials = New Net.NetworkCredential(Login, Password)
        smtp.Port = PuertoSMTP.ToString
        smtp.Host = ServidorSMTP.ToString

        ' Configuracion del mensaje
        messageSinAdjunto.[To].Add(txtPara.Text)
        messageSinAdjunto.From = New MailAddress(DireccionEmailEnvio, NombreMostrar, System.Text.Encoding.UTF8)
        messageSinAdjunto.Subject = txtAsunto.Text.ToString
        messageSinAdjunto.SubjectEncoding = System.Text.Encoding.UTF8
        messageSinAdjunto.Priority = MailPriority.High

        If rdbSSL.Checked = True Then
            smtp.EnableSsl = True
        Else
            smtp.EnableSsl = False
        End If

        If rdbTextoPlano.Checked = True Then

            messageSinAdjunto.IsBodyHtml = False
            messageSinAdjunto.Body = txtMensaje.Text

        ElseIf rdbTextoHtml.Checked = True Then

            Dim allMensajeMail As String
            messageSinAdjunto.IsBodyHtml = True
            allMensajeMail = txtMensaje.Text.ToString

            Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(textoHtml & allMensajeMail, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)

            Dim imgCorreoBody As LinkedResource = New LinkedResource(logoCorreo, MediaTypeNames.Image.Jpeg)
            imgCorreoBody.ContentId = "logoBody"
            htmlView.LinkedResources.Add(imgCorreoBody)

            Dim imgCorreoHeader As LinkedResource = New LinkedResource(logoCorreo, MediaTypeNames.Image.Jpeg)
            imgCorreoHeader.ContentId = "logoHeader"
            htmlView.LinkedResources.Add(imgCorreoHeader)
            'Dim textoMensaje As AlternateView = AlternateView.CreateAlternateViewFromString(allMensajeMail, Nothing, System.Net.Mime.MediaTypeNames.Text.Plain)

            messageSinAdjunto.AlternateViews.Add(htmlView)
            'messageSinAdjunto.AlternateViews.Add(textoMensaje)

        End If
        Try
            smtp.Send(messageSinAdjunto)
            listErrores.Text = (mensajeEnviado)
        Catch ex As Exception
            listErrores.Text = (mensajeNoEnviado & ex.ToString)
            addLogArchivo(ex.Message)
        End Try
    End If
End Sub
    
answered by 14.07.2017 в 19:07
0

The content of the two variables must have the same format, you can read the HTML template:

StreamReader lector = new StreamReader(ruta);
    contenido = lector.ReadToEnd();
    lector.Close(); 

//Tratas el contenido, añades textos, etc...

txtBody += contenido + "otro contenido";

// Puedes facilitar si tienes un HTML completo externo preparado
// y sustituyes una marca concreta del mismo por el contenido que tu quieras
txtBody = txtBody.Replace("##COMENTARIO##", tuComentario.ToString());

MailMessage mm = new MailMessage(txtFrom.ToString(), txtTo.ToString(), txtAsunto.ToString(), txtBody.ToString());
    mm.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
    /// Aquí la configuración de tu SMTP
    smtpClient.Credentials = ...
    smtp.Send(mm);

Remember that the result of the body of the email must be correct HTML with its corresponding tags.

    
answered by 28.06.2017 в 12:31