Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an e-mail with C# through Gmail

I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:

Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

What can I do to fix this issue?

namespace EmailService
{
    public class Service1 : IService1
    {    
        public string SendEmail(string inputEmail, string subject, string body)
        {
            string returnString = "";
            try
            {
                MailMessage email = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";

                // set up the Gmail server
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;

                // draft the email
                MailAddress fromAddress = new MailAddress("[email protected]");
                email.From = fromAddress;
                email.To.Add(inputEmail);
                email.Subject = body;
                email.Body = body;

                smtp.Send(email);

                returnString = "Success! Please check your e-mail.";
            }
            catch(Exception ex)
            {
                returnString = "Error: " + ex.ToString();
            }
            return returnString;
        }
    }
}
like image 589
Johnny Avatar asked Apr 06 '15 03:04

Johnny


People also ask

What is e mail in C?

e-mail, in full electronic mail, messages transmitted and received by digital computers through a network. An e-mail system allows computer users on a network to send text, graphics, sounds, and animated images to other users.

Can we send email using C++?

Send Outlook Emails using C++Create an object of SmtpClient. Set host, username, password, and port number. Set security options. Send email using SmtpClient->Send() method.

How to send a mail in C program?

Inside the C program using the mail command and system function you can send the mail to the user. Note: The file should be exists. If you want to type the content, yiu can type the content inside the file, then send that file to receiver.

What is the best way to send and receive emails?

Mail servers and clients use SMTP to send and receive mail messages. In C#, we can use System.Net.Mail and Mailkit to send emails. The built-in System.Net.Mail can be used for simple solutions, while Mailkit is better suited for complex tasks.

How do I send an e-mail from the command line?

Run sendmail and pass the e-mail to its standard input (on unix-like systems), or use some SMTP client library to connect to SMTP mail server. Show activity on this post. Use system () to call an existing command-line tool to send mail. Not very portable (requires an external tool with a given calling syntax, etc) but very easy to implement.

How to send an email in Visual Studio Code?

Send Email in C# Step 1. Create a Client Create a Windows Forms project in Visual Studio using C# template. Name it SendEmail, add a... Step 2. Email Database We're going to store our emails in a SQL Server backend database. In your SQL Server, create a... Step 3. Load data from database Select and ...


1 Answers

Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.

Also please go to this link and click on Continue Allow access to your Google account

also I edit it little bit :

public string sendit(string ReciverMail)
{
    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("[email protected]");
    msg.To.Add(ReciverMail);
    msg.Subject = "Hello world! " + DateTime.Now.ToString();
    msg.Body = "hi to you ... :)";
    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = true;
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = new NetworkCredential("[email protected]", "YourPassword");
    client.Timeout = 20000;
    try
    {
       client.Send(msg);
        return "Mail has been successfully sent!";
    }
    catch (Exception ex)
    {
        return "Fail Has error" + ex.Message;
    }
    finally
    {
       msg.Dispose();
    }
}

If the above code don't work , try to change it like the following code :

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("[email protected]", "YourPassword");
like image 130
Eqbal Sohrabi Avatar answered Sep 21 '22 19:09

Eqbal Sohrabi