Click here to Skip to main content
15,936,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai, i am a freelancer trying to make a c# code for sending a email. I have coded as shown below.
But once i run my code, it shows that email sending has been failed because of not implementing 502 command.It would be kindful of you if you could correct my code below.Please help me.god bless you
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EASendMail; 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            // Set sender email address, please change it to yours
            oMail.From = "faizel.shk@gmail.com";

            // Set recipient email address, please change it to yours
            oMail.To = "sumith.mani27@gmail.com";

            // Set email subject
            oMail.Subject = "test email from c# project";

            // Set email body
            oMail.TextBody = "this is a test email sent from c# project, do not reply";

            // Your SMTP server address
            SmtpServer oServer = new SmtpServer("smtp.gmail.com");

            // User and password for ESMTP authentication, if your server doesn't require
            // User authentication, please remove the following codes.            
            oServer.User = "faizel.shk";
            oServer.Password = "***********";

            // If your smtp server requires SSL connection, please add this line
             oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            try
            {
                Console.WriteLine("start to send email ...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            }
            Console.Read();
        }
    }
}
Posted
Updated 14-Mar-13 23:49pm
v3
Comments
Karthik Harve 15-Mar-13 5:50am    
[Edit] pre tags added.

C#
try
       {
              string myEmailAddress = "A*********@gmail.com";
              string mypwd = "***************";
              string toEmailAddress = "xyz@abc.com";
              string subject = "Try to send mail";
              string mailbody = "this is try mail body";

           var client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
           {
                  Credentials = new System.Net.NetworkCredential(myEmailAddress, mypwd),
                  EnableSsl = true
            };
           client.Send(myEmailAddress, toEmailAddress, subject, mailbody);


      }


       catch (Exception e1)
       {

         }



Try it ...
 
Share this answer
 
Or SmtpServer oServer = new SmtpServer("smtp.gmail.com");

Change to this line

SmtpServer oServer = new SmtpServer("smtp.gmail.com", 587);
 
Share this answer
 
Comments
Nelek 15-Mar-13 13:03pm    
If you need to add or change content to your question or a previous solution, please use the "improve question / solution" instead of writing a lot of messages. It will keep readability and help other users with similar problems to find it faster.

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