-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailSender.cs
144 lines (121 loc) · 5.14 KB
/
MailSender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MailSender
{
public partial class Form1 : Form
{
public List<string> someFiles;
public Form1()
{
InitializeComponent();
passwordTextBox.PasswordChar = '*';
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void textBox2_MouseClick(object sender, MouseEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectedText = comboBox1.SelectedText.Replace("\n", "");
comboBox1.SelectedText = comboBox1.SelectedText.Replace("\r", "");
someFiles = File.ReadAllLines(textBox2.Text).ToList();
richTextBox2.AppendText("Mail list loaded into memory !"+ Environment.NewLine);
comboBox1.SelectedIndex = 0;
if (comboBox1.SelectedItem == null)
{
timer1.Interval = Convert.ToInt16(comboBox1.GetItemText("900")) * 1000;
}
timer1.Enabled = true;
timer1.Interval = Convert.ToInt16(comboBox1.GetItemText("900")) * 1000;
timer1.Interval = 900000;
richTextBox2.AppendText("sending interval set to : " + timer1.Interval.ToString()+Environment.NewLine);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("60");
}
private void timer1_Tick(object sender, EventArgs e)
{
string line = "";
int counter = -1;
int flag = 0;
try {
string Subject = textBox1.Text;
string body;
foreach (string item in someFiles)//initialize the list from file before first use from the upper two lines
{
counter++;
String lin1 = item;
if (!item.Contains("Processed"))
{
flag = 1;
line = lin1;
break;
}
}
if (flag == 1)
{
string[] arrayList = line.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
// The form is as follow: name, mail, city, adress
Subject = textBox1.Text.Replace("#", arrayList[0]);
body = richTextBox1.Text;
body = body.Replace("#", arrayList[0]);
body = body.Replace("&", arrayList[2]);
body = body.Replace("^", arrayList[3]);
SmtpClient client = new SmtpClient();
client.Host = HostTextBox.Text;
client.Port = Convert.ToInt32(PortTextBox.Text);
client.EnableSsl = false;
client.Timeout = Convert.ToInt32(timeOutTextBox.Text);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromMailTextBox.Text, passwordTextBox.Text);
arrayList[1].Replace("\t","");
arrayList[1].Replace("\n", "");
arrayList[1].Replace(" ", "");
arrayList[1].Replace("\r", "");
MailMessage mail = new MailMessage(Convert.ToString(fromMailTextBox.Text), arrayList[1], Subject, body);
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.IsBodyHtml = true;
client.Send(mail);
someFiles[counter] = line + "; Processed";
richTextBox2.AppendText(someFiles[counter] + Environment.NewLine);
}
else
{
richTextBox2.AppendText("list finished!!" + Environment.NewLine);
StringBuilder sb = new StringBuilder();
foreach (var l in someFiles)
{
sb.AppendLine(l);
}
File.AppendAllText( "log.txt", sb.ToString());
Environment.Exit(0);
}
}
catch (Exception ex)
{
richTextBox2.AppendText(ex.ToString() + Environment.NewLine);
someFiles[counter] = line + "; Processed->error";
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}