Send Email with Attachment in C#, VB.NET

2017-06-29 07:19:11 Written by  support iceblue
Rate this item
(0 votes)

This article illustrates how to create an email message with attachment and send it using Spire.Email component in C# and VB.NET.

Detail steps:

Step 1: Declare and assign three MailAddress objects.

MailAddress addressFrom = "Alice.yang@e-iceblue.com";
MailAddress addressTo = "leondavisld@outlook.com";
MailAddress adressCC = "Shawn_Smithhh@outlook.com";

Step 2: Create an email message.

MailMessage message = new MailMessage(addressFrom, addressTo);

Step 3: Set subject, text body and creation time for the message.

message.Subject = "Spire.Email Component";
message.BodyText = "Hi!\r\n"+
                "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform.";
message.Date = DateTime.Now;

Step 4: Add an attachment and the second receiver to cc.

message.Attachments.Add(new Attachment("Hydrangeas.jpg"));
message.Cc.Add(adressCC.Address);

Step 5: Create a SmtpClient instance and send the email message.

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.outlook.com";
smtp.ConnectionProtocols = ConnectionProtocols.Ssl;
smtp.Username = addressFrom.Address;
smtp.Password = "password";
smtp.Port = 587;
smtp.SendOne(message);

Screenshot:

Send Email with Attachment in C#, VB.NET

Full code:

[C#]
using System;
using System.Globalization;
using Spire.Email;
using Spire.Email.IMap;
using Spire.Email.Smtp;

namespace Send_Email
{
    class Program
    {
        static void Main(string[] args)
        {
            MailAddress addressFrom = "Alice.yang@e-iceblue.com";
            MailAddress addressTo = "LeonDavisLD@outlook.com";
            MailAddress adressCC = "Shawn_Smithhh@outlook.com";

            MailMessage message = new MailMessage(addressFrom, addressTo);            

            message.Subject = "Spire.Email Component";
            message.BodyText = "Hi!\r\n"+
                "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform.";
            message.Date = DateTime.Now;

            message.Attachments.Add(new Attachment("Hydrangeas.jpg"));
            message.Cc.Add(adressCC.Address);
            
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.outlook.com";
            smtp.ConnectionProtocols = ConnectionProtocols.Ssl;
            smtp.Username = addressFrom.Address;
            smtp.Password = "password";
            smtp.Port = 587;

            Console.WriteLine("From   : " + message.From.ToString());
            Console.WriteLine("To     : " + message.To.ToString());
            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
            Console.WriteLine("Subject: " + message.Subject);
            Console.WriteLine("Attachment: " + message.Attachments.Count);
            Console.WriteLine("------------------- BODY -----------------");
            Console.WriteLine(message.BodyText);            
            Console.WriteLine("------------------- END ------------------");
            smtp.SendOne(message);

            Console.WriteLine("Message Sent.");
            Console.ReadLine();    
        }
    }
}
[VB.NET]
Imports System.Globalization
Imports Spire.Email
Imports Spire.Email.IMap
Imports Spire.Email.Smtp

Namespace Send_Email
	Class Program
		Private Shared Sub Main(args As String())
			Dim addressFrom As MailAddress = "Alice.yang@e-iceblue.com"
			Dim addressTo As MailAddress = "LeonDavisLD@outlook.com"
			Dim adressCC As MailAddress = "Shawn_Smithhh@outlook.com"

			Dim message As New MailMessage(addressFrom, addressTo)

			message.Subject = "Spire.Email Component"
			message.BodyText = "Hi!" & vbCr & vbLf + "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform."
			message.[Date] = DateTime.Now

			message.Attachments.Add(New Attachment("Hydrangeas.jpg"))
			message.Cc.Add(adressCC.Address)

			Dim smtp As New SmtpClient()
			smtp.Host = "smtp.outlook.com"
			smtp.ConnectionProtocols = ConnectionProtocols.Ssl
			smtp.Username = addressFrom.Address
			smtp.Password = "password"
			smtp.Port = 587

			Console.WriteLine("From   : " + message.From.ToString())
			Console.WriteLine("To     : " + message.To.ToString())
			Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture))
			Console.WriteLine("Subject: " + message.Subject)
			Console.WriteLine("Attachment: " + message.Attachments.Count)
			Console.WriteLine("------------------- BODY -----------------")
			Console.WriteLine(message.BodyText)
			Console.WriteLine("------------------- END ------------------")
			smtp.SendOne(message)

			Console.WriteLine("Message Sent.")
			Console.ReadLine()
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Wednesday, 15 September 2021 03:30