locked
My semi working email code in WPF app vb RRS feed

  • Question

  • HI all as some of you may have seen some of my recent posts i have been trying to send emails from WPF Vb app here is where I am at after taking on suggestions about the way i originally was going to run it though rich texts.

    I'm still having many problems with the code but am now at a loss as to what to try next. I have provided the code that I have for hopefully someone to have a look and build of just to see if they are also having the same troubles i am having and maybe offer a solution.

    As you may see that when the to and from fields are filled in the server thinks it's trying to send more than one message. so i tried only the to field and that throws its own error along with not putting an address in either field which again another error occurred lol.

    If it makes it any easier it will only ever have to send to one email address, it's not going to be used for bulk emailing so in theory if the email is locked somehow and hidden that's fine. It also doesn't need to have a from email I only added it as to see if i could get the basics working.

    As you will see when you have added your own details it does find the server so its definitely doing something.

    I have removed my credentials from the code as a heads up

    Imports System.Net
    Imports System.Net.Mail
    Imports System.Net.Sockets
    Imports System.IO
    Imports System.Text
    Class MainWindow
        Private Sub BtnSend_Click(sender As Object, e As RoutedEventArgs) Handles BtnSend.Click
    
            Dim emailTo As String = txtEmailTo.Text.Trim()
            Dim emailFrom As String = txtEmailFrom.Text.Trim()
            Dim emailSubject As String = txtEmailSubject.Text.Trim()
            Dim emailBody As String = txtEmailBody.Text.Trim()
    
            Dim smtpServer As New SmtpClient
    
            smtpServer.Host = "smpt.xxxxxx.com"
            smtpServer.Timeout = 60
            smtpServer.Port = 465
    
            smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
    
            Dim username As String = "xxxxxxx"
            Dim password As String = "xxxxxxx"
    
            smtpServer.Credentials = New NetworkCredential(username, password)
    
            Dim message As New MailMessage(emailFrom, emailTo)
    
            message.Subject = emailSubject
            message.Body = emailBody
            message.BodyEncoding = System.Text.Encoding.UTF8
            message.Headers.Add("reply-To", "Email_From")
            message.Headers.Add("X-Organization", "Home and Learn")
    
            AddHandler smtpServer.SendCompleted, AddressOf DoSendCompleted
    
            Dim userState As String = " - Mail Message"
            smtpServer.SendAsync(message, userState)
    
            Try
                smtpServer.SendAsync(message, userState)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
    
            End Try
    
        End Sub
    
        Private Sub DoSendCompleted(Sender As Object, e As ComponentModel.AsyncCompletedEventArgs)
    
    
            Dim Token As String = e.UserState.ToString
            If e.Error IsNot Nothing OrElse e.Cancelled Then
                MessageBox.Show("error " & Token)
            Else
                MessageBox.Show("Message Sent " & Token)
    
            End If
    
        End Sub
    End Class

    MainWindow.xaml

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:emailform"
            mc:Ignorable="d"
            Title="MainWindow" Height="487" Width="454">
        <Grid Margin="0,0,2,-1">
            <TabControl HorizontalAlignment="Left" Height="485" Margin="0,-25,-1,-3" VerticalAlignment="Top" Width="445">
                <TabItem Header="Send Emails">
                    <Grid Background="#FFE5E5E5" Margin="0,0,-1,-33">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="37*"/>
                            <ColumnDefinition Width="408*"/>
                        </Grid.ColumnDefinitions>
                        <Label Content="TO:" HorizontalAlignment="Left" Margin="5,70,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="27"/>
                        <TextBox x:Name="txtEmailTo" HorizontalAlignment="Left" Height="23" Margin="5,95,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250" RenderTransformOrigin="0.167,0" Grid.Column="1"/>
                        <Label Content="FROM:" HorizontalAlignment="Left" Margin="5,119,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="45"/>
                        <TextBox x:Name="txtEmailFrom" HorizontalAlignment="Left" Height="23" Margin="5,145,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250" RenderTransformOrigin="0.167,0" Grid.Column="1"/>
                        <Label Content="SUBJECT:" HorizontalAlignment="Left" Margin="7,169,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="58"/>
                        <TextBox x:Name="txtEmailSubject" HorizontalAlignment="Left" Height="23" Margin="5,195,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250" RenderTransformOrigin="0.167,0" Grid.Column="1"/>
                        <TextBox x:Name="txtEmailBody" HorizontalAlignment="Left" Height="120" Margin="5,245,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250" RenderTransformOrigin="0.167,0" HorizontalScrollBarVisibility="Auto" Grid.Column="1"/>
                        <Button x:Name="BtnSend" Content="Send Email" HorizontalAlignment="Left" Margin="5,375,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="20"/>
                    </Grid>
                </TabItem>
                <TabItem Header="Receive Emails">
                    <Grid Background="#FFE5E5E5" Margin="0,0,-6,0" Width="400"/>
                </TabItem>
            </TabControl>
    
        </Grid>
    </Window>
    Any feedback on where the problem is coming from in the code or domain would be much appreciated. 



    • Edited by Dazzz12345 Sunday, August 23, 2020 12:05 AM
    Sunday, August 23, 2020 12:01 AM

All replies

  • Hi

    Don't know if this is of any help.  This code should work OK (the original message was received correctly - many times) - the recipient went to great lengths to let me know 😊 They ended up getting an email with 33 repeated text blocks in it!

    ' Form1 with Button1
    Option Strict On
    Option Explicit On
    Imports System.Net.Mail
    Public Class Form1
      Dim [From] As String
      Dim [To] As String
      Dim [subject] As String
      Dim [body] As String
      Dim HowMany As Integer = 1
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        [From] = "NoBody@SomeWhere.Com"
        [To] = "SomeBody@SomeWhere.com"
        [subject] = "This email is going through OK"
        For i As Integer = 1 To HowMany
          [body] = "PLEASE STOP! I have had " & i.ToString & " sufficient reminders so far and it seems they are still coming! One is ENOUGH!"
          SendEmail()
        Next
      End Sub
      Private Sub SendEmail()
        Try
          Using e_mail As New MailMessage()
            e_mail.From = New MailAddress([From])
            e_mail.To.Add(New MailAddress([To]))
            e_mail.Subject = [subject]
            e_mail.IsBodyHtml = False
            e_mail.Body = [body]
            Using Smtp_Server As New SmtpClient("mail.btinternet.com", 25)
              Smtp_Server.UseDefaultCredentials = True
              Smtp_Server.Credentials = New Net.NetworkCredential("XXXX@YYYYY.com", "someverygoodpassword")
              Smtp_Server.EnableSsl = False
              Smtp_Server.Send(e_mail)
            End Using
            MessageBox.Show("Mail Sent")
          End Using
        Catch error_t As Exception
          MessageBox.Show(error_t.ToString)
        End Try
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, August 23, 2020 12:52 AM
    Sunday, August 23, 2020 12:49 AM
  • it's something with the environment outside of code or the wrong port which can lead back to the environment in regards to a blocked port from a virus program or firewall rule or proxy setting. 



    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 23, 2020 1:31 AM
  • Hi Les,

    thanks unfortunately that isn't working my end it wont debug and run due to a bit of text that i have been trying to correct in my mainwindow.xaml.vb something to do with the button1.click does not appear to be able to debug.

    Sunday, August 23, 2020 1:37 AM
  • Had sometime today so I took code from one of my GitHub repositories on fluent design patterns which has code for doing email and slimmed it down. I did tested the code and works expected.

    Full source in a .zip file may be found here. I tend not to write WPF code samples as they take a tad longer than windows forms but today had time. 

    There may be a few things to note

    • I used CommandBindings for the button rather than a Click event.
    • A class is used to represent controls on the window which works in tangent with DataContext for the window.
    • The code pattern used is called a builder pattern discussed in a Microsoft TechNet Wiki article I wrote.


    SMTP settings are in app.config where if you try this code sample must change the values from TODO to your setting.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <sectionGroup name="mailSettings">
          <section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
        </sectionGroup>
      </configSections>
      <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
        </startup>
      <mailSettings>
    
        <smtp_1 from="TODO">
          <network
            host="TODO"
            port="TODO"
            enableSsl="true"
            userName=""
            password=""
            defaultCredentials="false" />
    
          <specifiedPickupDirectory pickupDirectoryLocation="MailDrop"/>
    
        </smtp_1>
      </mailSettings>
    </configuration>

    The code shown below is the high level implementation (will not work by itself, there are several classes)

    Class MainWindow
        Public Shared ContinueRoutedCommand As New RoutedCommand()
        Private ReadOnly _emailItem As New EmailItem
        Public Sub New()
            InitializeComponent()
    
    
            CommandBindings.Add(New CommandBinding(
                ContinueRoutedCommand,
                AddressOf CommandBinding_OnExecuted,
                AddressOf CanExecuteHandler))
    
    
            _emailItem.ToAddress = "FakeAddress@comcast.net"
            _emailItem.Subject = "Test subject"
            _emailItem.Body = "<p>Hello <strong>Bob</strong></p>"
    
            DataContext = _emailItem
    
        End Sub
        Private Sub CommandBinding_OnExecuted(
            sender As Object,
            e As ExecutedRoutedEventArgs)
    
            Dim mailer As New MailBuilder()
    
            mailer.CreateMail(GmailConfiguration1).
                WithRecipient(_emailItem.ToAddress).
                WithSubject(_emailItem.Subject).
                AsRichContent().
                WithHtmlView(_emailItem.Body).
                WithTimeout(2000).
                SendMessage()
    
            If Not mailer.HasException Then
                MessageBox.Show("Message has been sent")
            Else
                MessageBox.Show(mailer.LastException.Message)
            End If
    
        End Sub
    
        Private Sub CanExecuteHandler(
            sender As Object,
            e As CanExecuteRoutedEventArgs)
    
            e.CanExecute = True
    
        End Sub
    
    End Class

    Project


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 23, 2020 9:56 PM
  • hi Karen thank you so much for taking so much of your time to help me out. you really wasnt joking when you said its a pain.

    so i have filled in my details in the TODO boxes and getting a failed message from the app, but im guessing that means its working just not sending.

    it feels so close to working now, so what i have tried to get it working from my gmail account is setting my account to allow less secure apps to send as i saw this may help it to send and to obviously allow smtp outgoing mail from my gmail account.

    in regards to the code just to make sure i am 100% filling this in correctly is changin just this part here with the following information

    <smtp_1 from="MY Email address">
          <network
            host="my Host"
            port="my port"
            enableSsl="true"
            userName="My Email Address"
            password="My Email Password"
            defaultCredentials="false" />

    now when i change my host between smtp.google.com and smtp.Gmail.com i get a diffrent fail from both, google.com just fails to send and gmail.com times out. i dont know what is the correct one to be using but im guessing now it is something to do with this part unless i have the smtp_from=wrong.

    again thanks for taking so much time to help.

    Monday, August 24, 2020 1:00 PM
  • Next thing to try is to create a console project and keep it simple. And I can't stress enough it can be environmental e.g. at my job there are rules in my by the organization on which port is allowed and even the from email address, if not sending email will fail immediately. 

    Option Infer On
    
    Imports System.Net.Mail
    Imports System.Net
    
    Namespace ConsoleApplication1
    	Friend Class Program
    		Shared Sub Main(ByVal args() As String)
    			Dim client = New SmtpClient("smtp.gmail.com", 587) With {
    				.Credentials = New NetworkCredential("myusername@gmail.com", "mypwd"),
    				.EnableSsl = True
    			}
    			client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody")
    			Console.WriteLine("Sent")
    			Console.ReadLine()
    		End Sub
    	End Class
    End Namespace
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, August 24, 2020 1:35 PM