Single Mailbox bulk email program Email WinForm

Source: Internet
Author: User
Tags mailmessage smtpclient

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.Mime;
Using System.Net;
Using System.IO;
Using System.Data.OleDb;

Namespace Sendemail1._0
{
public partial class Mail:form
{
Public Mail ()
{

InitializeComponent ();
}
list<string> listemail = new list<string> ();
<summary>
Form loading
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Mail_load (object sender, EventArgs e)
{
Add the names of the two smpt servers
CMBBOXSMTP.ITEMS.ADD ("smtp.163.com");
CMBBOXSMTP.ITEMS.ADD ("SMTP. Qq.com ");
CMBBOXSMTP.ITEMS.ADD ("smtp.gmail.com");
Set as drop-down list
Cmbboxsmtp.dropdownstyle = ComboBoxStyle.DropDownList;
The first option is selected by default
Cmbboxsmtp.selectedindex = 1;
Add what you want to initialize below, such as display name, user name, etc.
}

private void Btn_send_click (object sender, EventArgs e)
{
Try
{
MessageBox.Show (SendEmail (Txtemail.text));
}
catch (Exception ex)
{

MessageBox.Show (ex. Message);
}
}

private string SendEmail (String toemail)
{
Try
{
Determine the SMTP server address. Instantiate an SMTP client
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient (cmbboxsmtp.text);
Generate a Send address
String strfrom = String. Empty;
if (Cmbboxsmtp.selectedindex = = 0)
{
Strfrom = txtUsername.Text + "@163.com";
}
else if (Cmbboxsmtp.selectedindex = = 1)
{
Strfrom = txtUsername.Text + "@QQ. com";
}
Else
{
Strfrom = txtUsername.Text + "@gmail. com";
}


Constructs a sender Address object
MailAddress from = new MailAddress (strfrom, Txtdisplayname.text, Encoding.UTF8);
Constructs a recipient Address object
MailAddress to = new MailAddress (Txtemail.text, Txttoname.text, Encoding.UTF8);

//Construct an email message object
MailMessage message = new MailMessage ();
message. To.add (Toemail);
//message. To.add ("[email protected]");
message. from = from;
//Add attachment to Message
foreach (TreeNode TreeNode in treeviewfilelist.nodes)
{
//Get file name
string filename = tr Eenode.text;
//Determine if the file exists
if (file.exists (fileName))
{
//constructs an Attachment object
Attachment attach = new Attachment (filename);
Get the information for the file
contentdisposition disposition = attach. Contentdisposition;
Disposition. CreationDate = System.IO.File.GetCreationTime (fileName);
Disposition. Modificationdate = System.IO.File.GetLastWriteTime (fileName);
Disposition. Readdate = System.IO.File.GetLastAccessTime (fileName);
//Add an attachment to a message
message. Attachments.Add (attach);
}
Else
{
MessageBox.Show ("file + FileName +" not found!) ");
}
}

Add message subject and content
Message. Subject = Txtsubject.text;
Message. subjectencoding = Encoding.UTF8;
Message. Body = Rtxtbody.text;
Message. bodyencoding = Encoding.UTF8;

Set information for a message
Client. Deliverymethod = Smtpdeliverymethod.network;
Message. bodyencoding = System.Text.Encoding.UTF8;
Message. Isbodyhtml = false;

If the server supports a secure connection, set the secure connection to true.
Gmail support, 163 not supported, if it is Gmail, be sure to set it to true
if (Cmbboxsmtp.selectedtext = = "Smpt.163.com")
Client. Enablessl = false;
Else
Client. Enablessl = true;

Set the user name and password.
String userState = message. Subject;
Client. useDefaultCredentials = false;
string username = txtUsername.Text;
string passwd = txtPassword.Text;
User login Information
NetworkCredential mycredentials = new NetworkCredential (username, passwd);
Client. Credentials = mycredentials;
Send mail
Client. Send (message);
Prompt to send successfully
Return "Success:" + Toemail;
MessageBox.Show ("sent successfully!");
}
catch (Exception ex)
{
Return "failed:" + toemail + "; Reason:" + ex. Message;
MessageBox.Show (ex. Message);
}
}

private void btnAdd_Click (object sender, EventArgs e)
{//define and initialize an object of the OpenFileDialog class
OpenFileDialog openFile = new OpenFileDialog ();
Openfile.initialdirectory = Application.startuppath;
Openfile.filename = "";
Openfile.restoredirectory = true;
Openfile.multiselect = false;

Displays the Open File dialog box and determines whether the OK button is clicked
if (openfile.showdialog () = = DialogResult.OK)
{
Get the selected file name
string fileName = Openfile.filename;
Add a file name to the TreeView
TREEVIEWFILELIST.NODES.ADD (FileName);
}

}

private void Btndelete_click (object sender, EventArgs e)
{
Determine if the node is selected
if (Treeviewfilelist.selectednode! = null)
{
Get the selected node
TreeNode Tempnode = Treeviewfilelist.selectednode;
Delete the selected node
TreeViewFileList.Nodes.Remove (Tempnode);
}
Else
{
MessageBox.Show ("Select the attachment you want to delete. ");
}
}

<summary>
Mass
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnsends_click (object sender, EventArgs e)
{
list<string> list = new list<string> ();

int sendnum = 0;
string s = txttoemails.text;
string[] Sarray = S.split (new char[] {' \ r ', ' \ n '});
for (int i = 0; i < sarray.length; i++)
{
Sarray[i] is the contents of each line
if (!string. IsNullOrEmpty (Sarray[i]))
{
List. ADD (Sarray[i]);
}

}
foreach (var item in list)
{
String str = SendEmail (item);
if (str. Split (': ') [0]. Equals ("Success"))
{
Sendnum + = 1;
Txtlog. Text + = str + System.Environment.NewLine;
}
Else
{
Txtlog. Text + = str + System.Environment.NewLine;
}

}
MessageBox.Show ("Send successfully" + sendnum + "message");


}
<summary>
Uploading Contacts
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnuptoemail_click (object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog ();
Openfile.initialdirectory = Application.startuppath;
Openfile.filename = "";
Openfile.restoredirectory = true;
Openfile.multiselect = false;

Displays the Open File dialog box and determines whether the OK button is clicked
if (openfile.showdialog () = = DialogResult.OK)
{
Get the selected file name
string fileName = Openfile.filename;
Add a file name to the TreeView
Txttoemails.text=filename;

DataSet ds = Exceltods (fileName);
foreach (DataRow cRow in DS. Tables[0]. Rows)
{
Txttoemails.text + = crow["Email"]. ToString () + System.Environment.NewLine;

}

}
}
<summary>
Read Excel
</summary>
<param name= "Path" ></param>
<returns></returns>
Public DataSet exceltods (string Path)
{
This connection can only manipulate Excel2007 (. xls) files
String strconn = "provider=microsoft.jet.oledb.4.0;" + "Data source=" + Path + ";" + "Extended properties=excel 8.0;";
This connection can manipulate the. xls and. xlsx files (connection strings that support Excel2003 and Excel2007)
Remark: "Hdr=yes;" Is that the first line of the Excel file is the column name instead of the data, "HDR=NO;" It's just the opposite of the front.
"Imex=1" If the data type in the column is inconsistent, the use of "imex=1" will avoid data type collisions.
String strconn = "provider=microsoft.ace.oledb.12.0;" + "Data source=" + Path + "; Extended properties= ' Excel 12.0; Hdr=yes; Imex=1 ' ";
OleDbConnection conn = new OleDbConnection (strconn);
Conn. Open ();
String strexcel = "";
OleDbDataAdapter mycommand = null;
DataSet ds = null;
Strexcel = "SELECT * FROM [sheet1$]";
mycommand = new OleDbDataAdapter (Strexcel, strconn);
ds = new DataSet (); Mycommand.fill (ds, "Table1");
return DS;
}
}
}

Single Mailbox bulk email program Email WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.