Solved

asp.net email issues !?

Posted on 2006-06-15
4
346 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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