Sending email in ASP.NET VB

Discussion in 'Site Programming, Development and Design' started by tashworth, Mar 23, 2010.

  1. Read through the forum from front to back trying to get your VB example to work, which I've been trying to do for 2 days. http://support.Winhost.com/KB/a650/h...in-aspnet.aspx

    One of the threads dated 10-12-2009, 03:05 PM from SEAN said the C# example was right, but the VB example was wrong. Ray said he would fix the example.

    So I decided to copy and paste the C# example, and change the addresses & credentials to mine. I don't code in C#. Worked the first time.

    Please send me a corrected VB version of your example.

    Also, is there anywhere with a button_click example for Winhost, as that is how almost every form is sent. Might be a better example to post.
     
    Last edited by a moderator: Oct 14, 2015
  2. Ray

    Ray

    Last edited by a moderator: Oct 14, 2015
  3. Not getting any error

    Not getting any error message. When I copied and pasted the VB example no mail came through, when I copied and pasted the C# example I got the email. I also just tested the following code and got the email using VB...which I'm not sure why it is working since it isn't authenticated.

    Web Page - VB code will be after


    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="mailtext.aspx.vb" Inherits="mailtext" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    <p>
    Your email address:&nbsp; <asp:TextBox ID="txtFrom" runat="server" Width="200px"></asp:TextBox>
    </p>
    <p>
    Subject:&nbsp;
    <asp:TextBox ID="txtSubject" runat="server" Width="350px"></asp:TextBox>
    </p>
    <p>
    Message</p>
    <p>
    <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Height="134px"
    Width="400px"></asp:TextBox>
    </p>
    <asp:Button ID="Button1" runat="server" Text="Send" />
    <br />
    <br />
    <asp:Label ID="lblResult" runat="server"></asp:Label>
    </form>
    </body>
    </html>

    VB Code - note I have changed my email account settings in this example


    Partial Class mailtext
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim msg As New Net.Mail.MailMessage( _
    txtFrom.Text, _
    txtTo.Text, _
    txtSubject.Text, _
    txtMessage.Text)

    Dim mySmtp As New Net.Mail.SmtpClient("mail.NOTaREALsmptServer.com")
    Try
    mySmtp.Send(msg)
    lblResult.Text = "Mail Sent"
    Catch ex As Exception
    lblResult.Text = ex.Message
    End Try
    End Sub
    End Class

    Thanks Ray, trying to get a form to email from a button_click event to work. I highly suggest an example for this, and want to get the example to work first to eliminate errors at that simple first step.
     
  4. Never mind, it is now working...though I don't know why. Can't trace an error that isn't there.
     
  5. Ray

    Ray

    If you're not passing SMTP authentication on your code then it will not send an email outside of our network. Only an email that we host on our email server. As an example, if you have an account with us 'myaccount.com', and you setup a website that sends an email to postmaster@myaccount.com then it should work since it is not going outside our network unlike postmaster@yahoo.com.

    However, if you want to send an email outside our network then you do need to pass SMTP authentication other wise our mail server will not deliver the email. Emails that do not enforce SMTP authentication are typically considered as an open relay mail server. We don't support this because open relay mail servers typically are used for spamming.

    Unfortunately its difficult to create a an example that covers an event driven procedure. The layer fo complexity is added and there are too many variations to really implement an example for. The example we setup is specifically to help to pass the smtp authentication and to help verify that the site is able to connect to our email server to relay email. Actually the the method it uses 'System.net' is a method used in the .Net web.config file and these values can also be set within the applications web.config file.
     

Share This Page