ilhan DEMİRTEPE
ParamTech
Published in
4 min readSep 11, 2020

--

What is the purpose of the SMTP

In this article I’ll try to explain how we can send emails using SMTP servers.

  • Protocol
    Protocol is a group of rules which is used by applications that communicate each other from network.
  • SMTP — Simple Mail Transfer Protocol.
    Is a protocol used to transfer emails.First RFC was published in August 1982.We can say that when we compose a message in an outlook application and send it, the outlook application sends this email to mail server by using the smtp protocol.

c# import and export excel ,send email using smtp-pulse

ALL THE CODE
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<User> userList = new List<User>();
userList.Add(new User {
Id=1,
Name="ilhan",
Surname="Demirtepe"
});
userList.Add(new User
{
Id = 2,
Name = "Akif",
Surname = "Demirtepe"
});
userList.Add(new User
{
Id = 3,
Name = "Mehmet",
Surname = "Demirtepe"
});
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add("Worksheet1");
var worksheet = excel.Workbook.Worksheets["Worksheet1"];
worksheet.Cells[1, 1].Value = "Id";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Surname";
int recordIndex = 2;
foreach (var article in userList)
{
worksheet.Cells[recordIndex, 1].Value = article.Id;
worksheet.Cells[recordIndex, 2].Value = article.Name;
worksheet.Cells[recordIndex, 3].Value = article.Surname;
recordIndex++;
}
// AutoFit() method here.
worksheet.Column(1).AutoFit();
worksheet.Column(2).AutoFit();
worksheet.Column(3).AutoFit();
string root = Path.Combine("Content");
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
string subdir = Path.Combine("Content/" + "Customer.xlsx");
FileInfo excelFile = new FileInfo(subdir);
excel.SaveAs(excelFile);
FileStream streamTemp = System.IO.File.Open(subdir, FileMode.Open, FileAccess.Read);
byte[] m_Bytes = ReadToEnd(streamTemp);
Attachment attachment = WrapExcelBytesInAttachment(m_Bytes);
SendEmail("fromemailadres", "DisplayName", "Subject","ilhandemirtepe@gmail.com", attachment);
}
}
static Attachment WrapExcelBytesInAttachment(byte[] excelContent)
{
try
{
string subdir = Path.Combine("Content/" + "Customer.xlsx");
Stream stream = new MemoryStream(excelContent);
Attachment attachment = new Attachment(stream, subdir);
attachment.ContentType = new ContentType("application/vnd.ms-excel");
return attachment;
}
catch (Exception ex)
{
return null;
}
}
static bool SendEmail(string fromEmailAddress, string fromDisplayName, string fromSubject, string toEmailAddress, Attachment message)
{
bool result = false;
try
{
MailMessage ePosta = new MailMessage();
ePosta.From = new MailAddress(fromEmailAddress, fromDisplayName);
ePosta.To.Add(toEmailAddress);
ePosta.Subject = fromSubject;
ePosta.Body = "deneme";
ePosta.IsBodyHtml = true;
int port = 587; ///server email port
string host = "smtp-pulse.com";
string emailaddress = "xyz@abc.com"; ///server email adress
string password = "123456"; ///server email password
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(emailaddress, password);
smtp.Port = port;
smtp.Host = host;
smtp.EnableSsl = false;
try
{
ePosta.IsBodyHtml = true;
if (message != null)
{
ePosta.Attachments.Add(message);
}
smtp.Send(ePosta);
result = true;
}
catch (SmtpException ex)
{
throw new Exception(ex.Message);
}
}
catch (Exception)
{
return result;
}
return result;
}
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = 0;
if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
}
}
}
}

--

--