Click here to Skip to main content
15,885,895 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: I need your VB6 programs! Pin
DutchComputerKid20-May-16 6:27
DutchComputerKid20-May-16 6:27 
GeneralRe: I need your VB6 programs! Pin
DutchComputerKid20-May-16 9:53
DutchComputerKid20-May-16 9:53 
GeneralRe: I need your VB6 programs! Pin
DutchComputerKid2-Sep-16 0:07
DutchComputerKid2-Sep-16 0:07 
GeneralRe: I need your VB6 programs! Pin
Chris Quinn2-Sep-16 1:16
Chris Quinn2-Sep-16 1:16 
GeneralRe: I need your VB6 programs! Pin
DutchComputerKid2-Sep-16 2:19
DutchComputerKid2-Sep-16 2:19 
QuestionAdding Items to data-bound Combo Box Pin
Raabi Anony12-May-16 20:06
Raabi Anony12-May-16 20:06 
AnswerRe: Adding Items to data-bound Combo Box Pin
Dave Kreskowiak13-May-16 1:07
mveDave Kreskowiak13-May-16 1:07 
QuestionTranslating and understanding what this code block does in c#, adn see if I translated it right for me to use. Pin
jkirkerx12-May-16 13:52
professionaljkirkerx12-May-16 13:52 
I'm having problems with sending email using CBeyond from all of my programs in VB.
So I decided to change my send function to SendAsync, and Async, and just send each message 1 at at a time and wait for the results so it can be logged when done. (Thus the SQLite Question earlier)

So I thought this would be pretty easy, just change SmtpClient.Send(mailMessage) to smtpClient.SendAsync(mailMessage, cancelToken), but I guess now I need to add a callback.

So I got some ideas from this link, and used some of the concepts.
The part I don't understand is calling the function, and setting the Public Event for the callback.

c# - Send email when using SmtpClient - Stack Overflow[^]

I c#, the code is this, but I have no clue what it is, it's beyond my c# skills.
C#
var sender = new EmailSender();

sender.SendEmailCompleted += (o, eventArgs) 
  => Console.WriteLine(eventArgs.RetryCount);

sender.SendEmailAsync(new MailMessage(), 5);
And I translated it using CodeTranslator CodeTranslator: Code Translation From VB.NET
I'm looking at it and I have no clue what it is, I only recognize the first and last lines.
VB
Dim sender As var = New EmailSender
sender.SendEmailCompleted = (sender.SendEmailCompleted + o)
,eventArgs
Unknown=GreaterConsole.WriteLine(eventArgs.RetryCount)
sender.SendEmailAsync(New MailMessage, 5)
This is what I wrote from the C# example in the web page link. I'm pretty sure I got it right and updated some of the event handlers.
VB
Imports System.ComponentModel
Imports System.Net.Mail

Public Class SendEngine

    Private _currentRetryCount As Integer
    Private _maxRetryCount As Integer
    Private _mailMessage As MailMessage
    Private _isAlreadyRun As Boolean
    Public Event SendEmailCompleted As SendEmailCompletedEventHandler

    Public Sub SendEmailAsync(ByVal smtpClient As SmtpClient, ByVal message As MailMessage, ByVal retryCount As Integer)

        If _isAlreadyRun Then
            Throw New InvalidOperationException("sendEngine doesn't support multiple concurrent invocations.")
        End If

        _isAlreadyRun = True
        _maxRetryCount = retryCount
        _mailMessage = message

        AddHandler smtpClient.SendCompleted, AddressOf SmtpClientSendCompleted

        SendMessage(smtpClient)

    End Sub
    Private Sub SendMessage(ByVal smtpClient As SmtpClient)

        Try
            smtpClient.SendAsync(_mailMessage, Guid.NewGuid)

        Catch exception As Exception

            EndProcessing(smtpClient)

        End Try

    End Sub
    Private Sub EndProcessing(ByVal smtpClient As SmtpClient)

        If (_mailMessage IsNot Nothing) Then
            _mailMessage.Dispose()
        End If

        If (smtpClient IsNot Nothing) Then

            RemoveHandler smtpClient.SendCompleted, AddressOf SmtpClientSendCompleted
            smtpClient.Dispose()

        End If

        OnSendCompleted(New SendEmailCompletedEventArgs(Nothing, False, Nothing, _currentRetryCount))

        _isAlreadyRun = False
        _currentRetryCount = 0

    End Sub
    Private Sub SmtpClientSendCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)

        Dim smtpClient = CType(sender, SmtpClient)
        If ((e.Error Is Nothing) OrElse (_currentRetryCount >= _maxRetryCount)) Then

            EndProcessing(smtpClient)

        Else

            _currentRetryCount = (_currentRetryCount + 1)
            SendMessage(smtpClient)

        End If

    End Sub
    Protected Overridable Sub OnSendCompleted(ByVal args As SendEmailCompletedEventArgs)

        RaiseEvent SendEmailCompleted(Me, args)

    End Sub

End Class

Public Delegate Sub SendEmailCompletedEventHandler(ByVal sender As Object, ByVal e As SendEmailCompletedEventArgs)

Public Class SendEmailCompletedEventArgs
    Inherits AsyncCompletedEventArgs

    Private mRetryCount As Integer

    Public Sub New(ByVal ex As Exception, ByVal canceled As Boolean, ByVal userState As Object, ByVal retryCount As Integer)

        MyBase.New(ex, canceled, userState)
        retryCount = retryCount

    End Sub

    Public Property RetryCount As Integer
        Get
            Return mRetryCount
        End Get

        Set(value As Integer)
            value = mRetryCount
        End Set

    End Property

End Class


modified 13-May-16 3:00am.

AnswerRe: Translating and understanding what this code block does in c#, adn see if I translated it right for me to use. Pin
Richard Deeming13-May-16 2:10
mveRichard Deeming13-May-16 2:10 
GeneralRe: Translating and understanding what this code block does in c#, adn see if I translated it right for me to use. Pin
jkirkerx13-May-16 4:07
professionaljkirkerx13-May-16 4:07 
GeneralRe: Translating and understanding what this code block does in c#, adn see if I translated it right for me to use. Pin
Richard Deeming13-May-16 4:16
mveRichard Deeming13-May-16 4:16 
GeneralRe: Translating and understanding what this code block does in c#, adn see if I translated it right for me to use. Pin
jkirkerx13-May-16 5:02
professionaljkirkerx13-May-16 5:02 
QuestionData string manipulation in VB 2010 Pin
Gus11312-May-16 0:26
Gus11312-May-16 0:26 
AnswerRe: Data string manipulation in VB 2010 Pin
Richard MacCutchan12-May-16 0:38
mveRichard MacCutchan12-May-16 0:38 
AnswerRe: Data string manipulation in VB 2010 Pin
Robert g Blair12-May-16 12:27
Robert g Blair12-May-16 12:27 
GeneralRe: Data string manipulation in VB 2010 Pin
Gus11313-May-16 2:19
Gus11313-May-16 2:19 
GeneralRe: Data string manipulation in VB 2010 Pin
Gus11318-May-16 4:15
Gus11318-May-16 4:15 
GeneralRe: Data string manipulation in VB 2010 Pin
Robert g Blair18-May-16 10:47
Robert g Blair18-May-16 10:47 
GeneralRe: Data string manipulation in VB 2010 Pin
Gus11318-May-16 19:52
Gus11318-May-16 19:52 
GeneralRe: Data string manipulation in VB 2010 Pin
Robert g Blair19-May-16 11:30
Robert g Blair19-May-16 11:30 
GeneralRe: Data string manipulation in VB 2010 Pin
Gus11319-May-16 22:41
Gus11319-May-16 22:41 
GeneralRe: Data string manipulation in VB 2010 Pin
Gus11319-May-16 23:33
Gus11319-May-16 23:33 
GeneralRe: Data string manipulation in VB 2010 Pin
Robert g Blair22-May-16 12:12
Robert g Blair22-May-16 12:12 
QuestionAdd unsubscribe link to header of the mail sent from outlook Pin
srikrishnathanthri11-May-16 20:15
srikrishnathanthri11-May-16 20:15 
AnswerRe: Add unsubscribe link to header of the mail sent from outlook Pin
Richard Deeming12-May-16 2:02
mveRichard Deeming12-May-16 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.