MailBee.NET Objects 4.0

Imap.UploadMessage Method (MailMessage, String, SystemMessageFlags)

Uploads a mail message to the specified folder and assigns the specified flags to this message.

public bool UploadMessage(
   MailMessage msg,
   string folderName,
   SystemMessageFlags systemFlags
);

Parameters

msg
A reference to the MailMessage object representing the message to be uploaded.
folderName
The full name of the folder to upload the message to.
systemFlags
A set of flags to be assigned to the message.

Return Value

true if the message was uploaded successfully; otherwise, false.

Remarks

This method implements APPEND command of the IMAP4 protocol. The uploaded message will have the specified flags assigned + "\Recent" flag which is always added by the server for uploaded messages. The date of receiving the message by the server (INTERNALDATE in IMAP4 terms) will be set to the current datetime/timezone of the local computer (not the mail server).

To upload a file, the developer can first load it into MailMessage object using LoadMessage method, and then call UploadMessage passing a reference to this MailMessage object. There is no extra overhead on parsing the message since LoadMessage just reads a file into memory. The MailMessage won't parse the contained message unless the application starts accessing its properties or methods (so-called "lazy" model).

To upload a mail message just sent using Smtp component, the developer call UploadMessage method passing a reference to the Message property.

To obtain the UID value assigned to the uploaded message, the developer should either examine DestUidString property of UidPlusResult object passed in res parameter of UploadMessage method (the server must support UIDPLUS capability), or obtain UidNext value calling GetFolderStatus method before making upload (this approach is compatible with all servers).

Exceptions

Exception TypeCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This sample sends a mail message using Smtp component and then uploads it into "Sent" folder setting "\Seen" and "\Answered" flags.

The sample assumes "Sent" folder had already been created in the past. To learn how to make sure the folder exists, see UploadMessage or UploadMessage samples.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;
using MailBee.SmtpMail;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        // Send the message using Smtp object.
        Smtp mailer = new Smtp();

        // Send directly (via MX lookup). To send via SMTP server,
        // comment the next line and uncomment 2nd or 3rd statement below.
        mailer.DnsServers.Autodetect();

        // Uncomment to send via SMTP server not using SMTP authentication.
        // mailer.SmtpServers.Add("smtp.host.com");

        // Uncomment to send via SMTP server using SMTP authentication.    
        // mailer.SmtpServers.Add("smtp.host.com", "jdoe", "secret");

        mailer.From = new EmailAddress("jdoe@host.com", "John Doe");
        mailer.To.Add("sally@company.com", "Sally Smith");
        mailer.Subject = "Annual report";
        mailer.BodyPlainText = "The report contents";
        mailer.Send();

        Imap imp = new Imap();

        imp.Log.Filename = @"C:\log.txt";
        imp.Log.Enabled = true;
        imp.Log.Clear();

        // Connect to the IMAP4 server and log in the account.
        imp.Connect("imap.host.com");
        imp.Login("jdoe", "secret");

        // Upload the message just sent into "Sent" folder and set
        // \Seen and \Answered flags.
        imp.UploadMessage(mailer.Message, "Sent",
            SystemMessageFlags.Seen | SystemMessageFlags.Answered);

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail
Imports MailBee.SmtpMail
Imports MailBee.Mime

Module Sample
    Sub Main()
        ' Send the message using Smtp object.
        Dim mailer As New Smtp

        ' Send directly (via MX lookup). To send via SMTP server,
        ' comment the next line and uncomment 2nd or 3rd statement below.
        mailer.DnsServers.Autodetect()

        ' Uncomment to send via SMTP server not using SMTP authentication.
        ' mailer.SmtpServers.Add("smtp.host.com")

        ' Uncomment to send via SMTP server using SMTP authentication.    
        ' mailer.SmtpServers.Add("smtp.host.com", "jdoe", "secret")

        mailer.From = New EmailAddress("jdoe@host.com", "John Doe")
        mailer.To.Add("sally@company.com", "Sally Smith")
        mailer.Subject = "Annual report"
        mailer.BodyPlainText = "The report contents"
        mailer.Send()

        Dim imp As New Imap

        imp.Log.Filename = "C:\log.txt"
        imp.Log.Enabled = True
        imp.Log.Clear()

        ' Connect to the IMAP4 server and log in the account.
        imp.Connect("imap.host.com")
        imp.Login("jdoe", "secret")

        ' Upload the message just sent into "Sent" folder and set
        ' \Seen and \Answered flags.
        imp.UploadMessage(mailer.Message, "Sent", _
            SystemMessageFlags.Seen Or SystemMessageFlags.Answered)

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | Imap.UploadMessage Overload List | Smtp