Sending mail using mailkit

  • Thread starter Thread starter Yerkhan
  • Start date Start date
Y

Yerkhan

Guest
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

Continue reading...
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
J
Replies
0
Views
76
Jackson_1990
J
Back
Top