Microsoft provides us functionality to send email using C# code. We can use System.Net and System.Net.Sockets namespace to send email. Basically these namespace is responsible for sending the data and receiving the data over internet. SMTP protocol is used for sending the emails. SMTP stands for Simple Mail Transfer Protocol.
Today, I am going to show, how to create an WPF application to send emails using gmail. Gmail always uses SSL/TLS authentication, so to send email you have to enable SSL property for the port 587 or 465.
I have app.config to store the secure data and fetch when required it.
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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.Configuration;
using System.Net.Mail;
using System.Net;
namespace EmailSendExample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
int flag = 0;
if (txtTo.Text.Trim().Length == 0)
{
flag = 0;
txtbTo.Text = "Required";
txtTo.Focus();
}
else if (!Regex.IsMatch(txtTo.Text, @"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"))
{
flag = 0;
txtbTo.Text = "Invalid";
txtTo.Focus();
}
else
{
flag = 1;
txtbTo.Text = "";
}
if (txtSubject.Text.Trim().Length == 0)
{
flag = 0;
txtbSubject.Text = "Required";
txtSubject.Focus();
}
if (txtContent.Text.Trim().Length == 0)
{
flag = 0;
txtbContent.Text = "Required";
txtContent.Focus();
}
if (flag == 1)
{
var smtpServerName = ConfigurationManager.AppSettings["SmtpServer"];
var port = ConfigurationManager.AppSettings["Port"];
var senderEmailId = ConfigurationManager.AppSettings["SenderEmailId"];
var senderPassword = ConfigurationManager.AppSettings["SenderPassword"];
var smptClient = new SmtpClient(smtpServerName, Convert.ToInt32(port))
{
Credentials = new NetworkCredential(senderEmailId, senderPassword),
EnableSsl = true
};
smptClient.Send(senderEmailId, txtTo.Text.Trim(), txtSubject.Text, txtContent.Text);
MessageBox.Show("Message Sent Successfully");
txtTo.Text = "";
txtSubject.Text = "";
txtContent.Text = "";
txtTo.Focus();
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
txtTo.Text = "";
txtSubject.Text = "";
txtContent.Text = "";
txtTo.Focus();
txtbTo.Text = "";
txtbSubject.Text = "";
txtbContent.Text = "";
}
}
}
MainWindow.xaml
<Window x:Class="EmailSendExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Email Send Example" Height="378.947" Width="559.211">
<Grid Margin="-2,10,-28,-51">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="41*"/>
<ColumnDefinition Width="540*"/>
</Grid.ColumnDefinitions>
<Label Content="Enter details to send email" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="1" Margin="146,0,0,0"/>
<Label Content="To" HorizontalAlignment="Left" Margin="30,38,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
<TextBox HorizontalAlignment="Left" Name="txtTo" Height="23" Margin="67.579,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
<Label Content="Subject" HorizontalAlignment="Left" Margin="30,79,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
<TextBox HorizontalAlignment="Left" x:Name="txtSubject" Height="23" Margin="67.579,82,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
<Label Content="Content" HorizontalAlignment="Left" Margin="30,124,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
<TextBox HorizontalAlignment="Left" x:Name="txtContent" Height="124" Margin="67.579,127,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
<Button Content="Send" HorizontalAlignment="Left" Margin="91.579,276,0,0" VerticalAlignment="Top" Width="75" Name="btnSend" Click="btnSend_Click" Grid.Column="1"/>
<Button Content="Reset" HorizontalAlignment="Left" Margin="191.579,276,0,0" VerticalAlignment="Top" Width="75" Name="btnReset" Click="btnReset_Click" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Name="txtbTo" Foreground="red" Margin="422.579,43,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" x:Name="txtbSubject" Foreground="red" Margin="422.579,84,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" x:Name="txtbContent" Foreground="red" Margin="422.579,137,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>
</Grid>
</Window>
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="SmtpServer" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="SenderEmailId" value="xyz@gmail.com"/>
<add key="SenderPassword" value="xxxxxx"/>
</appSettings>
</configuration>
Conclusion:
So, today we learned how to create a WPF application and using this how to send email. We also learned how to validate a field if required.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks
Posted Comments :
lakshmi Posted : 8 Years Ago
In the above program i have one error about the Configuration Manager. Please help me sir
Mukesh Kumar Author Posted : 8 Years Ago
Kindly send the exact error which you are facing.
AMS Posted : 8 Years Ago
@lakshmi You need to add a Reference for System Configuration
Ram Posted : 8 Years Ago
Turn on Less Secure App setting in your gmail account
santhosh Posted : 8 Years Ago
Hi i have error in this line smptClient.Send(senderEmailId, txtTo.Text.Trim(), txtSubject.Text, txtContent.Text); error is (Argument Null Exception occurred,value cannot be null) but i fill all the fields To,Sub and Content but this error will be occurred
Prakash Posted : 6 Years Ago
Thanks, it is complete code, only you need to put send section in try catch block.
Sanjay Singh Posted : 5 Years Ago
Thanks dear, it is working properly
Priyanka Posted : 4 Years Ago
ujio
Priyanka Posted : 4 Years Ago
ujio
asdf Posted : 3 Years Ago
were can i find the app.config file? Im working with Visual Studio 2019
xys Posted : 3 Years Ago
fdg
xys Posted : 3 Years Ago
fdg
xys Posted : 3 Years Ago
fdg
Martin Dodd Posted : 8 Years Ago
Here is my code: Using mail As MailMessage = New MailMessage(New MailAddress(senderAddress), New MailAddress("fred@gmail.com", "Fred Bob")) mail.To.Add(New MailAddress("bob@gmail.com", "Bob")) mail.Subject = subjectMatter mail.Body = Message Using client As SmtpClient = New SmtpClient() client.Host = "smtp.gmail.com" client.Port = 587 client.Credentials = New NetworkCredential(senderAddress, senderPassword) client.Timeout = 3000000 client.EnableSsl = True client.Send(mail) End Using End Using This code is still failing. I currently receiving the error: The server response was: 5.5.1 Authentication Required. I have tried setting up app Password in gmail, but this doesn't work either. Any ideas