Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asp.net email issues !?

Posted on 2006-06-15
4
Medium Priority
?
355 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 Ageil earned 2000 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 Ageil
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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 an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

886 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