We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

planetcall on 02/13/08


Tagged

sendmail smtp gmail aspnet csharp


Versions (?)


Send Email from your GMAIL account using ASP.Net and C#


Published in: C# 


URL: http://shabdar.org/send-email-using-gmail-account-asp-net-csharp.html

This is a code example which demonstrates how to send Email with attachment using your GMAIL Account.


  1. using System.Web.Mail;
  2. using System;
  3. public class MailSender
  4. {
  5. public static bool SendEmail(
  6. string pGmailEmail,
  7. string pGmailPassword,
  8. string pTo,
  9. string pSubject,
  10. string pBody,
  11. System.Web.Mail.MailFormat pFormat,
  12. string pAttachmentPath)
  13. {
  14. try
  15. {
  16. System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
  17. myMail.Fields.Add
  18. ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
  19. "smtp.gmail.com");
  20. myMail.Fields.Add
  21. ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
  22. "465");
  23. myMail.Fields.Add
  24. ("http://schemas.microsoft.com/cdo/configuration/sendusing",
  25. "2");
  26. //sendusing: cdoSendUsingPort, value 2, for sending the message using
  27. //the network.
  28.  
  29. //smtpauthenticate: Specifies the mechanism used when authenticating
  30. //to an SMTP
  31. //service over the network. Possible values are:
  32. //- cdoAnonymous, value 0. Do not authenticate.
  33. //- cdoBasic, value 1. Use basic clear-text authentication.
  34. //When using this option you have to provide the user name and password
  35. //through the sendusername and sendpassword fields.
  36. //- cdoNTLM, value 2. The current process security context is used to
  37. // authenticate with the service.
  38. myMail.Fields.Add
  39. ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
  40. //Use 0 for anonymous
  41. myMail.Fields.Add
  42. ("http://schemas.microsoft.com/cdo/configuration/sendusername",
  43. pGmailEmail);
  44. myMail.Fields.Add
  45. ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
  46. pGmailPassword);
  47. myMail.Fields.Add
  48. ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
  49. "true");
  50. myMail.From = pGmailEmail;
  51. myMail.To = pTo;
  52. myMail.Subject = pSubject;
  53. myMail.BodyFormat = pFormat;
  54. myMail.Body = pBody;
  55. if (pAttachmentPath.Trim() != "")
  56. {
  57. MailAttachment MyAttachment =
  58. new MailAttachment(pAttachmentPath);
  59. myMail.Attachments.Add(MyAttachment);
  60. myMail.Priority = System.Web.Mail.MailPriority.High;
  61. }
  62.  
  63. System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
  64. System.Web.Mail.SmtpMail.Send(myMail);
  65. return true;
  66. }
  67. catch (Exception ex)
  68. {
  69. throw;
  70. }
  71. }
  72. }
  73.  
  74.  
  75. //Example usage of this class
  76.  
  77. EmailLib.SendEmail.SendEmail("your_gmail_id",
  78. "your_gmail_password",
  79. "to_email@anydomain.com",
  80. "This is email subject" ,
  81. "This is email body",
  82. Web.Mail.MailFormat.Text,
  83. "Physical path to your Attachment")

Report this snippet 

You need to login to post a comment.