MailMessage

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
I been looking at this on and off all day banging my head agains a wall, is there any way with MailMessage, that I can see what is in the outbound queues.

I am developing an inhouse control system to tie togther sage, nop commers and make order managment a lot easier for the girls in the office. The problem I am getting is that I have built a mail component using .Net MailMessage provider and seam to have managed to put a delay in there some where.

Messages I sent mid morning are taking a couple of hours to be sent and i wanted to check that it was not been delayed on my pc.

Here the code for the class as it stands at the moment

Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System.Security.Permissions
Imports System
Imports System.Net
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel


Public Class EmailFunctions


    Private fromemailaddress As String = ""
    Private toemailaddress As String = ""
    Private smtpserver As String = ""
    Private messagesubject As String = ""
    Private IsBodyHtm As Boolean = True
    Private mailimp As MailPriority = Net.Mail.MailPriority.Normal
    Private emailmessage As String = ""
    Private myfilearray As New ArrayList
    Private m_enclodingmethod As System.Text.Encoding = System.Text.Encoding.UTF8
    public m_internalstatus As String = ""

    Private Shared mailSent As Boolean = False
    Private Shared mailstatustext As String = ""
    Private Shared mailstatus As Integer = 0
    Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        ' Get the unique identifier for this asynchronous operation.
        Dim token As String = CStr(e.UserState)

        If e.Cancelled Then
            mailstatustext = "Send canceled." & token
            mailstatus = 1
        End If
        If e.Error IsNot Nothing Then
            Try
                mailstatustext = token & " " & e.Error.ToString()
                mailstatus = -1
            Catch ex As Exception
                mailstatustext = e.Error.ToString()
                mailstatus = -1
            End Try
        Else
            mailstatustext = "Message sent."
            mailstatus = 0
        End If
        mailSent = True
    End Sub

    Public Function GetMessageStatus() As String
        'Gets the message status
        Return mailstatustext
    End Function

    Public Sub SetEncodingmethod(Encodingmethod As System.Text.Encoding)
        'Sets the email encoding method!
        m_enclodingmethod = Encodingmethod
    End Sub

    Public Sub addfile(ByVal filename As String)
        'Add a record to the file array!
        myfilearray.Add(filename)
    End Sub

    Public Sub setfrom(ByVal setaddress As String)
        'This sets the from email address!
        fromemailaddress = setaddress
    End Sub
    Public Sub setsmtp(ByVal setsmtp As String)
        'this sets the smtp mail address
        smtpserver = setsmtp
    End Sub
    Public Sub setto(ByVal settoaddress As String)
        'this sets the to email address
        toemailaddress = settoaddress
    End Sub
    Public Sub subject(ByVal setsubject As String)
        'this sets the email subject!
        messagesubject = setsubject
    End Sub
    Public Sub ishtmnl(ByVal setIsBodyHtm As Boolean)
        'Set the body flag to and from html!
        IsBodyHtm = setIsBodyHtm
    End Sub
    Public Sub MailPriority(ByVal setMailPriority As MailPriority)
        'Set the mail priority
        mailimp = setMailPriority
    End Sub
    Public Sub message(ByVal setemailmessage As String)
        emailmessage = setemailmessage.Trim
    End Sub
    Public Function sendemail(Optional SendAsunc As Boolean = False, Optional tokenmessage As String = "") As Boolean
        'this send the email
        Dim r As Boolean = False
        Try
            Dim MailMsg As New MailMessage
            MailMsg.From = New MailAddress(toemailaddress.Trim())
            MailMsg.To.Add(toemailaddress)
            MailMsg.Subject = messagesubject
            MailMsg.Body = emailmessage
            MailMsg.Priority = mailimp
            MailMsg.IsBodyHtml = IsBodyHtm
            MailMsg.SubjectEncoding = m_enclodingmethod
            'now add the files to the array list!
            If myfilearray.Count >= 1 Then
                For a As Integer = 0 To myfilearray.Count - 1
                    Dim MsgAttach As New Attachment(myfilearray(a))
                    MailMsg.Attachments.Add(MsgAttach)
                Next
            End If

            Dim SmtpMail As New SmtpClient
            SmtpMail.Timeout = 100
            SmtpMail.Host = smtpserver

            If SendAsunc = True Then AddHandler SmtpMail.SendCompleted, AddressOf SendCompletedCallback

            If SendAsunc = False Then SmtpMail.Send(MailMsg)
            If SendAsunc = True Then SmtpMail.SendAsync(MailMsg, tokenmessage)
            If myfilearray.Count >= 1 Then
                myfilearray.Clear()
            End If

            'checks to
            If mailSent = False And mailstatus = 0 Then
                If SendAsunc = True Then
                    SmtpMail.SendAsyncCancel()
                    r = False
                End If

            End If

            Return r
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function
End Class

Can any one see any problems.
11 years ago
Fixed the problem on a round about way, figged out in the end it was an SMTP time out problem not sending all the emails so built in a buffer, that corrected the fault.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.