Solved

asp.net email issues !?

Posted on 2006-06-15
4
340 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
kewl...thats handy to no =0)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

11 Experts available now in Live!

Get 1:1 Help Now