- program.cs =
- using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace email_with_attach
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
- Form 1.cs=
- //this project didn't run after implementing form 2, form 2 handles the attachments
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace email_with_attach
{
public partial class Form1 : Form
{
MailMessage mail = new MailMessage();
public Form1()
{
InitializeComponent();
}
//adding attachments to the email, this action calls on form 2
private void button1_Click(object sender, EventArgs e)
{
smtp_csharp.frmAddAttachment frm = new smtp_csharp.frmAddAttachment();
frm.ShowDialog(this);
if (frm.txtFile.Text.Trim() != "")
listBox1.Items.Add(frm.txtFile.Text);
frm.Dispose();
}
//removes attachments from list box
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
//send button
private void button3_Click(object sender, EventArgs e)
{
using (MailMessage mailMessage =
new MailMessage(new MailAddress(textBox1.Text),
new MailAddress(textBox1.Text)))
{
mailMessage.Body = textBox5.Text;
mailMessage.Subject = textBox4.Text;
try
{
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials =
new System.Net.NetworkCredential(textBox2.Text, textBox3.Text);
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = textBox1.Text.Split(',');
mail.From = new MailAddress(textBox2.Text);
Byte i;
for (i = 0; i < addr.Length; i++)
mail.To.Add(addr[i]);
mail.Subject = textBox4.Text;
mail.Body = textBox5.Text;
if (listBox1.Items.Count != 0)
{
for (i = 0; i < listBox1.Items.Count; i++)
mail.Attachments.Add(new Attachment(listBox1.Items[i].ToString()));
}
mail.IsBodyHtml = true;
mail.DeliveryNotificationOptions =
DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(textBox1.Text);
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "EMail",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
}
- Form2.cs
- //when this is implemented to the project the progamme crashes
.using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace smtp_csharp
{
/// <summary>
/// Summary description for frmAddAttachment.
/// </summary>
public class frmAddAttachment : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button btnCancel;
internal System.Windows.Forms.Button btnOK;
internal System.Windows.Forms.Button btnBrowse;
internal System.Windows.Forms.OpenFileDialog dlg;
public System.Windows.Forms.TextBox txtFile;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmAddAttachment()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
dlg.ShowDialog();
txtFile.Text = dlg.FileName;
}
private void btnOK_Click(object sender, System.EventArgs e)
{
this.Hide();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
txtFile.Text = "";
this.Hide();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// frmAddAttachment
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "frmAddAttachment";
this.Load += new System.EventHandler(this.frmAddAttachment_Load);
this.ResumeLayout(false);
}
private void frmAddAttachment_Load(object sender, System.EventArgs e)
{
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
- MD5=Main.cs
- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace EnCryptDecrypt
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
if (txtClearText.Text == "")
{
error.SetError(txtClearText, "Enter the text you want to encrypt");
}
else
{
error.Clear();
string clearText = txtClearText.Text.Trim();
string cipherText = CryptorEngine.Encrypt(clearText, true);
txtDecryptedText.Visible = false;
label3.Visible = false;
txtCipherText.Text = cipherText;
btnDecrypt.Enabled = true;
}
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
string cipherText = txtCipherText.Text.Trim();
string decryptedText = CryptorEngine.Decrypt(cipherText, true);
txtDecryptedText.Text = decryptedText;
txtDecryptedText.Visible = true;
label3.Visible = true;
}
}
}
- Cryptor engine.cs
- using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
namespace EnCryptDecrypt
{
public class CryptorEngine
{
/// <summary>
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
/// </summary>
/// <param name="toEncrypt">string to be encrypted</param>
/// <param name="useHashing">use hashing? send to for extra secirity</param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
/// </summary>
/// <param name="cipherString">encrypted string</param>
/// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
/// <returns></returns>
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}