• 0

Email from WPF C#


Question

i've created an email app within a WPF but the program times out, i've tried it a few ways with different smtp clients and domains with no luck, please could someone shine some light on where i might be going wrong


Title="Email " Height="346" Width="336">
<Grid>
<Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="181,256,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Label Content="To" Height="28" HorizontalAlignment="Left" Margin="24,16,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="From" Height="28" HorizontalAlignment="Left" Margin="24,50,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<Label Content="Subject" Height="28" HorizontalAlignment="Left" Margin="28,89,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,94,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<Label Content="Body" Height="28" HorizontalAlignment="Left" Margin="28,123,0,0" Name="label4" VerticalAlignment="Top" />
<TextBox Height="68" HorizontalAlignment="Left" Margin="71,154,0,0" Name="textBox4" VerticalAlignment="Top" Width="217" />
</Grid>
[/CODE]

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Net.Mail;
namespace Email_test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
System.Net.NetworkCredential auth = new System.Net.NetworkCredential("my@gmail", "mypassword", "gmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = false;
client.UseDefaultCredentials = false;
mail.IsBodyHtml = true;
client.Credentials = auth;

try {
client.Send(mail);
MessageBox.Show("Success" + Environment.NewLine + "Emai sent", "Close", MessageBoxButton.OK);
}
catch
{
MessageBox.Show("Error" + Environment.NewLine + "Email was not sent", "try again", MessageBoxButton.OK);
}
}
}
}
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1128656-email-from-wpf-c/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

i've created an email app within a WPF but the program times out, i've tried it a few ways with different smtp clients and domains with no luck, please could someone shine some light on where i might be going wrong


Title="Email " Height="346" Width="336">
<Grid>
<Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="181,256,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Label Content="To" Height="28" HorizontalAlignment="Left" Margin="24,16,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="From" Height="28" HorizontalAlignment="Left" Margin="24,50,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<Label Content="Subject" Height="28" HorizontalAlignment="Left" Margin="28,89,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,94,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<Label Content="Body" Height="28" HorizontalAlignment="Left" Margin="28,123,0,0" Name="label4" VerticalAlignment="Top" />
<TextBox Height="68" HorizontalAlignment="Left" Margin="71,154,0,0" Name="textBox4" VerticalAlignment="Top" Width="217" />
</Grid>
[/CODE]

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Net.Mail;
namespace Email_test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
[b]System.Net.NetworkCredential auth = new System.Net.NetworkCredential("my@gmail", "mypassword", "gmail.com");[/b]
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = false;
client.UseDefaultCredentials = false;
mail.IsBodyHtml = true;
client.Credentials = auth;

try {
client.Send(mail);
MessageBox.Show("Success" + Environment.NewLine + "Emai sent", "Close", MessageBoxButton.OK);
}
catch
{
MessageBox.Show("Error" + Environment.NewLine + "Email was not sent", "try again", MessageBoxButton.OK);
}
}
}
}
[/CODE]

It looks like one issue is in the text that I bolded above. Do you have an email address at Gmail [[email protected]][email protected][/email] and a password "mypassword"?

EDIT: it won't let me bold it. :( It is the line of code with the "[b]" next to it.

  • 0

yes i do but i changed them for this post, for obvious reasons. i'm not sure if its an error with my code or the domian or smtp client and port details and was wondering if anyone else can get this to work?

My apps send from gmail smtp clients all the time. Mine doesn't contain the domain param. Just auth = new NetworkCredentials("[email protected]", "password"); is all I do.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • You are clueless. The updates are done in the background so the next time you open Edge the updates are applied automatically. There is no need to close all your tabs. Just keep browsing like you normally do. Clearly you don't use Edge and are just one of those haters that complain for the sake of complaining.
    • I don't get this David. Can you explain it please.  
    • Microsoft is busy. Lots of changes to be released imminently for Windows server or soon. Also, lots happening for next version as well. Third party virus scanning software is being moved out of Kernel mode to avoid repeat of Crowdstrike incident. Windows Protected Mode and Windows Ready Print no longer require third party print drivers to be installed. New storage stack being developed. New NVME drivers now available for Windows Server 2025 to improve local NVME drive performance by 60+ percent. NVME-Of of fabric being worked on for next release to improve network access to NVME drives. ReFs (next file system) now has ability to boot and will become default file system in next release of Windows Server. ReFs improves on NTFS in several areas including resiliency and reliability and scalability. New update stack is being worked on to unify Windows updates, and updates for drivers and first party/3rd party application software. A stricter and more robust third-party driver certification program (ODI) is being worked on to improve performance, thermals, battery life, and reliability on modern Windows hardware by tightening how OEMs and IHVs (Intel, AMD, Qualcomm, NVIDIA, etc.) build and ship drivers. There is a tone more but too numerous to mention.
    • Now disable that stupid OneDrive backup request when Windows starts please. So unbelievably frustrating to only have “remind me later” instead of “no and never ask me again”
    • Hello, The Media Creation Tool is still at v10.0.26100.7019 from October 2025. Just looks like the backend has been updated. Regards, Aryeh Goretsky
  • Recent Achievements

    • One Month Later
      Markus94287 earned a badge
      One Month Later
    • Week One Done
      Markus94287 earned a badge
      Week One Done
    • One Year In
      Markus94287 earned a badge
      One Year In
    • Dedicated
      truespursfan earned a badge
      Dedicated
    • Rookie
      restore went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!