My semi working email code in WPF app vb

  • Thread starter Thread starter Dazzz12345
  • Start date Start date
D

Dazzz12345

Guest
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.

Continue reading...
 
Back
Top