Send Emails with Gmail Account with Attachment

With the code sample below you can send emails via your application using your Gmail account. First you need to change the settings in your Gmail account

step 1: Login to your Gmail Account with from which you will be sending e-mails.

step 2: Go to Gmail settings then click on Forwarding and POP/IMAP

step 3: In IMAP Access Check Enable IMAP

step 4: Then go to your application use below code

The AttachFile() method such be called first, which in turn will call the SendMail() method.

void AttachFile(string attachmentFile)
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("");
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("");
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);
mm.Subject = "Email Subject";
System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(printScreen);
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.UTF8;

string sendMail(System.Net.Mail.MailMessage mm)
string smtpHost = "";
string userName = "";//sending Id
string password = "your-password";
System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
mClient.Port = 587;
mClient.EnableSsl = true;
mClient.UseDefaultCredentials = false;
mClient.Credentials = new NetworkCredential(userName, password);
mClient.Host = smtpHost;
mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
catch (Exception ex)


Author: Varma Suresh30 Oct 2009 Member Level: Gold   Points : 0

HI how can I send Mails from Yahoo or Hotmail can u send smtp host name

Author: Gaurav Kumar Arora30 Oct 2009 Member Level: Gold   Points : 1

A nice representation of code , but there is one more code which is more near to fashion written by Meetu Choudhary.

Suresh - You can check the relevant sites to get their smtp

Author: Varma Suresh30 Oct 2009 Member Level: Gold   Points : 0

ok thanks for u r repaly...
I got it.

Author: Anand07 Dec 2010 Member Level: Bronze   Points : 1

please help me i am new to dot net and i unable to send mail with gmail account in windows application

i use the following code.
for attachement

Dim toAddress As New System.Net.Mail.MailAddress("")
Dim fromAddress1 As New System.Net.Mail.MailAddress("")
Dim mm As New System.Net.Mail.MailMessage(fromAddress1, toAddress)
mm.Subject = "SPIN SOFT : Cotton Receipt - MCF Report for the month of " & Format(dtpStartDate, "MMMM, yyyy")
Dim mailAttachment As New System.Net.Mail.Attachment("C:\123.pdf")
mm.IsBodyHtml = True
mm.BodyEncoding = System.Text.Encoding.UTF8
mm.Body = "Please find the attachment for the report in Portable Document Format." & Chr(13) & "Reports generated from Spinsoft. Spinsoft is powered by Vertex Infocomm."

and send mail function is

Sub SendMail(ByVal msg As System.Net.Mail.MailMessage)
Dim smtpHost As String = ""
Dim userName As String = ""
Dim password As String = "mypassowrd"
Dim mClient As New System.Net.Mail.SmtpClient()
mClient.Port = 587
mClient.EnableSsl = True
mClient.UseDefaultCredentials = False
mClient.Credentials = New Net.NetworkCredential(userName, password)
mClient.Host = smtpHost
mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network

Catch ex As Exception
End Try
End Sub

but i got the following error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication
Required. Learn more at

Thank you