popovIliya

Email client from console

Feb 20th, 2021 (edited)
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. //***************************************************************************
  2. // Author Iliya Popov 20.02.2021                                           //
  3. //***************************************************************************
  4.  
  5. using System;
  6. using System.Net;
  7. using System.Net.Mail;
  8.  
  9. namespace EmailClient
  10. {
  11.     class EmailClient
  12.     {
  13.         private const string emServer = "mail.server";
  14.         private const byte emPort = "server port";// default 25
  15.         private const string fromEmail = "[email protected]";
  16.         private const string passEmailAccount = "your password";
  17.        
  18.         static void Main(string[] args)
  19.         {
  20.             string toEml, subject, contentTxt;
  21.             do
  22.             {
  23.                 Console.Write("Send email to: ");
  24.                 toEml = Console.ReadLine();
  25.                 Console.Write("Subject: ");
  26.                 subject = Console.ReadLine();
  27.                 Console.WriteLine("Text: ");
  28.                 contentTxt = Console.ReadLine();
  29.             }while (sendEmail(toEml, subject, contentTxt));
  30.             Console.ReadKey();
  31.         }
  32.         private static Boolean sendEmail(string toTxt, string subjectTxt, string bodyTxt)
  33.         {
  34.             SmtpClient smClient = new SmtpClient(emServer, emPort);
  35.             smClient.Credentials = new NetworkCredential(fromEmail, passEmailAccount);
  36.             MailMessage msg = new MailMessage();
  37.             msg.From = new MailAddress(fromEmail);
  38.             msg.To.Add(new MailAddress(toTxt));
  39.             msg.Subject = subjectTxt;
  40.             msg.Body = bodyTxt;
  41.             try
  42.             {
  43.                 smClient.Send(msg);
  44.                 Console.WriteLine("message sent successfully!");
  45.                 return false;
  46.             }
  47.             catch (SmtpException ex)
  48.             {
  49.                Console.WriteLine("Error send msg, {0}", ex);
  50.                return true;
  51.             }
  52.             finally
  53.             {
  54.                 smClient.Dispose();
  55.             }
  56.         }
  57.     }
  58. }
  59.  
Add Comment
Please, Sign In to add comment