Adsence750x90

Monday, April 7, 2008

Send Emails Using Gmail Account With Attachment file in ASP.Net - SMTP mail

Send Emails Using Gmail Account With Attachment file(ASP.NET /C#)

Description

you can send emails via your application using your gmail account.first you change setting in your gmail account
step 1:
login to your gmail Account this id is used for sending emails from your application
step 2:
goto gmail settings then click on Forwarding and POP/IMAP
step 3:
In IMAP Access Check Enable IMAP
step 4:
then goto ur application use below code

C# ASP.NET CODE


ButtonClick

{

string attachmentFile = @"C:\Documents and Settings\Administrator\Desktop\testImage.jpg";
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);

}






private static 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);
}
}

No comments: