Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Why is my project sending two e-mails? RSS

4 replies

Last post Aug 31, 2009 04:04 PM by rgouette

(0)
  • rgouette

    rgouette

    Member

    7 Points

    105 Posts

    Why is my project sending two e-mails?

    Aug 28, 2009 03:49 PM | LINK

    Hi lads, my Silverlight 2.x / .asmx app is sending two e-mails when I'm clicking the send button once.

    I stepped through th code, but saw nothing that caught my eye..

     

    page.xaml below, followed by page.xaml code behind follows , then service code behind.

     

    <UserControl x:Class="AskTheCIO_082709.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:askthecio="clr-namespace:AskTheCIO_082709"
        
        Width="400" Height="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
                    <Storyboard x:Name="grid1_fadeout" Completed="grid1_fadeout_Completed">
                        <DoubleAnimation
                        Storyboard.TargetName="grid1"
                        Storyboard.TargetProperty="Opacity"
                        From="1.0" To="0.0" Duration="0:0:2" 
                        AutoReverse="False"/>
                    </Storyboard>
            </Grid.Resources>
                <Grid.RowDefinitions>
        		<RowDefinition Height="0.19*"/>
        		<RowDefinition Height="0.12*"/>
        		<RowDefinition Height="0.49*"/>
        		<RowDefinition Height="0.2*"/>
        	</Grid.RowDefinitions>
        	<Grid Margin="4,4,1,-7" Grid.RowSpan="4" x:Name="grid1">
        		<TextBlock Height="Auto" Margin="144,60,163,0" x:Name="tb_askthecio" VerticalAlignment="Top" TextWrapping="Wrap" d:LayoutOverrides="Width"><Run FontSize="14" Text="Ask The CIO"/></TextBlock>
        		<TextBlock Height="23" HorizontalAlignment="Center" Margin="9,97,26,0" x:Name="tb_questions" VerticalAlignment="Top" FontSize="12" Text="Comments or Questions:" TextWrapping="Wrap"/>
        		<TextBox Margin="-1,124,0,71" x:Name="txt_comments" Text="" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True"/>
        		<Image Height="42" Margin="28.957,4,26.043,0" VerticalAlignment="Top" Source="MH_Logo.png"/>
        		<Button Content="Send Message" x:Name="btn_submit" ToolTipService.ToolTip="Send this Message..." Click="btn_submit_Click" Height="35" VerticalAlignment="Bottom" Margin="112,0,121,20"/>
        	</Grid>
        	<TextBox Margin="101,5,119,15" Grid.Row="3" Text="" TextWrapping="Wrap" x:Name="txt_complete" Visibility="Collapsed" BorderThickness="1,1,1,1" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="12" d:IsHidden="True"/>
    
        </Grid>
    </UserControl>
    
    
     

     

     

    Imports System
    Imports System.Net
    Imports AskTheCIO_082709
    
    
    Partial Public Class Page
        Inherits UserControl
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            'Find the URL we're currently running in (Local DEV, WebDev Staging, Production, etc.)
            Dim serviceURI As String = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri
    
            'Add the name of our local proxy web service file to the URL
            'In this case, we need a proxy because SharePoint requires HTTP authentication that Silverlight can't handle
            serviceURI = serviceURI.Substring(0, serviceURI.LastIndexOf("/")) & "/svcMailer.asmx"
    
            'Create an instance of our proxy web service that we defined under Service References
            Dim svcmailer As Mailsender.svcMailerSoapClient = New Mailsender.svcMailerSoapClient
    
    
    
            'Use the URL we generated to set the endpoint for the proxy web service
            svcmailer.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri(serviceURI))
    
    
            'Set the Security Mode of the endpoint to match our SSL usage
            If serviceURI.ToLower.Contains("http:") Then
                svcmailer.Endpoint.Binding = New System.ServiceModel.BasicHttpBinding(ServiceModel.BasicHttpSecurityMode.None)
            Else
                svcmailer.Endpoint.Binding = New System.ServiceModel.BasicHttpBinding(ServiceModel.BasicHttpSecurityMode.Transport)
            End If
            'here we create a handler for the data
            AddHandler svcmailer.sendmailCompleted, AddressOf receivehandler
    
            'here we send um something....
            Try
                ''Fire off our spinner(in Spinner XAML)
                '////////////////////////////////////////////
                '                      '////////////////////////////////////////////
                svcmailer.sendmailAsync("anotherusername@mmc.org", "username@mmc.org", txt_comments.Text.Trim)
    
    
            Catch ex As Exception
    
                txt_complete.Text = ex.ToString
    
            End Try
    
        End Sub
        Private Sub receivehandler(ByVal sender As Object, ByVal e As MailSender.sendmailCompletedEventArgs)
            'display the webservice's return in a text box
            Try
                txt_complete.Text = e.Result.ToString
                grid1_fadeout.Begin()
            Catch es As Exception
                txt_complete.Text = "XAML Error:" & es.InnerException.ToString
            End Try
    
    
            Exit Sub
        End Sub
    
        Private Sub grid1_fadeout_Completed(ByVal sender As System.Object, ByVal e As System.EventArgs)
            txt_complete.Visibility = Windows.Visibility.Visible
        End Sub
    
    End Class
    

     

     

     

     

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Net.Mail
    Imports AskTheCIO_082709.Web
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    ' <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class svcMailer
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Function sendmail(ByVal fromAddress As String, ByVal toAddress As String, ByVal txt_comments As String) As String
            Try
                'check boxes
    
                'create the mail message
                Dim mail As New MailMessage()
                'set the addresses
                mail.From = New MailAddress(fromAddress)
                mail.To.Add(toAddress)
    
                'set the content
                mail.Subject = "From Ask the CIO Webform"
    
                'to embed images, we need to use the prefix 'cid' in the img src value
                'the cid value will map to the Content-Id of a Linked resource.
                'thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
                ' removed width=""275"" ""height=75""
                Dim strBody As String
                'Lets format the body text,
                strBody = "<html>" & vbNewLine & _
    "<body><img src=cid:companylogo><br>" & vbNewLine & _
    "<br><b>" & "Comments: </b>" & "<br>" & txt_comments & _
    "</body>" & vbNewLine & _
    "</html>"
    
    
                Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(strBody, Nothing, "text/html")
    
                'create the LinkedResource (embedded image)
                Dim logo As New LinkedResource(My.Request.MapPath("MH_Logo.png"))
                logo.ContentId = "companylogo"
    
                'add the LinkedResource to the appropriate view
                htmlView.LinkedResources.Add(logo)
    
                'add the views
                mail.AlternateViews.Add(htmlView)
    
    
                'send the message
                Dim smtp As New SmtpClient("mail.mmc.org") 'specify the mail server address
                smtp.Send(mail)
                Return "Message Sent!"
            Catch ex As Exception
                Return "There was a problem sending your message:" + vbCrLf + ex.Message
    
            End Try
        End Function
    End Class
    
     

  • Ken Tucker

    Ken Tucker

    All-Star

    23250 Points

    3534 Posts

    Re: Why is my project sending two e-mails?

    Aug 29, 2009 10:54 AM | LINK

    Only thing I can think is that the send button is being clicked twice before the message is done being sent.  Maybe add a boolean variable boolOkToSend to your project.  In the button press event only send the email if the boolOkToSend is true

     Dim boolOkToSend as boolean=true

    13       Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    14           'Find the URL we're currently running in (Local DEV, WebDev Staging, Production, etc.)

    If Not boolOkToSend then

    return

    else

    boolOkToSend = false

    end if

    etc..

     

    56       Private Sub receivehandler(ByVal sender As Object, ByVal e As MailSender.sendmailCompletedEventArgs)
    57           'display the webservice's return in a text box
    58           Try
    59               txt_complete.Text = e.Result.ToString
    60               grid1_fadeout.Begin()
    61           Catch es As Exception
    62               txt_complete.Text = "XAML Error:" & es.InnerException.ToString
    63           End Try
    64   boolOkToSend = true
    65  
    66           Exit Sub
    67       End Sub


    15           Dim serviceURI As String = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri

  • Brauliod

    Brauliod

    Contributor

    2448 Points

    744 Posts

    Re: Why is my project sending two e-mails?

    Aug 29, 2009 02:59 PM | LINK

    Mmmm...

      The issue can come from three different sources:

    • Silverlight button (strange stuff).
    • Service call.
    • SMTP Server (like the one that could be the guilty). 

      I would say, let's check first which part is the offending one (try in this order):

    • Create a console application and send an email using your code, got two emails? (SMTP Server offending).
    • Create a dummy app that calls your service (even ad dummy SL page, onLoad... or something like that). Got twice? (Service call issue).
    • Silverlight, mmm... a dummy test to check whether the button is called twice add a member varialbe, set it to zero, each time the button click code is run increment by one the varialbe (show a message if the varialbe has a value greater than one).

      Once we know for sure where is the error we can check how to proceed.

       Good luck

        Braulio

  • Brauliod

    Brauliod

    Contributor

    2448 Points

    744 Posts

    Re: Re: Why is my project sending two e-mails?

    Aug 29, 2009 03:03 PM | LINK

    Another thing that will help: laucnh fiddler and check if there are two calls to the service when you click on the button.

  • rgouette

    rgouette

    Member

    7 Points

    105 Posts

    Re: Re: Re: Why is my project sending two e-mails?

    Aug 31, 2009 04:04 PM | LINK

    The source of the issue has been identified.

    Thanks lads for your help.

    The issue was ..um...programmer induuced..

    Suffice it  to say, it was normal behaviour....

    [:$]