Email Code Doesn't send Email

Discussion in 'ASP.NET 2.0' started by dbireporter, Dec 20, 2014.

  1. I created an ASP.NET Web Page as shown below, but no email is sent when it loads. Any idea what's wrong?


    <%@
    PageLanguage="vb"AutoEventWireup="false"CodeBehind="SendEmail.aspx.vb"Inherits="Pawn_Shop_Site.SendEmail" %>

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

    %
    @ImportNamespace="System.Net.Mail" %


    <
    scriptrunat="server">


    PrivateSub Forgot(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim strFrom = "[email protected]"

    Dim strTo = "[email protected]"

    Dim MailMsg AsNew MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))


    MailMsg.BodyEncoding = Encoding.Default
    MailMsg.Subject =
    "Test Subject"

    MailMsg.Body =
    "This is a sample message"

    MailMsg.Priority = MailPriority.High
    MailMsg.IsBodyHtml =
    True


    'Smtpclient to send the mail message

    Dim SmtpMail AsNew SmtpClient

    SmtpMail.Host =
    "localhost"

    SmtpMail.Send(MailMsg)

    ForgotLabel.Visible =
    True

    EndSub


    </
    script>

    <
    htmlxmlns="http://www.w3.org/1999/xhtml">

    <
    headrunat="server">

    <title>Untitled Page</title>

    </
    head>

    <
    body>

    <formid="form1"runat="server">

    <div>

    <asp:LabelID="ForgotLabel"runat="server"Text="Mail Sent!"Visible="False"></asp:Label></div>

    </form>

    </
    body>

    </
    html>
     
  2. Further question: Does the From address have to be an email address at discountasp.net?
     
  3. martino

    martino DiscountASP.NET Staff

    If you're using localhost to send the email message. Yes, you can change the from field to an email address that is not associated with your site account.

    Did you get any bounce back error messages? Any error messages at all?
     
  4. Here is the VB code I am using in my page.aspx.vb: (I am not using script in my page.aspx as I need other functions performed in the page before actually sending the email)
    ---------------------------------------------
    Dim strFrom = "[email protected]"
    Dim strTo = "[email protected]"
    Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))

    MailMsg.BodyEncoding = Encoding.Default
    MailMsg.Subject = "Test Subject"
    MailMsg.Body = "This is a sample message"
    MailMsg.Priority = MailPriority.High
    MailMsg.IsBodyHtml = True

    'Smtpclient to send the mail message
    Dim SmtpMail As New SmtpClient
    SmtpMail.Host = "localhost"
    SmtpMail.Send(MailMsg)
    -------------------------------------------------------
    I get the following error: "Server Error in '/' Application. Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected]"

    I did try running your sample script sendmailvb.aspx and I did receive the generated email. Is your script in your sendmailvb.aspx.vb or in sendmailvb.aspx? If it is VB code, could you send the VB code to me?
     
  5. This problem is solved. I got the error because I ran the app on my PC. When I published it to the site, it works fine.
    Thanks for all the help!
     
    mjp and RayH like this.

Share This Page