C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Send Emails with Gmail Account with Attachment


Posted Date: 03 Apr 2008    Resource Type: Code Snippets    Category: Email
Author: Raju.MMember Level: Gold    
Rating: 1 out of 5Points: 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);
}
}



Responses

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: Diamond   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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Send Mail with GMail account with attachments  .  Send Mail with GMail account  .  Send Mail using IMAP  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Send a mail in asp.net with c# 2.0
Previous Resource: Sending email using Gmail SMTP
Return to Discussion Resource Index
Post New Resource
Category: Email


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use