article

C# SMTP Mail Console

Email
Submitted on: 1/1/2015 1:37:00 AM
By: Peter V. (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C#
Views: 1451
 
     Send Mail by C# in console. Simple view use of a class , properties (get/set) you can test it by using .NET SDK framework compile it with the csc command in console c:> csc ClsMail.cs after compiling it will give you the exe file

 
				
using System;
using System.Web.Mail;
namespace myMail
{
	/// <summary>
	/// Summary description for ClsStartup.
	/// </summary>
	public class ClsMail
	{
		private string mTxtEmailTo; // Emailaddres for user to send
		private string mTxtEmailFrom;// Emailaddres for user from 
		private string mTxtMailSubj;
		private string mTxtMailBody;
		private string mTxtServerName;
		public ClsMail()
		{
			Console.WriteLine ("Console SMTP Mail V0.1");
			Console.WriteLine ("----------------------");
			mTxtEmailFrom = "";
			mTxtEmailTo = "";
		}
		public string SmtpServer
		{
			set
			{
				mTxtServerName = value;
				SmtpMail.SmtpServer = value;
			}
			get
			{
				return mTxtServerName;
			}
		}
		public string EmailFrom 
		{
			set
			{
				mTxtEmailFrom = value;
			}
			get
			{
				return mTxtEmailFrom;
			}
		}
		public string EmailTo
		{
			set
			{
				mTxtEmailTo = value;
			}
			get
			{
				return mTxtEmailTo;
			}
		}
		public string MailBody
		{
			set
			{
				mTxtMailBody = value;
			}
			get
			{
				return mTxtMailBody;
			}
		}
		public string MailSubject
		{
			set
			{
				mTxtMailSubj = value;
			}
			get
			{
				return mTxtMailSubj;
			}
		}
		public bool SendNow()
		{
			try
			{
				Console.WriteLine();
				Console.WriteLine("Connecting to : {0}" , mTxtServerName);
				SmtpMail.Send (mTxtEmailFrom,mTxtEmailTo,mTxtMailSubj,
											 mTxtMailBody);
			 return true;
			}
			catch (Exception e)
			{
				Console.WriteLine("Error Mail Sending : {0}" , e.Message );
				return false;
			}
		}
		/// <summary>
		/// Startup Method
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			/// Header MainApplication
		ClsMail Smtp = new ClsMail();
		Console.Write("Smtp server : ");
		Smtp.SmtpServer = Console.ReadLine();
		Console.Write("Your Emailaddr : ");
		Smtp.EmailFrom = Console.ReadLine();
		Console.Write("Email To Send : ");
		Smtp.EmailTo = Console.ReadLine();
		Console.Write("Subject Mail : ");
		Smtp.MailSubject = Console.ReadLine();
		Console.Write("Body : ");
		Smtp.MailBody = Console.ReadLine();
			if (Smtp.SendNow())
			{
				Console.WriteLine("End Session : Succesfully");
			}
			else
			{
				Console.WriteLine("End Session : Failed");
			}
		Console.WriteLine("Press any Key");
		Console.Read();
		}
			
	}
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.