Help - duplicate emails sent from my application

Discussion in 'Visual Studio' started by g6swj, Dec 2, 2014.

  1. Hi - sorry if I have posted this in the wrong place! - I'm new here :)

    Any pointers would really be appreciated - thank you in advance

    I am self taught so my code is probably fairly crappy - be gentle!!


    I have a aspx web page that uses vb to send an email. The problem is that sometime it sends 1 copy or more (worst up to five) copies of the same email which as the attachment is approx. 1mb makes me a little unpopular let alone not being professional. What am I doing wrong.

    My code (slightly edited to protect confidentiality ) is below

    The email send bit is here
    'Email creation
    If File.Exists(fl) = TrueThen
    Dim mail As NewMailMessage()
    'set the addresses
    mail.From = NewMailAddress(valid email address here)
    mail.To.Add(reportparam3)
    mail.Bcc.Add("valid email address here")

    'set the content
    mail.Subject = "subject"
    Dim mailmessage AsString

    'add an attachment from the filesystem
    mail.Attachments.Add(NewAttachment("e:\web\xxx\htdocs\tempreportstore\" & filename))
    Dim smtpmail AsNewSmtpClient
    smtpmail.Host = "localhost"
    smtpmail.Send(mail)

    the whole page code here
    ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
    IfNot Page.IsPostBack Then
    'Configure data sources
    Me.ReportViewer_CT2.LocalReport.DataSources.Clear()
    Dim rds AsReportDataSource = NewReportDataSource("rptds_getresult", Me.SqlDataSource_getresults)
    Me.ReportViewer_CT2.LocalReport.DataSources.Add(rds)
    Dim rds2 AsReportDataSource = NewReportDataSource("rptds_getpersonaverage", Me.SqlDataSource_getpersonaverage)
    Me.ReportViewer_CT2.LocalReport.DataSources.Add(rds2)
    AddHandler ReportViewer_CT2.LocalReport.SubreportProcessing, AddressOf JDASubreportProcessingEventHandler
    Dim reportparam1, reportparam2, reportparam3, reportparam4, reportparam5 AsString
    reportparam1 = Request.QueryString("ddddddd")
    reportparam2 = Request.QueryString("name")
    reportparam3 = Trim(Request.QueryString("email"))
    reportparam4 = Request.QueryString("pfilename")
    reportparam5 = Request.QueryString("xxxxx") 'newsletter
    Dim p(2) AsReportParameter
    p(0) = NewReportParameter("rpt_p_intperson_obj_no", reportparam1)
    p(1) = NewReportParameter("rpt_p_name", reportparam2)
    Me.ReportViewer_CT2.LocalReport.SetParameters({p(0), p(1)})
    Dim b AsByte()
    b = Me.ReportViewer_CT2.LocalReport.Render("PDF", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
    Dim getdate AsDate = Now()
    Dim filedate AsString
    filedate = getdate.ToString("ddMMyy")
    Dim filename AsString = "abcd_" & Trim(reportparam4) & "_" & filedate & "_IC00" & Trim(reportparam1) & ".pdf"
    Dim fl AsString = "e:\web\abcd\htdocs\tempreportstore\" & filename
    Dim virtualDir = "\\" & filename
    IfFile.Exists(fl) = TrueThen
    File.Delete(fl)
    EndIf
    'XXXXXXXXXXXXXXXXXXXXXXXXXActivate lines below for live
    Dim fs AsNewFileStream(fl, FileMode.Create)
    fs.Write(b, 0, b.Length)
    fs.Close()
    'XXXXXXXXXXXXXXXXXXXXXX
    'Email creation
    IfFile.Exists(fl) = TrueThen
    Dim mail AsNewMailMessage()
    'set the addresses
    mail.From = NewMailAddress(valid email address here)
    mail.To.Add(reportparam3)
    mail.Bcc.Add("valid email address here")
    If reportparam1 > 50 Then
    mail.Bcc.Add("valid email address here")
    EndIf
    'set the content
    mail.Subject = "subject"
    Dim mailmessage AsString
    If LCase(reportparam5) = "y"Then
    mailmessage = "Text."
    mailmessage = mailmessage & vbCrLf & vbCrLf & "Text."
    Else
    mailmessage = "Text."
    mailmessage = mailmessage & vbCrLf & vbCrLf & "Text."
    mailmessage = mailmessage & vbCrLf & vbCrLf & "text"
    'add an attachment from the filesystem
    mail.Attachments.Add(NewAttachment("e:\web\xxx\htdocs\tempreportstore\" & filename))
    Dim smtpmail AsNewSmtpClient
    smtpmail.Host = "localhost"
    smtpmail.Send(mail)
    'Update dtmReportRun
    Dim connectionString AsString = WebConfigurationManager.ConnectionStrings("connectionstring_xxx").ConnectionString
    Dim con AsSqlConnection = NewSqlConnection(connectionString)
    Dim cmd AsNewSqlCommand("sp_SetReportRunDate", con)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add("@personID", SqlDbType.VarChar, 10).Value = Trim(reportparam1)
    Try
    cmd.Connection.Open()
    cmd.ExecuteNonQuery()
    Catch ex AsException
    EndTry
    cmd.Connection.Close()
    'End Update dtmReportRun
    Response.Redirect("http://valid address)
    EndIf
    EndIf
    EndSub
     

Share This Page