Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was writing codes for my home email client which does not bind me to open gmail account every time when I am supposed to send the email.
Below is source codes:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;

namespace Emailclient 
{
    class Program
    {
        static void Main(string[] args)
        {
            string sender, receipents;
            MailMessage msgobj = new MailMessage();
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
           // client.Host="www.yahoo.com";
            //client.Port = 587;
            client.EnableSsl = true;
            Console.WriteLine(".................Email Client Version 1.0.0.0.........");
            Console.Write(".................Printing the Service point protocol version........");
            Console.Write(client.ServicePoint.ProtocolVersion.ToString());
            if (client.Credentials == null)
            {
                //Console.WriteLine("..............Credentials are set to null............");
                // username and password Is set as ceredntial for connecting to smtp accesss to gmail account
                client.Credentials = new System.Net.NetworkCredential("**your email id", "password");

            }
            Console.WriteLine("..............Enter your email address here and press Enter..........");
            try
            {
                // sender's emaill address .......
               // sender = Console.ReadLine();
               msgobj.From = new MailAddress("niraj.1991@gmail.com");
               Console.ForegroundColor = ConsoleColor.Red;
               Console.BackgroundColor = ConsoleColor.White;
               Console.WriteLine("Sending mail on the behalf of niraj.1991@gmail.com");
            }
            catch (FormatException fe)
            {
                Console.WriteLine("Following Error Ocured.......\n" + fe.Message);
                Console.Clear();
                Console.WriteLine("..............Enter your email address here and press Enter..........");
               sender= Console.ReadLine();
               msgobj.From = new MailAddress(sender);
            }
            Console.WriteLine("Enter your friend's email address here and press Enter..........");
            try 
            {
                // emaill addresses of recepients goes here........
                Console.ForegroundColor = ConsoleColor.Gray;

                Console.BackgroundColor = ConsoleColor.DarkCyan;
                receipents = Console.ReadLine();
               msgobj.To.Add ( receipents);
            }
            catch (FormatException fe2)
            {
                Console.WriteLine("Following Error Ocured.......\n" + fe2.Message);
                Console.Clear();
                Console.WriteLine("Enter your friend's email address here and press Enter..........");
                Console.ForegroundColor=ConsoleColor.Red;
                Console.WriteLine("Following Error Ocured.......\n" + fe2.Message);
                Console.Read();
               // Console.WriteLine("Press esc key to exit");
                
            }
            
            Console.WriteLine(".........Enter subject of the message.......");
            msgobj.Subject = Console.ReadLine().ToString();
           // msgobj.Attachments =new AttachmentCollection("C:\\Documents and Settings\\NIRAJ KUMAR CHAUHAN\\Desktop\\exception");
           // msgobj.BodyEncoding=System.Text.Encoding.ASCII;
            msgobj.IsBodyHtml=true;
            //TransferEncoding.Base64;
       
           msgobj.Priority=MailPriority.High;
            Console.WriteLine("........Enter your message body here...........");
            
            msgobj.Body = Console.Read().ToString();
            Console.WriteLine("..........Sending message to " + msgobj.To.ToString()+"..........");
            try
            {

                client.Send(msgobj);
                            
            }
            catch (SmtpException e)
            {
                Console.WriteLine(" ......Serious Error occured " + e.Message);
                Console.Read();
            }
            
            /*
                Console.WriteLine("Message send successfulluy.....");
            else
                Console.WriteLine("Mesaage sending failed......");*/
           
            Console.Read();
        }

      
    }
}

Please work on above?
I worked email got send but there is slight problem which I couldn't solve out.
The problem is email body got encoded into numeric formats.
I don't understand what to do with this problem.
Posted
Updated 21-Sep-11 5:17am
v2

Add this line
C#
msgobj.IsBodyHtml = true;
 
Share this answer
 
I have tried the following but not got my problem solved
C#
msgobj.IsBodyHtml=true;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900