Accessing Gmail on SSL
SMTP
This article shows how to perform connect to a Gmail server and send an email using the SMTP protocol on SSL.
Connecting to Gmail SMTP server
The following code snippet shows you how to connect to an SSL enabled SMTP server.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
SmtpClient client = new SmtpClient("smtp.gmail.com"); | |
// Set username, Password, Port No, and SecurityOptions | |
client.Username = "your.email@gmail.com"; | |
client.Password = "your.password"; | |
client.Port = 587; |
Sending an Email Message
The code above set up the SMTPClient object to connect to the Gmail server. To send a message using the same client object, create a MailMessage class object and send the message using the SMTP client object. The following code snippet shows you how to set the message properties, for example the subject, to and body:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MailMessage msg = new MailMessage(); | |
// Set From, To whom | |
msg.From = new MailAddress("user@gmail.com"); | |
msg.To.Add(new MailAddress("user@gmail.com")); | |
// Set the subject, priority, Set the message bodies (plain text and HTML), and Add the attachment | |
msg.Subject = "subject"; | |
msg.Priority = MailPriority.High; | |
msg.Body = "Please open with html browser"; | |
msg.HtmlBody = "<bold>this is the mail body</bold>"; | |
Attachment attachment = new Attachment(dataDir + "File.txt"); | |
msg.Attachments.Add(attachment); | |
client.Send(msg); |
IMAP
This article shows how to perform a number of activities on an SSL enabled mail server using the IMAP protocol:
- Connect to a mail server.
- Get the total number of messages in an inbox.
- Save messages locally.
- Create a message an add it to a folder.
Connecting to the Mail Server
Use Aspos.Email’s ImapClient class object to connect to the mail server. The server’s address, port, username and password are required to establish a connection. Gmail uses port 993 for the IMAP protocol, the following code snippet shows you how connects to Gmail using that port.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Connect to the Gmail server | |
ImapClient imap = new ImapClient("imap.gmail.com", 993, "user@gmail.com", "pwd"); |
Selecting a Folder and Getting the Total Number of Messages
Checking the Inbox folder is the most frequent task when checking email. Using Aspose.Email, this can be done using just two simple lines of code. The following code snippet shows you how to access the Inbox folder and get the total number of messages in the folder.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
imap.SelectFolder(ImapFolderInfo.InBox); | |
Console.WriteLine(imap.CurrentFolder.TotalMessageCount + " messages found."); |
Saving Messages to a Local Hard Drive
Once a folder has been selected with the SelectFolder method, use the ListMessages function to get a list of all the messages in the folder in an ImapMessagesInfoCollection object. Iterate through this collection and save email messages to the local drive of computer as follows:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Gets the message info collection and Download each message | |
ImapMessageInfoCollection list = imap.ListMessages(); | |
for (int i = 0; i < list.Count; i++) | |
{ | |
// Save the message as an EML file | |
imap.SaveMessage(list[i].UniqueId, list[i].UniqueId + "_out.eml"); | |
} |
Creating a New Folder
The IMAP protocol also allows you to create a new folder on the email server. This can be done using a simple function call.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
imap.CreateFolder("NewFolder"); |
Creating a New Message in a Folder
Add a new message to the folder using the MailMessage and ImapClient classes. The examples below creates a MailMessage object first by providing the subject, to and from values. It then subscribes to a folder and adds the message to it. The following code snippet shows you create a new message in a folder.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Create a message and Subscribe to the Inbox folder ans Append the newly created message | |
MailMessage message = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message"); | |
imap.SelectFolder(ImapFolderInfo.InBox); | |
imap.SubscribeFolder(imap.CurrentFolder.Name); | |
imap.AppendMessage(imap.CurrentFolder.Name, message); |
POP3
This article shows some examples that use the POP3 protocol on SSL. To connect to an SSL-protected server, we need to define the SSL port and two extra properties. The rest of the code is the same as for connecting to a normal POP3 server.
The code samples below show how to:
- Connect to an SSL server.
- Check the mailbox status
- Get information about the message
- Retrieve emails.
Connecting to Mail Server
Connect to the SSL enabled mail server using the Pop3client class as described below.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Create a POP3 client | |
Pop3Client client = new Pop3Client(); | |
// Basic settings (required) | |
client.Host = "pop.gmail.com"; | |
client.Username = "user@gmail.com"; | |
client.Password = "pwd"; | |
client.Port = 995; | |
client.SecurityOptions = SecurityOptions.SSLImplicit; |
Checking the Mailbox Status
The following code snippet shows you how checks the number of messages stored in the mailbox and the size of the mailbox. Use Pop3MailboxInfo class for this purpose.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Create an object of type Pop3MailboxInfo and Get the mailbox information, Check number of messages, and Check mailbox size | |
Pop3MailboxInfo info; | |
info = client.GetMailboxInfo(); | |
Console.WriteLine(info.MessageCount); | |
Console.Write(info.OccupiedSize + " bytes"); |
Checking Message Information
This example checks all the messages in the mailbox using the Pop3MessageInfoCollection class. Use the Pop3Client.ListMessages() function to get the Pop3MessageInfoCollection collection. Then iterate through the collection to read the message information: message ID, index, subject and size
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get the list of messages in the mailbox and Iterate through the messages | |
Pop3MessageInfoCollection infos = client.ListMessages(); | |
foreach (Pop3MessageInfo info in infos) | |
{ | |
Console.Write("ID:" + info.UniqueId); | |
Console.Write("Index number:" + info.SequenceNumber); | |
Console.Write("Subject:" + info.Subject); | |
Console.Write("Size:" + info.Size); | |
} |
Retrieving Messages
To get the messages from the mailbox, use the Pop3Client class' FetchMessage() method to get the message into a MailMessage type object. The following code snippet shows you how to counts the number of emails in the mailbox and then iterates through them to retrieve each one.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get the number of messages in the mailbox | |
int messageCount = client.GetMessageCount(); | |
// Iterate through the messages and retrieve one by one | |
for (int i = 1; i <= messageCount; i++) | |
{ | |
// Create an object of type MailMessage and Retrieve the message in MailMessage format directly | |
Aspose.Email.MailMessage msg; | |
msg = client.FetchMessage(i); | |
Console.WriteLine("From:" + msg.From.ToString()); | |
Console.WriteLine("Subject:" + msg.Subject); | |
Console.WriteLine(msg.HtmlBody); | |
msg.Save(i + "Getmessage_out.eml"); | |
} |