Results 1 to 8 of 8

Thread: Problem send mail with html

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    32

    Question Problem send mail with html

    Hi

    I have a problem, I want to send mail through a form on a website, but do not send me html structure

    This is the code I've done

    Imports System.Data
    Imports System.Data.Odbc
    Imports Microsoft.VisualBasic
    Imports System.Web.Mail

    Partial Class Contacto_Default
    Inherits System.Web.UI.Page

    Public Function SQLInsert(ByVal SQLcmd As String) As Integer
    Dim sql As String
    sql = "select last_insert_id() as ultimo"

    Dim ConnStr As String = ConfigurationManager.AppSettings("ODBCConnection")
    Dim con As OdbcConnection = New OdbcConnection(ConnStr)
    Dim cmd As OdbcCommand = New OdbcCommand(SQLcmd, con)

    Dim cmd2 As OdbcCommand = New OdbcCommand(sql, con)
    Dim rs As OdbcDataReader

    ' ejecuta el insert
    con.Open()
    cmd.ExecuteNonQuery()

    rs = cmd2.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.SingleResult)
    rs.Read()
    SQLInsert = rs("ultimo").ToString()

    rs.Close()
    con.Close()
    rs = Nothing
    con = Nothing
    End Function

    Private Sub SendMail(ByVal Message As String, ByVal Subject As String, ByVal Email As String)
    Dim smtp As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("mail.dominio.com", 25)
    smtp.Send("correo@dominio.com", Email, Subject, Message)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Subject As String = "Correo enviado"
    Dim Sigue As Boolean = True
    Dim myMail As New MailMessage()

    If TextBox1.Text = "" Then
    Label1.Text = "Indique su Nombre"
    Sigue = False
    End If

    If Not Sigue Then
    Label1.Visible = True
    Else
    Dim sql As String
    Dim numsol As Integer

    sql = "insert into contacto (nombre, direccion, cuando) values ("
    sql &= "'" & QuitaC(TextBox1.Text) & "', "
    sql &= "'" & QuitaC(TextBox2.Text) & "', now())"

    numsol = SQLInsert(sql)
    Dim mensaje As String
    mensaje &= "<html>"
    mensaje &= "<body topmargin=""0"" leftmargin=""0"" rightmargin=""0"" bottommargin=""0"">"
    mensaje &= "<table style=""width: 800px"">"
    mensaje &= "<tr>"
    mensaje &= "<td class=""auto-style3"" colspan=""2""><strong>Correo generado</strong></td>"
    mensaje &= "</tr>"
    mensaje &= "<tr>"
    mensaje &= "<td class=""auto-style1"" style=""width: 119px"" valign=""top""><strong>"
    mensaje &= "Nombre:</strong></td>"
    mensaje &= "<td class=""auto-style2"" valign=""top"">"
    mensaje &= "<div style=""width: 669px"">"
    mensaje &= " sasas"
    mensaje &= "</div>"
    mensaje &= "</td>"
    mensaje &= "</tr>"
    mensaje &= "<tr>"
    mensaje &= "<td class=""auto-style1"" style=""width: 119px"" valign=""top""><strong>"
    mensaje &= "Dirección:</strong></td>"
    mensaje &= "<td class=""auto-style2"" valign=""top"">"
    mensaje &= " ddd"
    mensaje &= "</td>"
    mensaje &= "</tr>"
    mensaje &= "</table>"
    mensaje &= "</body>"
    mensaje &= "</html>"

    myMail.BodyFormat = MailFormat.HTML
    myMail.Body = mensaje

    SendMail(mensaje, Subject, "mail@destino.com")
    Response.Redirect("enviado.aspx")
    End If
    End Sub
    End Class
    and this is how I get to my email

    <html><body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0"><table style="width: 800px"><tr><td class="auto-style3" colspan="2"><strong>Correo generado</strong></td></tr><tr><td class="auto-style1" style="width: 119px" valign="top"><strong>Nombre:</strong></td><td class="auto-style2" valign="top"><div style="width: 669px"> sasas</div></td></tr><tr><td class="auto-style1" style="width: 119px" valign="top"><strong>Dirección:</strong></td><td class="auto-style2" valign="top"> ddd</td></tr></table></body></html>

    What am I doing wrong?

  2. #2
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Problem send mail with html

    What email client are you using? Not all support HTML and some you have to enable HTML formatting before it will render correctly.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    32

    Re: Problem send mail with html

    I have tried with yahoo, hotmail, gmail aun my outlook and I get all the same

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Problem send mail with html

    You need to set the IsBodyHTML property.... http://msdn.microsoft.com/en-us/libr...sbodyhtml.aspx

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Problem send mail with html

    I actually want to know where the .BodyFormat property is coming from. It isn't mentioned on MSDN so I would think the could wouldn't work...

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Problem send mail with html

    Turns out there are TWO MailMessage classes....

    System.Object
    System.Web.Mail.MailMessage
    Namespace: System.Web.Mail
    Assembly: System.Web (in System.Web.dll)
    http://msdn.microsoft.com/en-us/library/3ykd86hh.aspx

    System.Object
    System.Net.Mail.MailMessage
    Namespace: System.Net.Mail
    Assembly: System (in System.dll)
    http://msdn.microsoft.com/en-us/library/8t22a8ww.aspx


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Problem send mail with html

    Quote Originally Posted by techgnome View Post
    Turns out there are TWO MailMessage classes....

    System.Object
    System.Web.Mail.MailMessage
    Namespace: System.Web.Mail
    Assembly: System.Web (in System.Web.dll)
    http://msdn.microsoft.com/en-us/library/3ykd86hh.aspx

    System.Object
    System.Net.Mail.MailMessage
    Namespace: System.Net.Mail
    Assembly: System (in System.dll)
    http://msdn.microsoft.com/en-us/library/8t22a8ww.aspx


    -tg
    I was not aware of this, thanks lol. I notice is says the Web.Mail.MailMessage is obsolete. Switch to the Net.Mail one link recommended by msdn.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Problem send mail with html

    Originally the mail class was in the System.Web namespace... it was moved when the FW updated from 1.1 to 2.0 ... System.Web.Mail is still there (it's easier to add things than it is to take it out) ... but it's basically no longer maintained... so it is essentially 10 years old... System.Net.Mail on the other hand is maintained and is why it is recommended.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width