Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. class Email
  2. {
  3.  
  4. public Email(string password, string username)
  5. {
  6. this.username = username;
  7. this.password = password;
  8. }
  9.  
  10. private string username;
  11. private string password;
  12.  
  13. public bool Send(string toAddress, string subject,
  14. string body, string attachment)
  15. {
  16. // Create the message
  17. System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
  18.  
  19. // Add all the addresses
  20. message.To.Add(new System.Net.Mail.MailAddress(toAddress));
  21.  
  22. // Set the from address
  23. message.From = new System.Net.Mail.MailAddress(this.username);
  24.  
  25. // Set the subject
  26. message.Subject = subject;
  27. // Set the subject encoding
  28. message.SubjectEncoding = Encoding.UTF8;
  29.  
  30. // Set the body
  31. message.Body = body;
  32. // Set the body encoding
  33. message.BodyEncoding = Encoding.UTF8;
  34. // The body is not an HTML
  35. message.IsBodyHtml = false;
  36.  
  37. // Set the priority
  38. message.Priority = System.Net.Mail.MailPriority.High;
  39.  
  40. // Connect to Gmail
  41. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
  42.  
  43. smtpClient.UseDefaultCredentials = false;
  44. smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  45.  
  46. // Set the credentials
  47. smtpClient.Credentials = new System.Net.NetworkCredential(this.username, this.password);
  48. // Set the port
  49. smtpClient.Port = 25;
  50. // Set the host
  51. smtpClient.Host = "smtp.gmail.com";
  52. // Enable SSL
  53. smtpClient.EnableSsl = true;
  54.  
  55. // Add attachments
  56. message.Attachments.Add(new System.Net.Mail.Attachment(attachment));
  57.  
  58. // Send the message
  59. try
  60. {
  61. smtpClient.Send(message);
  62. return true;
  63. }
  64. catch
  65. {
  66. message.Attachments.Dispose();
  67. return false;
  68. }
  69.  
  70. }
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement