Results 1 to 23 of 23

Thread: [2005] how to send email notification?

  1. #1

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    [2005] how to send email notification?



    Hi,

    I got a question ..
    in my application .. I need to send an email notification when user do something

    for example, I need to to send an email to user when he insert some data
    and the insertion was successful

    like here in the forum, we got an email when we first register

    how am going to do that?
    please help ..


    many thanks in advance ..

  2. #2
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: [2005] how to send email notification?

    don't worry about the CreateRequest call or method

    vb Code:
    1. Private Sub SendAdminEmail()
    2.         Dim config As System.Configuration.Configuration = _
    3.             WebConfigurationManager.OpenWebConfiguration( _
    4.                 HttpContext.Current.Request.ApplicationPath)
    5.         Dim settings As System.Net.Configuration.MailSettingsSectionGroup = _
    6.             CType(config.GetSectionGroup("system.net/mailSettings"), _
    7.             MailSettingsSectionGroup)
    8.  
    9.         'Obtain the Network Credentials from the mailSettings section
    10.         Dim credential As New NetworkCredential( _
    11.             settings.Smtp.Network.UserName, settings.Smtp.Network.Password)
    12.  
    13.         'Create the SMTP Client
    14.         Dim client As New SmtpClient()
    15.         client.Host = settings.Smtp.Network.Host
    16.         client.Credentials = credential
    17.  
    18.         'Build Email Message
    19.         Dim email As New MailMessage
    20.  
    21.         email.From = New MailAddress(ConfigurationManager.AppSettings("FakeEmail"))
    22.         For Each address As String In ConfigurationManager.AppSettings("SendToAdmin").Split(New Char() {";"}, StringSplitOptions.RemoveEmptyEntries)
    23.             email.To.Add(address)
    24.         Next
    25.         'email.To.Add(ConfigurationManager.AppSettings("SendToAdmin"))
    26.         email.Subject = "New WLR User Account Request"
    27.         email.IsBodyHtml = True
    28.         email.Body = "<strong>A new user has signed up for access to the WLR</strong>"
    29.         email.Body += "<br><br><strong>The following information is viewable once you login.</strong><br><br>"
    30.         email.Body += "<br><br><strong>Name:</strong> " + Me.lblVerifyName.Text
    31.         email.Body += "<br><strong>Address 1:</strong> " + Me.lblVerifyAdd1.Text
    32.         email.Body += "<br><strong>Address 2:</strong> " + Me.lblVerifyAdd2.Text
    33.         email.Body += "<br><strong>City:</strong> " + Me.lblVerifyCity.Text
    34.         email.Body += "<br><strong>State:</strong> " + Me.lblVerifyState.Text
    35.         email.Body += "<br><strong>Zipcode:</strong> " + Me.lblVerifyZip.Text
    36.         email.Body += "<br><strong>Phone:</strong> " + Me.lblVerifyPhone.Text
    37.         email.Body += "<br><strong>Email:</strong> " + Me.lblVerifyEmail.Text
    38.         email.Body += "<br><strong>Company:</strong> " + Me.lblVerifyCompany.Text
    39.  
    40.         'Send Email
    41.         client.Send(email)
    42.     End Sub
    43.  
    44.     Private Sub CreateRequest()
    45.         Dim mr As New WlrHelper.MembershipRequest
    46.  
    47.         mr.Name = txtName.Text.Trim
    48.         mr.Address1 = txtAdd1.Text.Trim
    49.         mr.Address2 = txtAdd2.Text.Trim
    50.         mr.City = txtCity.Text.Trim
    51.         mr.State = txtState.Text.Trim
    52.         mr.Zip = txtZip1.Text.Trim + IIf(Not String.IsNullOrEmpty(txtZip2.Text.Trim), txtZip2.Text.Trim, "")
    53.         mr.Phone = txtPhoneAreaCode.Text.Trim + txtPhonePrefix.Text.Trim + txtPhonePostfix.Text.Trim
    54.         mr.Email = txtEmail.Text.Trim
    55.         mr.Company = txtCompany.Text.Trim
    56.         WlrHelper.Logger.AddMembershipRequest(mr)
    57.     End Sub
    58.  
    59.     Protected Sub wizRegister_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizRegister.FinishButtonClick
    60.         If Me.CaptchaControl1.UserValidated Then
    61.             Try
    62.                 SendAdminEmail()
    63.                 CreateRequest()
    64.                 Me.wizRegister.Visible = False
    65.                 Me.litFinished.Text = "A request has been submitted for the system administrator(s) to review for account activation!  A system administrator my contact you by phone or email with you account information.  <br /><br />Upon your first sign in you will be required to change your password." '"An email has been sent to the system administrator requesting account activation.  The system administrator my contact you by phone or email with you account information.  <br /><br />Upon your first sign in you will be required to change your password."
    66.                 Me.litFinished.Visible = True
    67.             Catch ex As Exception
    68.                 Me.wizRegister.Visible = False
    69.                 Me.pnlFinished.Font.Size = FontUnit.Point(10)
    70.                 Me.pnlFinished.ForeColor = Drawing.Color.Red
    71.                 Me.litFinished.Text = ex.ToString '"An error occured while trying to create a request for the system administrator(s) to review!  Please try again." '"An error occured while trying to send information to the accout administrator!  Please try again."
    72.                 Me.litFinished.Visible = True
    73.             End Try
    74.         End If
    75.     End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] how to send email notification?

    You really don't need all that code to do it if you are running straight from the server that's sending the email. The Configuration Manager is a great place to store that information, but the reality is, you just need to define the SMTP client, mail.To, mail.From, mail.Subject, and mail.Body, then send it out and clear the object out. I have several scripts running to send out emails based on provided information, and none of them have nearly that much code... except the one that sends two different emails at once...

  4. #4
    Addicted Member
    Join Date
    Jul 2005
    Posts
    194

    Re: [2005] how to send email notification?

    here anothe example in C#:

    http://www.geula.biz/works/sample/as.../sendmail.aspx

    + demo

  5. #5
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: [2005] how to send email notification?

    Quote Originally Posted by timeshifter
    You really don't need all that code to do it if you are running straight from the server that's sending the email. The Configuration Manager is a great place to store that information, but the reality is, you just need to define the SMTP client, mail.To, mail.From, mail.Subject, and mail.Body, then send it out and clear the object out. I have several scripts running to send out emails based on provided information, and none of them have nearly that much code... except the one that sends two different emails at once...

    yea that's very true.... however, i was give him/her an "example"

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  6. #6

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to send email notification?



    thank you vbdotnetboy,

    I will try to understand that whole bunch of codes

  7. #7

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to send email notification?



    timeshifter,
    what about posting it here, if u don't mind

  8. #8
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] how to send email notification?

    I'll post it tomorrow at work.

  9. #9
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] how to send email notification?

    As promised. This is a simple script to email to a given parameter and from the AppSettings block of keys.

    vb Code:
    1. Public Shared Function EmailPassword(ByVal email As String, ByVal password As String)
    2.  
    3.         Dim msg As New MailMessage
    4.  
    5.         msg.To.Add(New MailAddress(email))
    6.         msg.From = New MailAddress(ConfigurationManager.AppSettings("EmailFrom"))
    7.  
    8.         msg.Subject = "Subject"
    9.         msg.IsBodyHtml = True
    10.         msg.Body = "My message.<br>New line."
    11.  
    12.         Dim smtpClient As New SmtpClient
    13.         smtpClient.Host = ConfigurationManager.AppSettings("SMTPServer")
    14.         smtpClient.Send(msg)
    15.  
    16.         Return True
    17.     End Function

  10. #10

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to send email notification?

    timeshifter, thank you very much

  11. #11
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    Quote Originally Posted by timeshifter
    As promised. This is a simple script to email to a given parameter and from the AppSettings block of keys.

    vb Code:
    1. Public Shared Function EmailPassword(ByVal email As String, ByVal password As String)
    2.  
    3.         Dim msg As New MailMessage
    4.  
    5.         msg.To.Add(New MailAddress(email))
    6.         msg.From = New MailAddress(ConfigurationManager.AppSettings("EmailFrom"))
    7.  
    8.         msg.Subject = "Subject"
    9.         msg.IsBodyHtml = True
    10.         msg.Body = "My message.<br>New line."
    11.  
    12.         Dim smtpClient As New SmtpClient
    13.         smtpClient.Host = ConfigurationManager.AppSettings("SMTPServer")
    14.         smtpClient.Send(msg)
    15.  
    16.         Return True
    17.     End Function
    sorry for asking a stupid question,
    i'm still n00b at ASP.NET n using ASP.NET 1.1

    so can u help me on how to send email in ASP.NET 1.1?

    Dim Mail As New Mail.MailMessage

    Mail.To = txtTo.Text

    Mail.From = txtFrom.Text

    Mail.Subject = txtSubject.Text

    Mail.Body = txtBody.Text

    SmtpMail.SmtpServer = "localhost"

    SmtpMail.Send(Mail)
    that's my code n it doesn't work and the error message is "Can't connect to the server"
    as u can see i'm using vb code, so any clue in how to set the smtp server?
    Last edited by AeroConfident; Jun 25th, 2007 at 01:33 AM.

  12. #12
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] how to send email notification?

    I'm afraid I can't help you in 1.1. I keep all important variables defined in the web.config file to ensure consistency. I think the SMTP server for that is defined as the mail server on my internal network... try setting it as 127.0.0.1 and see what it does.

  13. #13
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    ah.. forget about my previous request.

    now i'm changing into ASP.NET 2. and i tried your code.
    but it shows errors

    msg.From = New MailAddress(ConfigurationManager.AppSettings("localhost"))

    it said about the value mustn't null.

    n also 1 thing where should i use the parameter "password" in your code?

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to send email notification?

    It's looking for a key in the web.config file called 'localhost'. First, don't use a key called 'localhost', that's just confusing. Timeshifter used EmailFrom. Create a key in your web.config called EmailFrom, and give it a value of your email address.

  15. #15
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    sorry for asking a n00b Question, but how do i create key in web.config?

    <EmailFrom>
    localhost
    </EmailFrom>
    so after set the code i change my code

    from
    msg.From = New MailAddress(ConfigurationManager.AppSettings("localhost"))


    to
    msg.From = New MailAddress(ConfigurationManager.AppSettings("EmailFrom"))

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to send email notification?

    Code:
    <appSettings>
    <add key="EmailFrom" value="[email protected]" />
    </appSettings>
    This goes under 'configuration'.

  17. #17
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] how to send email notification?

    To call that later, your VB call will look like:
    Code:
    Dim emTo As String = ConfigurationManager.AppSettings("EmailFrom")
    Oh, and thanks for the personal touch, mendhak.

  18. #18
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    it works now many thanks to mendhak n timeshifter

    and i wanna to ask another thing, what should i do if u need to authen to the SMTPServer in order to send the email?

  19. #19
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to send email notification?

    Code:
    Dim auth As New System.Net.NetworkCredential("userid", "pwd")
    smtpClient.Credentials = auth

  20. #20
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    Thx MendHak

  21. #21
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to send email notification?

    Add resolved, dude. Big thread, big searches.

  22. #22
    Member
    Join Date
    Jun 2007
    Posts
    37

    Re: [2005] how to send email notification?

    i can't edit the thread, i'm not a topic starter in here. >.<

  23. #23
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to send email notification?

    I should pay more attention. Slap me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width