none
Sending mail using mailkit RRS feed

  • Question

  • Hi everyone.

    I've trying to send email using C# console application with NET core 2.1 and using Mailkit.

    Here is my code:

    using System;
    using MailKit.Net.Smtp;
    using MimeKit;
    namespace mailkit_core
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    //From Address
                    string FromAddress = "[Email from]@gmail.com";
                    string FromAdressTitle = "Email from";
                    //To Address
                    string ToAddress = "[mail_to]@gmail.com";
                    string ToAdressTitle = "mail to";
                    string Subject = "title";
                    string BodyContent = "body";
    
                    //Smtp Server
                    string SmtpServer = "smtp.gmail.com";
                    //Smtp Port Number
                    int SmtpPortNumber = 587;
    
                    var mimeMessage = new MimeMessage();
                    mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
                    mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
                    mimeMessage.Subject = Subject;
                    mimeMessage.Body = new TextPart("plain")
                    {
                        Text = BodyContent
    
                    };
    
                    using (var client = new SmtpClient())
                    {
                        Console.WriteLine("connection initiated");
                        client.Connect(SmtpServer, SmtpPortNumber, false);
                        Console.WriteLine("connection succeded");
                        
                        client.Authenticate("[msil_from]@gmail.com", "[pass]");
                        Console.WriteLine("start sending");
                        client.Send(mimeMessage);
                        Console.WriteLine("The mail has been sent successfully !!");
                        Console.ReadLine();
                        client.Disconnect(true);
    
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
    

    However, I can't connect to smtp server. What can be an issue? 

    I enabled connection of not trusted applications in google account.

    Regards,

    Yerkhan

    • Moved by CoolDadTx Monday, September 9, 2019 1:42 PM Third party product
    Monday, September 9, 2019 12:30 PM

All replies

  • This forum is for C#-specific questions only. Please post questions related to using third party libraries in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, September 9, 2019 1:42 PM