How to handle IMapClient to work with email such as gmail, hotmail and yahoo mail (C# .NET)

I running to trial on Aspose Email.

I want to know how to program the above mention email?

Regards

Please refer the below Post for details regarding Gmail

<a href="https://forum.aspose.com/t/37685</a></p><p> </p><p> </p>

How about Hotmail and yahoo?

Hi Chee,

Thanks for writing to Aspose.Email support team.

Aspose.Email(AE) provides variety of classes to process emails for Gmail, hotmail, yahoo and other exchanges. AE supports all the commonly used protocols i.e. STMP, Pop3 and Imap used in these servers. Both the SSL enabled/disabled configurations are supported by AE.

Following is a sample code which can be used to access mails from Gmail Inbox.

Aspose.Email.Pop3.Pop3Client client;
client = new Aspose.Email.Pop3.Pop3Client();

//Basic settings (required)
client.Host = “[pop.gmail.com ](http://pop.gmail.com/)”;
client.Username = “user”;
client.Password = “password”;

// set implicit security mode
client.SecurityMode = Aspose.Email.Pop3.Pop3SslSecurityMode.Implicit;

// enable SSL
client.EnableSsl = true;
client.Port = 995;

//Connect and login to Pop3 server
try
{
client.Connect();
client.Login();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

//Retrieve Message
try
{
//mail parser
Aspose.Email.Mail.MailMessage msg;

//retrieve first message in MailMessage format directly
msg = client.FetchMessage(1);
Console.WriteLine(“Received date: " + msg.Headers[“Received”].ToString());
string Received = msg.Headers[“Received”].ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//Disconnect from Pop3 server
try
{
client.Disconnect();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

For Hotmail you need to change the following settings:

[//client.Host](https://client.host/) = “[pop.gmail.com ](http://pop.gmail.com/)”;
client.Host = “[pop3.live.com](http://pop3.live.com/)”;

For Yahoo, following changes are required

[//client.Host](https://client.host/) = “[pop.gmail.com ](http://pop.gmail.com/)”;
[//client.Port](https://client.port/) = 995;

client.Host = “[pop.mail.yahoo.com](http://pop.mail.yahoo.com/)”;
client.Port = 465;

Following is the sample code that can be used to send mail via these servers using SmtpClient:

//Declare message as MailMessage instance
MailMessage message = new MailMessage();

//Here is the sender’s address
message.From = "sender@gmail.com”;

//Here is the recipient’s address
message.To = "receiver@gmail.com";

message.Subject = “My First Mail”;

//Here is the email message
message.TextBody = “Body of the message”;

//Send email using SmtpClient
//Create an instance of the SmtpClient Class
SmtpClient client = new SmtpClient();
client.Host = “[smtp.gmail.com](http://smtp.gmail.com/)”;

//Specify your mail user name
client.Username = “user”;

//Specify your mail password
client.Password = “password”;

//Specify your Port #
client.Port = 587;
client.EnableSsl = true;
client.SecurityMode = SmtpSslSecurityMode.Explicit;
client.Send(message);

For hotmail server, following changes are required:

[//client.Host](https://client.host/) = “[smtp.gmail.com](http://smtp.gmail.com/)”;
client.Host = “[smtp.live.com](http://smtp.live.com/)”;

For Yahoo server following are the changes:

[//client.Host](https://client.host/) = “[smtp.gmail.com](http://smtp.gmail.com/)”;
client.Host = “[smtp.mail.yahoo.com](http://smtp.mail.yahoo.com/)”;

Following is the sample code which can be used to access any folder in the server and process mails.

// Connect and login to IMAP
Aspose.Email.Imap.ImapClient imap = new Aspose.Email.Imap.ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”, 993, "user@gmail.com", “password”);
imap.EnableSsl = true; // enable the SSL
imap.SecurityMode = Aspose.Email.Imap.ImapSslSecurityMode.Implicit; // set security mode

client.Connect(true);
client.SelectFolder(“Inbox”);

// Set conditions
ImapQueryBuilder builder = new ImapQueryBuilder();

// Subject contains “Newsletter”
builder.Subject.Contains(“Newsletter”);

// Emails that arrived today
builder.InternalDate.On(DateTime.Now);

// Build the query
MailQuery query = builder.GetQuery();

// Get list of messages
ImapMessageInfoCollection messages = client.ListMessages(query);
Console.WriteLine(“Imap: " + messages.Count + " message(s) found.”);

// Disconnect from IMAP
client.Disconnect();

Currently Hotmail does not support Imap protocol.

For Yahoo following changes are required:

[//Aspose.Email.Imap.ImapClient](https://aspose.email.imap.imapclient/) imap = new Aspose.Email.Imap.ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”, 993, "user@gmail.com", “password”);
Aspose.Email.Imap.ImapClient imap = new Aspose.Email.Imap.ImapClient(“[imap.mail.yahoo.com ](http://imap.mail.yahoo.com/)”, 993, "user@gmail.com", “password”);

Following are few of the links for your reference:

Sending Email messages with Smtp

Managing Email messages with Pop3

Managin Email with Imap

Please feel free to write us back if you have any other query in this regard.

when i use this code.
It show an error message “The type or namespace name “imap” does not exist in the namespace “aspose.Email””
Please give me some suggetion.

@alibardisk,

I suggest you to please try using following sample code to access the gmail account.

            // connect to gmail server
            Aspose.Email.Clients.Imap.ImapClient imap = new Aspose.Email.Clients.Imap.ImapClient("imap.gmail.com", 993, "test3@gmail.com", "pass");

You don’t need to set following line.

imap.EnableSsl = true; // enable the SSL

PersonalStorage pststorage = PersonalStorage.FromFile(browseFile.FileName);
ImapClient imapClient = new ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”,993, "test@gmail.com", “pass”);
{
RestoreSettings restoreSettings = RestoreOptions.Recursive;
imapClient.Restore(pststorage, restoreSettings);
}

when i run this code it show a error message “AE_1_1_0009 NO [ALREADYEXISTS] Duplicate folder name (Failure)” how can i solve this issue?

@alibardisk,

In order to resolve this issue you need to delete existing email folder before restore or rename that to commence restore.