locked
Auto email myself on button click RRS feed

  • Question

  • User-1938163999 posted

    Hi,

    I downloaded the code from the asp.net training video on sending email from a website. I copied and pasted the code.  My goal is to simply send myself an email when someone places an order on my site.  I'm getting the following error:

    Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

    Source Error:

    Line 16:     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Line 17:         'Create instance of main mail message class.
    Line 18:         Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
    Line 19: 
    Line 20:         'Configure mail mesage

    My code behind looks like this:

     

      'Create instance of main mail message class.
            Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
    
            'Configure mail mesage
            'Set the From address with user input
            '    mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
            'Get From address in web.config
            mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
            'Another option is the "from" attirbute in the <smtp> element in the web.config.
    
            'Set additinal addresses
            mailMessage.To.Add(New System.Net.Mail.MailAddress(("alicia@shutterbugdesigns.com")))
            'mailMessage.CC
            'mailMessage.Bcc
            'mailMessage.ReplyTo
    
            'Set additional options
            mailMessage.Priority = Net.Mail.MailPriority.High
            'Text/HTML
            mailMessage.IsBodyHtml = False
    
            'Set the subjet and body text
            mailMessage.Subject = "You have an order"
            mailMessage.Body = "You have a photo order"
    
            'Add one to many attachments
            'mailMessage.Attachments.Add(New System.Net.Mail.Attachment("c:\temp.txt")
    
            'Create an instance of the SmtpClient class for sending the email
            Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
    
            'Use a Try/Catch block to trap sending errors
            'Especially useful when looping through multiple sends
            Try
                smtpClient.Send(mailMessage)
            Catch smtpExc As System.Net.Mail.SmtpException
                'Log error information on which email failed.
            Catch ex As Exception
                'Log general errors
            End Try
            
    My app settings in the web config look like this:
     
    figuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
        <appSettings>
                 <add key="fromEmailAddress" value="xxxxxxxxx"/>
        </appSettings>

     <system.net>
            <mailSettings>
                <smtp from="Shutterbug Designs">
                    <network host="xxxxxx" password="xxxxx" userName="xxxxxxx" />
                </smtp>
            </mailSettings>
        </system.net>

     

    What am I doing wrong?

    Thanks,

    Leesha



     

     

    Friday, November 26, 2010 2:32 PM

Answers

  • User-1938163999 posted

     The reason for two Froms................I copied and pasted from other's code. 

     

    And you were right on with the problem!  Thanks so much!

    Leesha

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 26, 2010 3:26 PM

All replies

  • User-231977777 posted

    hi

    why declare "From " twice .

    delete one of them , but you need to declre right email format in the following :

  • <system.net>   
  •        <mailSettings>   
  •            <smtp from="Shutterbug Designs">   
  •                <network host="xxxxxx" password="xxxxx" userName="xxxxxxx" />   
  •            </smtp>   
  •        </mailSettings>   
  •    </system.net

    change it to email form some@some.com , i think this the problem .

Friday, November 26, 2010 3:11 PM
  • User-1938163999 posted

     The reason for two Froms................I copied and pasted from other's code. 

     

    And you were right on with the problem!  Thanks so much!

    Leesha

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 26, 2010 3:26 PM