Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net.Mail;
- using System.Net;
- using System.Timers;
- using System.Web;
- using System.IO;
- using System.Management;
- using System.Text.RegularExpressions;
- namespace BDEmailer
- {
- public partial class Form1 : Form
- {
- public struct Settings
- {
- public static readonly string filepath = @"C:\email";
- //public static readonly string filepath = "V:\\DATA\\BD_AAI\\email";
- }
- private class EmailInformation
- {
- public readonly string server;
- public readonly string user;
- public readonly string pass;
- public readonly string from;
- public readonly string subject;
- public readonly string to;
- public EmailInformation(string to)
- {
- server = "mail.vanikinteractive.com";
- user = "info@vanikinteractive.com";
- pass = "info01";
- from = "info@vanikinteractive.com";
- subject = "Your Brilliantly Colored Masterpiece from BD";
- this.to = to;
- }
- private readonly string serverPath = "http://www.vanikftp.com/bd_aai/photos/";
- public string MailBody(string filePath)
- {
- StringBuilder mailBody = new StringBuilder();
- mailBody.AppendFormat("<html>");
- mailBody.AppendFormat("<body>");
- mailBody.AppendFormat("<table width='100 % ' align='center'>");
- mailBody.AppendFormat("<tr>");
- mailBody.AppendFormat("<td align='center'><a href=" + serverPath + filePath + " download='face2.jpg'><button id='Button1'>Click here to Download your Masterpiece</button></a></td>");
- mailBody.AppendFormat("</tr>");
- mailBody.AppendFormat("<tr>");
- mailBody.AppendFormat("<td align='center'>or right-click the image below and choose save image as</td>");
- mailBody.AppendFormat("</tr>");
- mailBody.AppendFormat("<tr>");
- mailBody.AppendFormat("<td align='center'><img src=" + serverPath + filePath + " border='0' alt=''></td>");
- mailBody.AppendFormat("</tr>");
- mailBody.AppendFormat("</table>");
- mailBody.AppendFormat("</body>");
- mailBody.AppendFormat("</html>");
- return mailBody.ToString();
- }
- }
- System.Timers.Timer t;
- public Form1()
- {
- InitializeComponent();
- t = new System.Timers.Timer(60000);
- t.Elapsed += T_Elapsed;
- t.Enabled = true;
- }
- private void T_Elapsed(object sender, ElapsedEventArgs e)
- {
- SendProcess();
- }
- private void SendMail_Click(object sender, EventArgs e)
- {
- SendProcess();
- }
- private List<Tuple<string, string>> EmailFileList;
- public void SendProcess()
- {
- try
- {
- Files = Emails = new List<string>();
- EmailFileList = new List<Tuple<string, string>>();
- ProcessDirectory(Settings.filepath, ref Files);
- Emails = FindEmails(Files);
- for (int i = 0; i < Files.Count; i++)
- {
- UploadFTP(Files[i], Emails[i]);
- }
- for (int i = 0; i < Emails.Count; i++)
- {
- EmailInformation info = new EmailInformation(Emails[i]);
- SendEmail(info.to, info.from, info.subject, info.MailBody("_" + Emails[i] + "_.jpg")
- , info.server, info.user, info.pass);
- }
- string files = "";
- foreach (string file in Files)
- files += file + "\n";
- string emails = "";
- foreach (string email in Emails)
- emails += email + "\n";
- MessageBox.Show("Email Sent Successfully\n" + files + emails);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private readonly string user = "vi-ftp-becton";
- private readonly string pass = "bD2016vi^^";
- private void UploadFTP(string filepath, string email)
- {
- FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(@"ftp://vanikftp.com/" + "_" + email + "_" + ".jpg");
- ftpClient.Credentials = new System.Net.NetworkCredential(user, pass);
- ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
- ftpClient.UseBinary = true;
- ftpClient.KeepAlive = true;
- System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
- ftpClient.ContentLength = fi.Length;
- byte[] buffer = new byte[4097];
- int bytes = 0;
- int total_bytes = (int)fi.Length;
- System.IO.FileStream fs = fi.OpenRead();
- System.IO.Stream rs = ftpClient.GetRequestStream();
- while (total_bytes > 0)
- {
- bytes = fs.Read(buffer, 0, buffer.Length);
- rs.Write(buffer, 0, bytes);
- total_bytes = total_bytes - bytes;
- }
- //fs.Flush();
- fs.Close();
- rs.Close();
- FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
- //value = uploadResponse.StatusDescription;
- uploadResponse.Close();
- }
- List<string> Emails;
- readonly char delimitChar = '_';
- private List<string> FindEmails (List<string> Files)
- {
- List<string> emails = new List<string>();
- foreach (string s in Files)
- {
- string[] words = s.Split(delimitChar);
- foreach (string w in words)
- {
- if (w.Contains("@"))
- {
- emails.Add(w);
- EmailFileList.Add(new Tuple<string, string>(w, s));
- }
- }
- }
- return emails;
- }
- List<string> Files;
- private void ProcessDirectory(string targetDirectory, ref List<string> Files)
- {
- string directory = targetDirectory;
- try
- {
- string[] fileEntries = Directory.GetFiles(Settings.filepath); //Directory.GetFiles(directory);
- foreach (string fileName in fileEntries)
- Files.Add(fileName);
- //string[] subdirectories = Directory.GetDirectories(directory);
- //foreach (string subdirectory in subdirectories)
- // ProcessDirectory(subdirectory, ref Files);
- }
- catch(Exception e)
- {
- throw e;
- }
- }
- List<string> alreadySent = new List<string>();
- public void SendEmail(string emailTo, string emailFrom, string subject, string body, string server, string user, string pass)
- {
- TextReader tr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\" + "sent.txt");
- string line;
- int counter = 0;
- while ((line = tr.ReadLine()) != null)
- {
- alreadySent.Add(line);
- counter++;
- }
- tr.Close();
- if (alreadySent.Contains(emailTo))
- return;
- try
- {
- MailMessage m;
- m = new MailMessage(new MailAddress(emailFrom), new MailAddress(emailTo));
- m.IsBodyHtml = true;
- m.Subject = subject;
- m.Body = body;
- SmtpClient sc = new SmtpClient();
- sc.Credentials = new NetworkCredential(user, pass);
- sc.Host = server;
- sc.EnableSsl = false;
- sc.Send(m);
- System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\" + "sent.txt", true);
- file.WriteLine(emailTo);
- file.Close();
- }
- catch(Exception e)
- {
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement