Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I send email in a simple application with C#?

What I have tried:

i need sample code to send email with c# language
Posted
Updated 24-Feb-22 4:19am
v2

I wrote an article covering the entire concept of sending the emails in .NET framework using C# language. You can read about it here, Sending emails over .NET framework, and general problems – using C# code[^].

I also included a few of the errors and problems that you might encounter while programming the application for sending the emails. General code for sending the emails in C# is:
C#
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");
   // client.UseDefaultCredentials = true;

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);

   /* 
    * Since I was using Console app, that is why I am able to use the Console
    * object, your framework would have different ones. 
    * There is actually no need for these following lines, you can ignore them
    * if you want to. SMTP protocol would still send the email of yours. */
    
   // Print a notification message
   Console.WriteLine("Email has been sent.");
   // Just for the sake of pausing the application
   Console.Read();
}

You can use this as a helper function or so. Update the values and if everything goes fine, the code would work!
 
Share this answer
 
Comments
bejos3519 8-Sep-16 1:37am    
I work at ABC Company . and I was assigned to make an application to send an email to employees .
i use
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

when I run, it does work well . but the email server that I would use is the domain of companies that Rudi_Bastian@abc.com
How do I get my company SmtpClient it ?

may be the master here can help me .
Afzaal Ahmad Zeeshan 8-Sep-16 13:59pm    
Basically, SmtpClient object doesn't care about the server being used, address being used. It just requires the following:

1) SMTP server location; host name. Port to connect at. Typically SMTP communication is done on port 25 (or TCP protocol), you can use other for secure communication — 587.

If that is the case, you can ask the company to provide you with the SMTP host name for your company, it would be something like, "smtp.abc.com", connect to it.

Then, you would be passing your own credentials. Such as, "Rudi_Rastian@abc.com" and the password of your account. Once authenticated, the same code would be used to send the emails from your company account.
 
Share this answer
 
Comments
bejos3519 4-Sep-16 8:17am    
i got the message error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at.
and then i also got email from gmail that related security system.
so, what can i do for it. please help me :'(
Garth J Lancaster 4-Sep-16 8:34am    
this might be one thing to check https://support.google.com/accounts/answer/6010255?hl=en
bejos3519 4-Sep-16 8:50am    
thanks friend...
that is usefull
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("xxxxx@gmail.com");
                mail.To.Add("yyyyyy@yahoo.com");
                mail.Subject = "Halo Boss";
                mail.Body = "Halo Boss, Super";
                mail.Priority = MailPriority.Normal;

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("D:\\Default.aspx");
                mail.Attachments.Add(attachment);
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("xxxxxx@gmail.com", "1234");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                Console.Write("Email Terkirim");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}



i try do like above. but when i run, i got the message error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at.
and then i also got email from gmail that related security system.
so, what can i do for it. please help me :'(
 
Share this answer
 
v2
Comments
charles henington 24-Feb-22 18:45pm    
I notice that the mail.From is a gmail address. If you're using gmail to send the message you will need to give your gmail account permission to use less secure apps

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