Advertisement
Guest User

Untitled

a guest
May 10th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.Net.Mail;
  12. using System.Net;
  13.  
  14. using System.Timers;
  15.  
  16. using System.Web;
  17. using System.IO;
  18.  
  19. using System.Management;
  20. using System.Text.RegularExpressions;
  21.  
  22. namespace BDEmailer
  23. {
  24. public partial class Form1 : Form
  25. {
  26. public struct Settings
  27. {
  28. public static readonly string filepath = @"C:\email";
  29. //public static readonly string filepath = "V:\\DATA\\BD_AAI\\email";
  30.  
  31. }
  32.  
  33. private class EmailInformation
  34. {
  35. public readonly string server;
  36. public readonly string user;
  37. public readonly string pass;
  38. public readonly string from;
  39. public readonly string subject;
  40. public readonly string to;
  41.  
  42. public EmailInformation(string to)
  43. {
  44. server = "mail.vanikinteractive.com";
  45. user = "info@vanikinteractive.com";
  46. pass = "info01";
  47. from = "info@vanikinteractive.com";
  48. subject = "Your Brilliantly Colored Masterpiece from BD";
  49. this.to = to;
  50. }
  51.  
  52. private readonly string serverPath = "http://www.vanikftp.com/bd_aai/photos/";
  53. public string MailBody(string filePath)
  54. {
  55. StringBuilder mailBody = new StringBuilder();
  56. mailBody.AppendFormat("<html>");
  57.  
  58. mailBody.AppendFormat("<body>");
  59.  
  60. mailBody.AppendFormat("<table width='100 % ' align='center'>");
  61.  
  62. mailBody.AppendFormat("<tr>");
  63.  
  64. mailBody.AppendFormat("<td align='center'><a href=" + serverPath + filePath + " download='face2.jpg'><button id='Button1'>Click here to Download your Masterpiece</button></a></td>");
  65.  
  66. mailBody.AppendFormat("</tr>");
  67.  
  68. mailBody.AppendFormat("<tr>");
  69.  
  70. mailBody.AppendFormat("<td align='center'>or right-click the image below and choose save image as</td>");
  71.  
  72. mailBody.AppendFormat("</tr>");
  73.  
  74. mailBody.AppendFormat("<tr>");
  75.  
  76. mailBody.AppendFormat("<td align='center'><img src=" + serverPath + filePath + " border='0' alt=''></td>");
  77.  
  78. mailBody.AppendFormat("</tr>");
  79.  
  80. mailBody.AppendFormat("</table>");
  81.  
  82. mailBody.AppendFormat("</body>");
  83.  
  84. mailBody.AppendFormat("</html>");
  85.  
  86. return mailBody.ToString();
  87. }
  88. }
  89.  
  90.  
  91. System.Timers.Timer t;
  92. public Form1()
  93. {
  94. InitializeComponent();
  95. t = new System.Timers.Timer(60000);
  96. t.Elapsed += T_Elapsed;
  97. t.Enabled = true;
  98. }
  99.  
  100. private void T_Elapsed(object sender, ElapsedEventArgs e)
  101. {
  102. SendProcess();
  103. }
  104.  
  105. private void SendMail_Click(object sender, EventArgs e)
  106. {
  107. SendProcess();
  108. }
  109.  
  110. private List<Tuple<string, string>> EmailFileList;
  111. public void SendProcess()
  112. {
  113. try
  114. {
  115. Files = Emails = new List<string>();
  116. EmailFileList = new List<Tuple<string, string>>();
  117. ProcessDirectory(Settings.filepath, ref Files);
  118. Emails = FindEmails(Files);
  119.  
  120. for (int i = 0; i < Files.Count; i++)
  121. {
  122. UploadFTP(Files[i], Emails[i]);
  123. }
  124.  
  125. for (int i = 0; i < Emails.Count; i++)
  126. {
  127.  
  128. EmailInformation info = new EmailInformation(Emails[i]);
  129. SendEmail(info.to, info.from, info.subject, info.MailBody("_" + Emails[i] + "_.jpg")
  130. , info.server, info.user, info.pass);
  131. }
  132.  
  133. string files = "";
  134. foreach (string file in Files)
  135. files += file + "\n";
  136.  
  137. string emails = "";
  138. foreach (string email in Emails)
  139. emails += email + "\n";
  140.  
  141. MessageBox.Show("Email Sent Successfully\n" + files + emails);
  142. }
  143. catch (Exception ex)
  144. {
  145. throw ex;
  146. }
  147. }
  148.  
  149. private readonly string user = "vi-ftp-becton";
  150. private readonly string pass = "bD2016vi^^";
  151. private void UploadFTP(string filepath, string email)
  152. {
  153.  
  154. FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(@"ftp://vanikftp.com/" + "_" + email + "_" + ".jpg");
  155. ftpClient.Credentials = new System.Net.NetworkCredential(user, pass);
  156. ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  157. ftpClient.UseBinary = true;
  158. ftpClient.KeepAlive = true;
  159. System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
  160. ftpClient.ContentLength = fi.Length;
  161. byte[] buffer = new byte[4097];
  162. int bytes = 0;
  163. int total_bytes = (int)fi.Length;
  164. System.IO.FileStream fs = fi.OpenRead();
  165. System.IO.Stream rs = ftpClient.GetRequestStream();
  166. while (total_bytes > 0)
  167. {
  168. bytes = fs.Read(buffer, 0, buffer.Length);
  169. rs.Write(buffer, 0, bytes);
  170. total_bytes = total_bytes - bytes;
  171. }
  172. //fs.Flush();
  173. fs.Close();
  174. rs.Close();
  175. FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
  176. //value = uploadResponse.StatusDescription;
  177. uploadResponse.Close();
  178.  
  179. }
  180.  
  181. List<string> Emails;
  182. readonly char delimitChar = '_';
  183. private List<string> FindEmails (List<string> Files)
  184. {
  185. List<string> emails = new List<string>();
  186. foreach (string s in Files)
  187. {
  188. string[] words = s.Split(delimitChar);
  189. foreach (string w in words)
  190. {
  191. if (w.Contains("@"))
  192. {
  193. emails.Add(w);
  194. EmailFileList.Add(new Tuple<string, string>(w, s));
  195. }
  196. }
  197. }
  198. return emails;
  199. }
  200.  
  201. List<string> Files;
  202. private void ProcessDirectory(string targetDirectory, ref List<string> Files)
  203. {
  204. string directory = targetDirectory;
  205. try
  206. {
  207. string[] fileEntries = Directory.GetFiles(Settings.filepath); //Directory.GetFiles(directory);
  208. foreach (string fileName in fileEntries)
  209. Files.Add(fileName);
  210.  
  211. //string[] subdirectories = Directory.GetDirectories(directory);
  212. //foreach (string subdirectory in subdirectories)
  213. // ProcessDirectory(subdirectory, ref Files);
  214. }
  215. catch(Exception e)
  216. {
  217. throw e;
  218. }
  219. }
  220.  
  221. List<string> alreadySent = new List<string>();
  222. public void SendEmail(string emailTo, string emailFrom, string subject, string body, string server, string user, string pass)
  223. {
  224. TextReader tr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\" + "sent.txt");
  225. string line;
  226. int counter = 0;
  227.  
  228. while ((line = tr.ReadLine()) != null)
  229. {
  230. alreadySent.Add(line);
  231. counter++;
  232. }
  233.  
  234. tr.Close();
  235.  
  236. if (alreadySent.Contains(emailTo))
  237. return;
  238.  
  239. try
  240. {
  241. MailMessage m;
  242.  
  243. m = new MailMessage(new MailAddress(emailFrom), new MailAddress(emailTo));
  244.  
  245. m.IsBodyHtml = true;
  246. m.Subject = subject;
  247.  
  248. m.Body = body;
  249.  
  250. SmtpClient sc = new SmtpClient();
  251. sc.Credentials = new NetworkCredential(user, pass);
  252. sc.Host = server;
  253. sc.EnableSsl = false;
  254.  
  255. sc.Send(m);
  256.  
  257. System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\" + "sent.txt", true);
  258. file.WriteLine(emailTo);
  259. file.Close();
  260. }
  261. catch(Exception e)
  262. {
  263. return;
  264. }
  265.  
  266. }
  267.  
  268.  
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement