Resources » Code Snippets » Email

Send Emails with Gmail Account with Attachment


Posted Date: 03-Apr-2008  Last Updated:   Category: Email    
Author: Member Level: Gold    Points: 8



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("your-reciving-email@gmail.com");
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("fromAddress@yahoo.com");
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.Attachments.Add(mailAttachment);
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.UTF8;
sendMail(mm);
}

string sendMail(System.Net.Mail.MailMessage mm)
{
try
{
string smtpHost = "smtp.gmail.com";
string userName = "your-email-address@gmail.com";//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;
mClient.Send(mm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Send Emails with Gmail Account with Attachment"
Author: Varma Suresh    30 Oct 2009Member Level: Gold   Points : 0
HI how can I send Mails from Yahoo or Hotmail can u send smtp host name


Author: Gaurav Arora    30 Oct 2009Member 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 Suresh    30 Oct 2009Member Level: Gold   Points : 0
ok thanks for u r repaly...
I got it.



Author: Anand    07 Dec 2010Member Level: Bronze   Points : 1
please help me i am new to dot net and i unable to send mail with gmail account in vb.net windows application

i use the following code.
for attachement

Dim toAddress As New System.Net.Mail.MailAddress("mailanandha@gmail.com")
Dim fromAddress1 As New System.Net.Mail.MailAddress("spinsoft@vertexinfocomm.in")
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.Attachments.Add(mailAttachment)
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."
sendMail(mm)


and send mail function is

Sub SendMail(ByVal msg As System.Net.Mail.MailMessage)
Try
Dim smtpHost As String = "smtp.gmail.com"
Dim userName As String = "spinsoft@vertexinfocomm.in"
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
mClient.Send(msg)

Catch ex As Exception
MessageBox.Show(ex.Message)
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



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India