Upload email to Sent folder after sending

Some SMTP servers automatically put sent messages in the sent folder, but this is not a SMTP protocol requirement and many servers don’t do that.

In such case, you’ll need to manually upload message to the sent folder using IMAP. Unfortunately there is no standard that would allow checking, if SMTP server puts send messages in Sent folder.

Create email message

First we’ll create new email message:

// C#

IMail email = Limilabs.Mail.Fluent.Mail
    .Html(@"Html with an image: <img src=""cid:lena"" />")
    .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
    .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
    .To("to@example.com")
    .From("from@example.com")
    .Subject("Subject")
    .Create();
' VB.NET

Dim email As IMail = Mail _
    .Html("Html with an image: <img src=""cid:lena"" />") _
    .AddVisual("C:\lena.jpeg").SetContentId("lena") _
    .AddAttachment("C:\tmp.doc").SetFileName("document.doc") _
    .To("to@example.com") _
    .From("from@example.com") _
    .Subject("Subject") _
    .Create()

Send email message

Then we’ll send it using SMTP protocol:

// C#

ISendMessageResult result;
using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");     // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    result = smtp.SendMessage(email);

    smtp.Close();
}
' VB.NET

Dim result As ISendMessageResult
Using smtp As New Smtp()
    smtp.Connect("smtp.server.com")     ' or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password")

    result = smtp.SendMessage(email)

    smtp.Close()
End Using

Upload email message

Finally we’ll connect to IMAP server, get Sent folder and upload message to it:

// C#

if (result.Status == SendMessageStatus.Success)
{
    using (Imap imap = new Imap())
    {
        imap.Connect("imap.server.com");     // or ConnectSSL for SSL
        imap.UseBestLogin("user", "password");

        FolderInfo sent = new CommonFolders(imap.GetFolders()).Sent;
        imap.UploadMessage(sent, email);

        imap.Close();
    }
}
' VB.NET

If result.Status = SendMessageStatus.Success Then
    Using imap As New Imap()
        imap.Connect("imap.server.com")		' or ConnectSSL for SSL
        imap.UseBestLogin("user", "password")

        Dim sent As FolderInfo = New CommonFolders(imap.GetFolders()).Sent
        imap.UploadMessage(sent, email)

        imap.Close()
   End Using
End If

Entire code

Here’s the full code:

// C#

IMail email = Limilabs.Mail.Fluent.Mail
    .Html(@"Html with an image: <img src=""cid:lena"" />")
    .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
    .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
    .To("to@example.com")
    .From("from@example.com")
    .Subject("Subject")
    .Create();

ISendMessageResult result;
using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");    // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    result = smtp.SendMessage(email);

    smtp.Close();
}

if (result.Status == SendMessageStatus.Success)
{
    using (Imap imap = new Imap())
    {
        imap.Connect("imap.server.com");     // or ConnectSSL for SSL
        imap.UseBestLogin("user", "password");

        FolderInfo sent = new CommonFolders(imap.GetFolders()).Sent;
        imap.UploadMessage(sent, email);

        imap.Close();
    }
}

' VB.NET

Dim email As IMail = Mail _
    .Html("Html with an image: <img src=""cid:lena"" />") _
    .AddVisual("C:\lena.jpeg").SetContentId("lena") _
    .AddAttachment("C:\tmp.doc").SetFileName("document.doc") _
    .To("to@example.com") _
    .From("from@example.com") _
    .Subject("Subject") _
    .Create()

Dim result As ISendMessageResult
Using smtp As New Smtp()
    smtp.Connect("smtp.server.com")	' or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password")

    result = smtp.SendMessage(email)

    smtp.Close()
End Using

If result.Status = SendMessageStatus.Success Then
    Using imap As New Imap()
        imap.Connect("imap.server.com")		' or ConnectSSL for SSL
        imap.UseBestLogin("user", "password")

        Dim sent As FolderInfo = New CommonFolders(imap.GetFolders()).Sent
        imap.UploadMessage(sent, email)

    imap.Close()
End Using
End If

Tags: , , ,

Questions?

Consider using our Q&A forum for asking any questions.