Send UWP Email directly via SMTP
This code example shows how to send an email directly from the app in UWP Windows App.
UWP example code
Windows app
C # code to embed.
//------------< send_Email() >------------ //send email with uwp and smtp-server //< email > MailMessage email = new MailMessage(); email.To.Add("Test@Readdy.Net"); email.From= new MailAddress("service@readdy.net", "Email-Server"); email.Subject = sTitle; email.Body = sText; //</ email >
//< email-server > SmtpClient client = new SmtpClient(); client.Host = app_settings._Smtp_Server; client.UseDefaultCredentials = false; client.DeliveryMethod = SmtpDeliveryMethod.Network; //< no ssl > client.Port = 25; client.EnableSsl = false; //</ no ssl > client.Credentials = new NetworkCredential("postmaster@readdy.net", "0123456789"); //</ email-server >
//< send > client.Send(email); //*with error message //</ send >
|
using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using System.Threading.Tasks; //*async using System.Net.Mail; //*smtp client using System.Net; //*Network Credential //using Windows.ApplicationModel.Email; //*email with default app
namespace test_app02_server { /// <summary> /// * send an email with uwp direct over smtp account /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); }
private async void BtnSend_Email_Click(object sender, RoutedEventArgs e) { await send_Email("UWP Email Test", "this is a email from uwp app"); }
public static async Task<bool> send_Email(string sTitle, string sText) { //------------< send_Email() >------------ //send email with uwp and smtp-server //< email > MailMessage email = new MailMessage(); email.To.Add(app_settings._admin_Email); email.From= new MailAddress(app_settings._Smtp_User, "Email-Server"); email.Subject = sTitle; email.Body = sText; //</ email >
//< email-server > SmtpClient client = new SmtpClient(); client.Host = app_settings._Smtp_Server; client.UseDefaultCredentials = false; client.DeliveryMethod = SmtpDeliveryMethod.Network; //< no ssl > client.Port = 25; client.EnableSsl = false; //</ no ssl >
//< ssl > //*securesmpt.t-online.de //client.Port = 587; //client.EnableSsl = true; //</ ssl > client.Credentials = new NetworkCredential(app_settings._Smtp_User, app_settings._Smtp_Password); //</ email-server >
//< send > //await client.SendMailAsync(email); //*no error message client.Send(email); //*with error message //</ send >
return true; //------------</ send_Email() >------------ }
} }
|
Then the email arrives as here