none
vb.net getting rid of CDO usage

    Question

  • Folks,

    I have inherited some vb.net code from a predecessor, that was using CDO to send emails. Now, I've been tasked to just use the standard smpt protocol to send these emails. I have no issue with creating new emails, in sending, etc. The issue I am having, is with iterating the list of emails waiting to be sent by the existing function. The function today, looks like this:

        Private Function GetMessages(ByVal dirName As String) As EmailMessage()
            Dim messages As New List(Of EmailMessage)

            Dim dropDir As New DropDirectory
            Dim cdoMessages As IMessages

            cdoMessages = dropDir.GetMessages(dirName)

            If Not cdoMessages Is Nothing Then

                Me.LogString &= String.Format("Total messages found: {0}", cdoMessages.Count) & vbCrLf & vbCrLf

                For Each msg As Message In cdoMessages
                        messages.Add(New EmailMessage(msg))
                        messages(messages.Count - 1).FileName = cdoMessages.FileName(msg)
                Next

                cdoMessages = Nothing
            End If

            dropDir = Nothing

            Return messages.ToArray()
        End Function

    Now, the issue seems to be the "dropdirectory" that this was using to iterate the messages in that dir. I don't see the equivalent in smpt, without using "pickupdirectorylocation" which seems to be on the smpt server, and not for me to "poll" my local server from, to then send emails to my smpt server. Basically, I have an inbound email folder to pass to my vb script, that I want to iterate through, to get all the messages, and then send them to my smtp server. 

    Suggestions? Thoughts? Help? :)

    Thanks in advance, 

    mpleaf

    Monday, March 13, 2017 9:50 PM

Answers

  • The .NET SmtpClient Class does not have the capability of reading messages from a mail folder. It is primarily for sending e-mail via SMTP. You would have to stick with CDO for picking up these e-mail messages and then using the SmtpClient Class to send.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by mpleaf Wednesday, March 29, 2017 8:41 PM
    Wednesday, March 15, 2017 12:57 PM

All replies

  • Your code is VBA/VB code it is not VBScript.  You need to post in the correct forum.

    \_(ツ)_/

    Tuesday, March 14, 2017 1:19 AM
  • Sorry JRV...cam you direct me to that proper forum? Is there one here on technet?

    mpleaf

    Tuesday, March 14, 2017 3:37 PM
  • Your question has been moved by Bill Stewart.  You are in the correct forum for VBA.


    \_(ツ)_/

    Tuesday, March 14, 2017 5:56 PM
  • Hello,

    This forum is for issues about Outlook Object Model. According to your code and description, you are sending mails by SMTP, so i would move this thread into VB.NET forum.

    Sorry for any inconvenience.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 15, 2017 9:34 AM
  • Hello,

    There are basic to advance examples here

    http://www.systemnetmail.com/default.aspx

    There is no equivalent for DropDirectory, just PickupDirectory which you can specify in app.config where in the following case MailDrop is a folder below the app folder.

    <system.net>
      <mailSettings>
        <smtp from="xxxxx@somedomain.gov">
          <network host="your host" port="25" />
          <specifiedPickupDirectory pickupDirectoryLocation="MailDrop" />
        </smtp>
      </mailSettings>
    </system.net>

    You can ensure the folder exists (no matter where you have it) by using a after build macro for the project e.g.

    <Target Name="AfterBuild">
      <MakeDir Directories="$(OutDir)MailDrop" Condition="!Exists('$(OutDir)MailDrop')" />
      <ItemGroup>
        <Templates Include="$(ProjectDir)MailDrop\*.*" />
      </ItemGroup>
      <Copy SourceFiles="@(Templates)" DestinationFolder="$(OutDir)MailDrop" />
    </Target>
    

    Generally speaking email files generated using PickupDirectory are .eml files which there is no existing functions in .net to parse them without a good deal of work but most major email programs such as Outlook can parse them just fine.

    This NuGet package coupled with this example may help sending eml files via SMTP.

    Bottom line is sending eml can very well be problematic.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 15, 2017 12:56 PM
    Moderator
  • The .NET SmtpClient Class does not have the capability of reading messages from a mail folder. It is primarily for sending e-mail via SMTP. You would have to stick with CDO for picking up these e-mail messages and then using the SmtpClient Class to send.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by mpleaf Wednesday, March 29, 2017 8:41 PM
    Wednesday, March 15, 2017 12:57 PM
  • Thanks Paul. I pretty much determined that as well. I ended up using a solution from codeproject to help direct me how to get the pieces I needed.

    https://www.codeproject.com/kb/office/eml_readerex.aspx

    Thanks again!

    mpleaf

    Wednesday, March 29, 2017 8:42 PM