Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Email .NET
>
Tutorial
>Send message with attachments
This example shows you how to add attachments to a message and send it.
C# example
using System; using Independentsoft.Email; using Independentsoft.Email.Smtp; using Independentsoft.Email.Mime; namespace Sample { class Program { static void Main(string[] args) { Message message = new Message(); message.From = new Mailbox("example@hotmail.com", "Name"); message.To.Add(new Mailbox("example@t-online.de")); message.Subject = "Test"; BodyPart textBodyPart = new BodyPart(); textBodyPart.ContentType = new ContentType("text", "plain", "utf-8"); textBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable; textBodyPart.Body = "This message has two attachments."; Attachment attachment1 = new Attachment("c:\\testfolder\\test.docx"); attachment1.ContentType = new ContentType("application", "ms-word"); Attachment attachment2 = new Attachment("c:\\testfolder\\image.jpg"); attachment1.ContentType = new ContentType("image", "jpg"); message.BodyParts.Add(textBodyPart); message.BodyParts.Add(attachment1); message.BodyParts.Add(attachment2); SmtpClient client = new SmtpClient("smtp.live.com"); client.Connect(); client.StartTls(); client.Login("example@hotmail.com", "password"); client.Send(message); client.Disconnect(); } } }
VB example
Imports System Imports Independentsoft.Email Imports Independentsoft.Email.Smtp Imports Independentsoft.Email.Mime Namespace Sample Class Program Shared Sub Main(ByVal args As String()) Dim message As New Message() message.From = New Mailbox("example@hotmail.com", "Name") message.[To].Add(New Mailbox("example@t-online.de")) message.Subject = "Test" Dim textBodyPart As New BodyPart() textBodyPart.ContentType = New ContentType("text", "plain", "utf-8") textBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable textBodyPart.Body = "This message has two attachments." Dim attachment1 As New Attachment("c:\testfolder\test.docx") attachment1.ContentType = New ContentType("application", "ms-word") Dim attachment2 As New Attachment("c:\testfolder\image.jpg") attachment1.ContentType = New ContentType("image", "jpg") message.BodyParts.Add(textBodyPart) message.BodyParts.Add(attachment1) message.BodyParts.Add(attachment2) Dim client As New SmtpClient("smtp.live.com") client.Connect() client.StartTls() client.Login("example@hotmail.com", "password") client.Send(message) client.Disconnect() End Sub End Class End Namespace