Access Gmail (SSL enabled) using SMTP, IMAP and POP3 protocols

In this article, we will show you how to access Gmail via SSL using Aspose.Network for .NET. We can access gmail using following protocols:

– SMTP
– IMAP
– POP3
– SMTP

In this section we will show you how to perform various tasks on Gmail server using SMTP protocol on SSL.

Connect to Gmail SMTP server

The following example shows you how you can connect to SSL enabled SMTP server. We will set the following properties for SSL support:

– SecurityMode
– EnableSSL
– Port

First create a new SmtpClient object as follows:

[C#]

Aspose.Network.Mail.SmtpClient client = new SmtpClient(“smtp.gmail.com”);

[VB.Net]

Dim client As Aspose.Network.Mail.SmtpClient = New SmtpClient(“smtp.gmail.com”)

We have passed smtp.gmail.com in the constructor which is the smtp server address of Gmail. Now, set the following properties which are necessary to connect to any SSL enabled mail server.

[C#]

// set your Gmail username
client.Username = “user@gmail.com“;
// set you Gmail password
client.Password = “pwd”;
// set the port to 587. This is the SSL port of Gmail SMTP server
client.Port = 587;
// set the security mode to explicit
client.SecurityMode = SmtpSslSecurityMode.Explicit;
// enable SSL
client.EnableSsl = true;

[VB.Net]

set your Gmail username
client.Username = “mailto:user@gmail.com
\\’ set you Gmail password
client.Password = \\\\”pwd\\\\”
\\’ set the port to 587. This is the SSL port of Gmail SMTP server
client.Port = 587
\\’ set the security mode to explicit
client.SecurityMode = SmtpSslSecurityMode.Explicit
\\’ enable SSL
client.EnableSsl = True

Send an email message

So far, we have setup the SMTP client object to connect to the Gmail server. To send a message using the above client object, we need to create a MailMessage object and send the message using SmtpClient object.

[C#]

Aspose.Network.Mail.MailMessage msg = new MailMessage();

[VB.Net]

Dim msg As Aspose.Network.Mail.MailMessage = New MailMessage()

Set the properties of the message e.g. subject, to, body as follows:

[C#]

// from whom
msg.From = new Aspose.Network.Mail.MailAddress(“user@gmail.com“);
// to whom
msg.To.Add( new Aspose.Network.Mail.MailAddress( “mailto:user@gmail.com” ) );
// set the subject
msg.Subject = “subject”;

// set the priority
msg.Priority = Aspose.Network.Mail.MailPriority.High;

// set the message bodies
msg.TextBody = “Please open with html browser”;

msg.HtmlBody = “this is the mail body“;

// add the attachment
Aspose.Network.Mail.Attachment attachment = new Attachment(@\\\\”c:\File.txt\\\\”);
msg.Attachments.Add(attachment);

try
{
// send the message
client.Send(msg);
}
// catch exception
catch( SmtpException ex )
{
System.Diagnostics.Trace.WriteLine( ex.ToString() );
}

Read the rest of this article.

Contact Information:

Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
Aspose – The .NET and Java component publisher
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.94651