0

i've tried to send email using this code..but an error occurred in smtp.Send(mail); messaging "Failure sending mail"

  MailMessage mail = new MailMessage();
  // set the addresses
  mail.From = new MailAddress("from@gmail.com");

  mail.To.Add(new MailAddress("to@yahoo.com"));

  // set the content
  mail.Subject = "test sample";
  mail.Body = @"thank you";
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");

  smtp.Credentials = new NetworkCredential("from@gmail.com", "password"); 
  smtp.Send(mail);
Arion
  • 31,011
  • 10
  • 70
  • 88
blessie
  • 11
  • 1
  • 2
    You need to supply more detail from the error - is an exception thrown? what's the full exception string trace? –  May 04 '12 at 06:57
  • Are you using a real email address for the From and To? What this code will do is go off to gmail and place your mail on the smtp queue, but before it does it'll check that your sending valid mail. Which means your addresses need to be legit – JonVD May 04 '12 at 06:57
  • are you missing mail.Host? and mail.Port? – Imran Rizvi May 04 '12 at 06:59
  • If you get an exception, also inspect inner exceptions - for mail that's where the real info is. – Hans Kesting May 04 '12 at 07:14

7 Answers7

1

In your code specify port number:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)

Also check out this thread Sending email through Gmail SMTP server with C#

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
0

You need to set smtp.EnableSsl = true for gmail.

Take a look at this class, it should work for you:

public class Email
{
    NetworkCredential credentials;
    MailAddress sender;

    public Email(NetworkCredential credentials, MailAddress sender)
    {
        this.credentials = credentials;
        this.sender = sender;
    }

    public bool EnableSsl
    {
        get { return _EnableSsl; }
        set { _EnableSsl = value; }
    }
    bool _EnableSsl = true;

    public string Host
    {
        get { return _Host; }
        set { _Host = value; }
    }
    string _Host = "smtp.gmail.com";

    public int Port
    {
        get { return _Port; }
        set { _Port = value; }
    }
    int _Port = 587;

    public void Send(MailAddress recipient, string subject, string body, Action<MailMessage> action, params FileInfo[] attachments)
    {
        SmtpClient smtpClient = new SmtpClient();

        // setup up the host, increase the timeout to 5 minutes
        smtpClient.Host = Host;
        smtpClient.Port = Port;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = credentials;
        smtpClient.Timeout = (60 * 5 * 1000);
        smtpClient.EnableSsl = EnableSsl;

        using (var message = new MailMessage(sender, recipient)
        {
            Subject = subject,
            Body = body
        })
        {
            foreach (var file in attachments)
                if (file.Exists)
                    message.Attachments.Add(new Attachment(file.FullName));
            if(null != action)
                action(message);
            smtpClient.Send(message);
        }
    }
}
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
0

fill mail.Host and mail.Port

Properties with proper values

Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
0

You should be using the using statement when creating a new MailMessage, plus a few things you missed out like port number and enableSSL

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("from@gmail.com");
    mail.To.Add(new MailAddress("to@yahoo.com"));
    mail.Subject = "test sample";
    mail.Body = @"thank you";

    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
    smtpServer.Port = 587;
    smtpServer.Credentials = new NetworkCredential("from@gmail.com", "password"); 
    smtpServer.EnableSsl = true;
    smtpServer.Send(mail);
}
c0deNinja
  • 3,956
  • 1
  • 29
  • 45
0

Here's a basic GMAIL smtp email implementation I wrote a while ago:

public static bool SendGmail(string subject, string content, string[] recipients, string from)
{
    bool success = recipients != null && recipients.Length > 0;

    if (success)
    {
        SmtpClient gmailClient = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential("******", "*****") //you need to add some valid gmail account credentials to authenticate with gmails SMTP server.
        };


        using (MailMessage gMessage = new MailMessage(from, recipients[0], subject, content))
        {
            for (int i = 1; i < recipients.Length; i++)
                gMessage.To.Add(recipients[i]);

            try
            {
                gmailClient.Send(gMessage);
                success = true;
            }
            catch (Exception) { success = false; }
        }
    }
    return success;
}

It should work fine for you, but you'll need to add a valid gmail acocunt where I've marked in the code.

Jason Larke
  • 5,289
  • 25
  • 28
0

This is the function which i checked to send mail...and it's working properly.

`

        private static bool testsendemail(MailMessage message)
        {

            try

            {

            MailMessage message1 = new MailMessage();

            SmtpClient smtpClient = new SmtpClient();

            string msg = string.Empty;

            MailAddress fromAddress = new MailAddress("FromMail@Test.com");
            message1.From = fromAddress;
            message1.To.Add("ToMail@Test1.com");
            message1.Subject = "This is Test mail";
            message1.IsBodyHtml = true;
            message1.Body ="You can write your body here"+message;
            smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
            smtpClient.Port = 587;
            smtpClient.EnableSsl = false;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new  System.Net.NetworkCredential("SenderMail@yahoo.com", "YourPassword");

            smtpClient.Send(message1);
        }
        catch
        {
            return false;
        }
        return true;

    }`           

Thank You.

Chetan S
  • 935
  • 1
  • 11
  • 21
0

Following is the C# code for Gmail Service

using System;
using System.Net;
using System.Net.Mail;

namespace EmailApp
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            String SendMailFrom = "Sender Email";
            String SendMailTo = "Reciever Email";
            String SendMailSubject = "Email Subject";
            String SendMailBody = "Email Body";

            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587);
                SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage email = new MailMessage();
                // START
                email.From = new MailAddress(SendMailFrom);
                email.To.Add(SendMailTo);
                email.CC.Add(SendMailFrom);
                email.Subject = SendMailSubject;
                email.Body = SendMailBody;
                //END
                SmtpServer.Timeout = 5000;
                SmtpServer.EnableSsl = true;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new NetworkCredential(SendMailFrom, "Google App Password");
                SmtpServer.Send(email);

                Console.WriteLine("Email Successfully Sent");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }

        }
    }
}

For reference: https://www.techaeblogs.live/2022/06/how-to-send-email-using-gmail.html