Solved

asp.net email issues !?

Posted on 2006-06-15
4
344 Views
Last Modified: 2006-11-18
Hey ya all,

OK I used a asp.net e-mail script from: http://www.4guysfromrolla.com/webtech/080801-1.shtml

Now most the scripts if looked at on the web use similar code to this, only problem is I am using Microsoft Visual Web Developer 2005 Express Edition and it tells me the script is mostly out of date. So I had a go at updating it and for the most part I think I got it. However obviously their is a problem, firstly I don’t no how to set the SmtpMail.host to use a default one, in the old script you just used SmtpMail.SmtpServer = "" but in the new script it can't be blank. I also not sure what the mail format should be, I set it to IsBodyHtml = False as their isn’t a MailFormat.Text equivilant. Encase your wondering I have tried to get the old script to work but got lots of errors like: <identity impersonate="true"/> error which I managed to fix in the web.config file only to then get a allowDefinition='MachineOnly' beyond machine.config. error message for witch I couldn’t fix. So rather than waist time trying to fix it I updated the code and no longer get errors like that so the code is working just need to set it up correctly. My code:


Code:
=============================================================================
<%@Import Namespace="System.Web.Mail" %>
<%@ Page Language="VB" AutoEventWireup="true" Debug="true" %>

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

<script runat="server">

Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
        ' Create an instance of the MailMessage class
        Dim objMM As New System.Net.Mail.MailMessage
       
        ' Get the reply address from the web page form
        Dim ad As New System.Net.Mail.MailAddress(txtEmail.Text)
       
        ' Set the a varible to be used in defining the SmtpClient what ever the smeg that is !?
        Dim SmtpMail As New System.Net.Mail.SmtpClient
       
        ' Set the properties - send the email to me
        objMM.To.Add("lemming29@hotmail.com")
       
        ' Set the reply addess to the e-mail address of who ever filled out the form
        objMM.From = ad

        ' Send the email in text format .... well can't do that in the new code as don't no how so just set it as not html unless you want to have html!?
        objMM.IsBodyHtml = False

        ' Set the priority - options are High, Low, and Normal
        objMM.Priority = System.Net.Mail.MailPriority.Normal

        ' Set the subject
        objMM.Subject = "4GuysFromRolla.com - Feedback"

        ' Set the body ...BNo idea if the body part is ok as not sure what this vbCrLf ment to do quite
        objMM.Body = "At " + DateTime.Now + " feedback was sent from an ASP.NET " & _
               "Web page.  Below you will find the feedback message " & _
               "send by " & txtName.Text & "." & vbCrLf & vbCrLf & _
               "---------------------------------------" & vbCrLf & vbCrLf & _
               txtMessage.Text & vbCrLf
 
        ' Specify to use the default Smtp Server ...ok so how do you set this to a defualt one ?
        SmtpMail.Host = ""
 
        ' Now, to send the message.....if i ever get this far that is !
        SmtpMail.Send(objMM)

        panelSendEmail.Visible = false
        panelMailSent.Visible = true
End Sub

</script>

<html>
<body>
  <asp:panel id="panelSendEmail" runat="server">
    <form id="Form1" runat="server">
      <h2>We are interested in your feedback!  Please enter the following
      requested information below to send us your comments.</h2>

      <b>Your Name:</b>
      <asp:textbox id="txtName" runat="server" />
      <br>

      <b>Your Email Address:</b>
      <asp:textbox id="txtEmail" runat="server" />
      <p>

      <b>Your Message:</b><br>
      <asp:textbox id="txtMessage" TextMode="MultiLine"
                      Columns="40" Rows="10" runat="server" />
      <p>

      <asp:button runat="server" id="btnSendFeedback" Text="Send Feedback!"
                  OnClick="btnSendFeedback_Click" />
    </form>
  </asp:panel>


  <asp:panel id="panelMailSent" runat="server" Visible="False">
    An email has been sent to the email address you specified.  Thanks!
  </asp:panel>
</body>
</html>
=============================================================================

Any help is welcome
0
Comment
Question by:Rawdon Hume
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Sammy earned 500 total points
ID: 16928540
you can use localhost as your default mailserver
I have used this script since the release of asp.net 2.0 and it never failed. I just use isBodyHTML=true but you can change it to false since you have no use for HTML formats.


Dim ObjMail As New SmtpClient
        Dim ObjFromAddress As New MailAddress(txtContactEmail.Text, txtContactName.Text)
        Dim ObjToAddress As New MailAddress("me@webdivisions.net", "My Name")
        Dim ObjMsg As New MailMessage(ObjFromAddress, ObjToAddress)
        ObjMsg.Body = "<html><head></head><title>Thank you My Site</title>" & _
        "<body bgcolor='#552023'>" & Me.txtContactMessage.Text & "<body></html>"
        ObjMsg.IsBodyHtml = True 'change to false to get text only
        ObjMsg.Subject = Me.txtContactSubject.Text
        ObjMail.Host = "localhost" 'uses the default IIS mail server
        ObjMsg.Priority = MailPriority.High
        Try
            'send the mail in try block to check for errors
            ObjMail.Send(ObjMsg)
        Catch ex As SmtpException
            Response.Write("An error has occurred!, system admin has been notified!")
            ObjMail.Send("admin@site.ca", "admin@site.ca", "Error Notification " & DateTime.Now.ToString, ex.ToString)
        Finally
            ObjMail = Nothing
            ObjMsg = Nothing
        End Try

the script uses Try Cach and finally
Try is to send the messag, catch to catch any errors and send an email to site admin with date time and the error itself and the finally block destroys the objects and release memory

HTH
0
 
LVL 2

Author Comment

by:Rawdon Hume
ID: 16930006
Yaaa Thank you! I had to update the code as Microsoft Visual Web Developer 2005 Express Edition didn't like it but main thing is it worked, oddly though I had tried localhost before but didn’t seam to like it, as well here is a copy of the final working version for any one who ever has the problems I did:

Code:
=============================================================================
<%@Import Namespace="System.Net.Mail" %>
<%@ Page Language="VB" AutoEventWireup="true" Debug="true" %>

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

<script runat="server">

Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
        ' Create an instance of the MailMessage class
        Dim objMM As New System.Net.Mail.MailMessage
       
        ' Get the reply address from the web page form
        Dim ad As New System.Net.Mail.MailAddress(txtEmail.Text)
       
        ' Set the a varible to be used in defining the SmtpClient what ever the smeg that is !?
        Dim SmtpMail As New System.Net.Mail.SmtpClient()
        'This object stores the authentication values
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential()
        'Put your own, or your ISPs, mail server name onthis next line
        SmtpMail.Host = "localhost"
 
        SmtpMail.Credentials = basicAuthenticationInfo
       
        ' Set the properties - send the email to me
        objMM.To.Add("lemming29@hotmail.com")
       
        ' Set the reply addess to the e-mail address of who ever filled out the form
        objMM.From = ad

        ' Send the email in text format .... well can't do that in the new code as don't no how so just set it as not html unless you want to have html!?
        objMM.IsBodyHtml = False

        ' Set the priority - options are High, Low, and Normal
        objMM.Priority = System.Net.Mail.MailPriority.Normal

        ' Set the subject
        objMM.Subject = "4GuysFromRolla.com - Feedback"

        ' Set the body ...BNo idea if the body part is ok as not sure what this vbCrLf ment to do quite
        objMM.Body = "At " + DateTime.Now + " feedback was sent from an ASP.NET " & _
               "Web page.  Below you will find the feedback message " & _
               "send by " & txtName.Text & "." & vbCrLf & vbCrLf & _
               "---------------------------------------" & vbCrLf & vbCrLf & _
               txtMessage.Text & vbCrLf
 
        ' Specify to use the default Smtp Server ...ok so how do you set this to a defualt one ?
        SmtpMail.Host = "localhost"

        ' Now, to send the message.....if i ever get this far that is !
        SmtpMail.Send(objMM)

        panelSendEmail.Visible = false
        panelMailSent.Visible = true
End Sub

</script>

<html>
<body>
  <asp:panel id="panelSendEmail" runat="server">
    <form id="Form1" runat="server">
      <h2>We are interested in your feedback!  Please enter the following
      requested information below to send us your comments.</h2>

      <b>Your Name:</b>
      <asp:textbox id="txtName" runat="server" />
      <br>

      <b>Your Email Address:</b>
      <asp:textbox id="txtEmail" runat="server" />
      <p>

      <b>Your Message:</b><br>
      <asp:textbox id="txtMessage" TextMode="MultiLine"
                      Columns="40" Rows="10" runat="server" />
      <p>

      <asp:button runat="server" id="btnSendFeedback" Text="Send Feedback!"
                  OnClick="btnSendFeedback_Click" />
    </form>
  </asp:panel>


  <asp:panel id="panelMailSent" runat="server" Visible="False">
    An email has been sent to the email address you specified.  Thanks!
  </asp:panel>
</body>
</html>
=============================================================================
0
 
LVL 27

Expert Comment

by:Sammy
ID: 16930192
You welcome,
for future reference, if localhost doesnt work, use 127.0.0.1
one of those will work unless IIS is messed up or some configs need to be changed to allow sending mail


Regards
Sammy
0
 
LVL 2

Author Comment

by:Rawdon Hume
ID: 16930685
kewl...thats handy to no =0)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now