Guest User

Untitled

a guest
Jan 22nd, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. public static void SendMail(string recipient, string subject, string body, string attachmentFilename)
  2. {
  3. SmtpClient smtpClient = new SmtpClient();
  4. NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password);
  5. MailMessage message = new MailMessage();
  6. MailAddress fromAddress = new MailAddress(MailConst.Username);
  7.  
  8. // setup up the host, increase the timeout to 5 minutes
  9. smtpClient.Host = MailConst.SmtpServer;
  10. smtpClient.UseDefaultCredentials = false;
  11. smtpClient.Credentials = basicCredential;
  12. smtpClient.Timeout = (60 * 5 * 1000);
  13.  
  14. message.From = fromAddress;
  15. message.Subject = subject;
  16. message.IsBodyHtml = false;
  17. message.Body = body;
  18. message.To.Add(recipient);
  19.  
  20. if (attachmentFilename != null)
  21. message.Attachments.Add(new Attachment(attachmentFilename));
  22.  
  23. smtpClient.Send(message);
  24. }
  25.  
  26. if (attachmentFilename != null)
  27. {
  28. Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
  29. ContentDisposition disposition = attachment.ContentDisposition;
  30. disposition.CreationDate = File.GetCreationTime(attachmentFilename);
  31. disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
  32. disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
  33. disposition.FileName = Path.GetFileName(attachmentFilename);
  34. disposition.Size = new FileInfo(attachmentFilename).Length;
  35. disposition.DispositionType = DispositionTypeNames.Attachment;
  36. message.Attachments.Add(attachment);
  37. }
  38.  
  39. using System.Net;
  40. using System.Net.Mail;
  41.  
  42. public void email_send()
  43. {
  44. MailMessage mail = new MailMessage();
  45. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  46. mail.From = new MailAddress("your mail@gmail.com");
  47. mail.To.Add("to_mail@gmail.com");
  48. mail.Subject = "Test Mail - 1";
  49. mail.Body = "mail with attachment";
  50.  
  51. System.Net.Mail.Attachment attachment;
  52. attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
  53. mail.Attachments.Add(attachment);
  54.  
  55. SmtpServer.Port = 587;
  56. SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
  57. SmtpServer.EnableSsl = true;
  58.  
  59. SmtpServer.Send(mail);
  60.  
  61. }
  62.  
  63. try
  64. {
  65. SmtpClient mailServer = new SmtpClient("smtp.gmail.com", 587);
  66. mailServer.EnableSsl = true;
  67.  
  68. mailServer.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
  69.  
  70. string from = "myemail@gmail.com";
  71. string to = "reciever@gmail.com";
  72. MailMessage msg = new MailMessage(from, to);
  73. msg.Subject = "Enter the subject here";
  74. msg.Body = "The message goes here.";
  75. msg.Attachments.Add(new Attachment("D:\myfile.txt"));
  76. mailServer.Send(msg);
  77. }
  78. catch (Exception ex)
  79. {
  80. Console.WriteLine("Unable to send email. Error : " + ex);
  81. }
  82.  
  83. System.Net.Mail.Attachment attachment;
  84. attachment = New System.Net.Mail.Attachment(Server.MapPath("~/App_Data/hello.pdf"));
  85. mail.Attachments.Add(attachment);
  86.  
  87. private void btnSent_Click(object sender, EventArgs e)
  88. {
  89. try
  90. {
  91. MailMessage mail = new MailMessage();
  92. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  93.  
  94. mail.From = new MailAddress(txtAcc.Text);
  95. mail.To.Add(txtToAdd.Text);
  96. mail.Subject = txtSub.Text;
  97. mail.Body = txtContent.Text;
  98. System.Net.Mail.Attachment attachment;
  99. attachment = new System.Net.Mail.Attachment(txtAttachment.Text);
  100. mail.Attachments.Add(attachment);
  101.  
  102. SmtpServer.Port = 587;
  103. SmtpServer.Credentials = new System.Net.NetworkCredential(txtAcc.Text, txtPassword.Text);
  104.  
  105. SmtpServer.EnableSsl = true;
  106.  
  107. SmtpServer.Send(mail);
  108. MessageBox.Show("mail send");
  109. }
  110. catch (Exception ex)
  111. {
  112. MessageBox.Show(ex.ToString());
  113. }
  114. }
  115.  
  116. private void button1_Click(object sender, EventArgs e)
  117. {
  118. MailMessage mail = new MailMessage();
  119. openFileDialog1.ShowDialog();
  120. System.Net.Mail.Attachment attachment;
  121. attachment = new System.Net.Mail.Attachment(openFileDialog1.FileName);
  122. mail.Attachments.Add(attachment);
  123. txtAttachment.Text =Convert.ToString (openFileDialog1.FileName);
  124. }
  125.  
  126. private void btnAtt_Click(object sender, EventArgs e) {
  127.  
  128. openFileDialog1.ShowDialog();
  129. Attachment myFile = new Attachment(openFileDialog1.FileName);
  130.  
  131. MyMsg.Attachments.Add(myFile);
  132.  
  133.  
  134. }
  135.  
  136. public void Send(string from, string password, string to, string Message, string subject, string host, int port, string file)
  137. {
  138.  
  139. MailMessage email = new MailMessage();
  140. email.From = new MailAddress(from);
  141. email.To.Add(to);
  142. email.Subject = subject;
  143. email.Body = Message;
  144. SmtpClient smtp = new SmtpClient(host, port);
  145. smtp.UseDefaultCredentials = false;
  146. NetworkCredential nc = new NetworkCredential(from, password);
  147. smtp.Credentials = nc;
  148. smtp.EnableSsl = true;
  149. email.IsBodyHtml = true;
  150. email.Priority = MailPriority.Normal;
  151. email.BodyEncoding = Encoding.UTF8;
  152.  
  153. if (file.Length > 0)
  154. {
  155. Attachment attachment;
  156. attachment = new Attachment(file);
  157. email.Attachments.Add(attachment);
  158. }
  159.  
  160. // smtp.Send(email);
  161. smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallBack);
  162. string userstate = "sending ...";
  163. smtp.SendAsync(email, userstate);
  164. }
  165.  
  166. private static void SendCompletedCallBack(object sender,AsyncCompletedEventArgs e) {
  167. string result = "";
  168. if (e.Cancelled)
  169. {
  170.  
  171. MessageBox.Show(string.Format("{0} send canceled.", e.UserState),"Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
  172. }
  173. else if (e.Error != null)
  174. {
  175. MessageBox.Show(string.Format("{0} {1}", e.UserState, e.Error), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
  176. }
  177. else {
  178. MessageBox.Show("your message is sended", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
  179. }
  180.  
  181. }
  182.  
  183. using (OpenFileDialog attachement = new OpenFileDialog()
  184. {
  185. Filter = "Exel Client|*.png",
  186. ValidateNames = true
  187. })
  188. {
  189. if (attachement.ShowDialog() == DialogResult.OK)
  190. {
  191. Send("yourmail@gmail.com", "gmail_password", "tomail@gmail.com", "just smile ", "mail with attachement", "smtp.gmail.com", 587, attachement.FileName);
  192.  
  193. }
  194. }
Add Comment
Please, Sign In to add comment