C # source code of the mail sending component
// ================================================ ======================================
// File: mailsender. CS
// Mail sending component
// Supports ESMTP and multiple attachments
// ================================================ ======================================
Namespace jcpersonal. Utility
{
Using system;
Using system. collections;
Using system. net. Sockets;
Using system. IO;
Using system. text;
/// <Summary>
/// Mail sender
/// </Summary>
Public class mailsender
{
/// <Summary>
/// SMTP server Domain Name
/// </Summary>
Public String server {
Get {return server ;}
Set {If (value! = Server) Server = value ;}
} Private string Server = "";
/// <Summary>
/// SMTP server port [25 by default]
/// </Summary>
Public int port {
Get {return port ;}
Set {If (value! = Port) Port = value ;}
} Private int Port = 25;
/// <Summary>
/// User name [If authentication is required]
/// </Summary>
Public String username {
Get {return username ;}
Set {If (value! = Username) username = value ;}
} Private string username = "";
/// <Summary>
/// Password [If authentication is required]
/// </Summary>
Public String password {
Get {return password ;}
Set {If (value! = PASSWORD) Password = value ;}
} Private string Password = "";
/// <Summary>
/// Sender address
/// </Summary>
Public String from {
Get {return from ;}
Set {If (value! = From) from = value ;}
} Private string from = "";
/// <Summary>
/// Recipient address
/// </Summary>
Public string {
Get {return ;}
Set {If (value! = To) to = value ;}
} Private string to = "";
/// <Summary>
/// Sender's name
/// </Summary>
Public String fromname {
Get {return fromname ;}
Set {If (value! = Fromname) fromname = value ;}
} Private string fromname = "";
/// <Summary>
/// Recipient's name
/// </Summary>
Public String toname {
Get {return toname ;}
Set {If (value! = Toname) toname = value ;}
} Private string toname = "";
/// <Summary>
/// Subject of the email
/// </Summary>
Public String subject {
Get {return subject ;}
Set {If (value! = Subject) Subject = value ;}
} Private string subject = "";
/// <Summary>
/// Body of the email
/// </Summary>
Public String body {
Get {return body ;}
Set {If (value! = Body) Body = value ;}
} Private string body = "";
/// <Summary>
/// The text of an email in hypertext format
/// </Summary>
Public String htmlbody {
Get {return htmlbody ;}
Set {If (value! = Htmlbody) htmlbody = value ;}
} Private string htmlbody = "";
/// <Summary>
/// Whether the email is in HTML Format
/// </Summary>
Public bool ishtml {
Get {return ishtml ;}
Set {If (value! = Ishtml) ishtml = value ;}
} Private bool ishtml = false;
/// <Summary>
/// Language encoding [gb2312 by default]
/// </Summary>
Public String languageencoding {
Get {return languageencoding ;}
Set {If (value! = Languageencoding) languageencoding = value ;}
} Private string languageencoding = "gb2312 ";
/// <Summary>
/// Email encoding [8bit by default]
/// </Summary>
Public String mailencoding {
Get {return encoding ;}
Set {If (value! = Encoding) encoding = value ;}
} Private string encoding = "8bit ";
/// <Summary>
/// Mail priority [3 by default]
/// </Summary>
Public int priority {
Get {return priority ;}
Set {If (value! = Priority) Priority = value ;}
} Private int priority = 3;
/// <Summary>
/// Attachment [attachmentinfo]
/// </Summary>
Public ilist attachments {
Get {return attachments ;}
// Set {If (value! = Attachments) attachments = value ;}
} Private arraylist attachments = new arraylist ();
/// <Summary>
/// Send an email
/// </Summary>
Public void Sendmail ()
{
// Create a tcpclient object and establish a connection
Tcpclient TCP = NULL;
Try
{
TCP = new tcpclient (server, Port );
}
Catch (exception)
{
Throw new exception ("unable to connect to the server ");
}
Readstring (TCP. getstream (); // gets the connection information
// Start Server Authentication
// If the status code is 250, the operation is successful.
If (! Command (TCP. getstream (), "ehlo localhost", "250 "))
Throw new exception ("Logon Failed ");
If (username! = "")
{
// Authentication required
If (! Command (TCP. getstream (), "auth login", "334 "))
Throw new exception ("authentication failed ");
String nameb64 = tobase64 (username); // convert username to base64 code
If (! Command (TCP. getstream (), nameb64, "334 "))
Throw new exception ("authentication failed ");
String passb64 = tobase64 (password); // convert the password to a base64 code.
If (! Commands (TCP. getstream (), passb64, "235 "))
Throw new exception ("authentication failed ");
}
// Prepare for sending
Writestring (TCP. getstream (), "mail from:" + from );
Writestring (TCP. getstream (), "rcpt to:" + );
Writestring (TCP. getstream (), "data ");
// Send the mail header
Writestring (TCP. getstream (), "date:" + datetime. Now); // time
Writestring (TCP. getstream (), "From:" + fromname + "<" + from + ">"); // sender
Writestring (TCP. getstream (), "Subject:" + subject); // topic
Writestring (TCP. getstream (), "to:" + toname + "<" + to + ">"); // recipient
// Mail format
Writestring (TCP. getstream (), "Content-Type: multipart/mixed; boundary =" unique-boundary-1 "");
Writestring (TCP. getstream (), "reply-to:" + from); // reply address
Writestring (TCP. getstream (), "x-priority:" + priority); // priority
Writestring (TCP. getstream (), "mime-version: 1.0"); // MIME Version
// Data ID, random
// Writestring (TCP. getstream (), "message-ID:" + datetime. Now. tofiletime () + "@ security.com ");
Writestring (TCP. getstream (), "content-transfer-encoding:" + encoding); // Content Encoding
Writestring (TCP. getstream (), "x-mailer: jcpersonal. Utility. mailsender"); // email sender
Writestring (TCP. getstream (),"");
Writestring (TCP. getstream (), tobase64 ("this is a multi-part message in MIME format ."));
Writestring (TCP. getstream (),"");
// Separate input from this start
Writestring (TCP. getstream (), "-- unique-boundary-1 ");
// Define the second separator here
Writestring (TCP. getstream (), "Content-Type: multipart/alternative; boundary =" unique-boundary-2 "");
Writestring (TCP. getstream (),"");
If (! Ishtml)
{
// Text information
Writestring (TCP. getstream (), "-- unique-boundary-2 ");
Writestring (TCP. getstream (), "Content-Type: text/plain; charset =" + languageencoding );
Writestring (TCP. getstream (), "content-transfer-encoding:" + encoding );
Writestring (TCP. getstream (),"");
Writestring (TCP. getstream (), body );
Writestring (TCP. getstream (), ""); // after a part is written, it is written as null information and segmented.
Writestring (TCP. getstream (), "-- unique-boundary-2 --"); // The ending symbol of the separator. The tail is followed by more --
Writestring (TCP. getstream (),"");
}
Else
{
// Html Information
Writestring (TCP. getstream (), "-- unique-boundary-2 ");
Writestring (TCP. getstream (), "Content-Type: text/html; charset =" + languageencoding );
Writestring (TCP. getstream (), "content-transfer-encoding:" + encoding );
Writestring (TCP. getstream (),"");
Writestring (TCP. getstream (), htmlbody );
Writestring (TCP. getstream (),"");
Writestring (TCP. getstream (), "-- unique-boundary-2 --"); // The ending symbol of the separator. The tail is followed by more --
Writestring (TCP. getstream (),"");
}
// Send the attachment
// Looping the file list
For (INT I = 0; I <attachments. Count; I ++)
{
Writestring (TCP. getstream (), "-- unique-boundary-1"); // mail content Separator
Writestring (TCP. getstream (), "Content-Type: Application/octet-stream; name =" "+ (attachmentinfo) attachments ). filename + "); // File Format
Writestring (TCP. getstream (), "content-transfer-encoding: base64"); // Content Encoding
Writestring (TCP. getstream (), "content-Disposition: attachment; filename =" "+ (attachmentinfo) attachments). filename +" "); // file name
Writestring (TCP. getstream (),"");
Writestring (TCP. getstream (), (attachmentinfo) attachments). bytes); // write the File Content
Writestring (TCP. getstream (),"");
}
Command (TCP. getstream (), ".", "250"); // enter "."
// Close the connection
TCP. Close ();
}
/// <Summary>
/// Write characters to the stream
/// </Summary>
/// <Param name = "netstream"> stream from tcpclient </param>
/// <Param name = "str"> written characters </param>
Protected void writestring (networkstream netstream, string Str)
{
STR = STR + ""; // Add a linefeed
// Convert the command line to byte []
Byte [] bwrite = encoding. getencoding (languageencoding). getbytes (Str. tochararray ());
// Because the size of data written each time is limited, we set the length of the data written each time to 75 bytes. Once the command length exceeds 75, the data is written step by step.
Int start = 0;
Int length = bwrite. length;
Int page = 0;
Int size = 75;
Int COUNT = size;
Try
{
If (length> 75)
{
// Data Paging
If (length/size) * size <length)
Page = length/size + 1;
Else
Page = length/size;
For (INT I = 0; I <page; I ++)
{
Start = I * size;
If (I = page-1)
Count = length-(I * size );
Netstream. Write (bwrite, start, count); // write data to the server
}
}
Else
Netstream. Write (bwrite, 0, bwrite. Length );
}
Catch (exception)
{
// Ignore errors
}
}
/// <Summary>
/// Read characters from the stream
/// </Summary>
/// <Param name = "netstream"> stream from tcpclient </param>
/// <Returns> read characters </returns>
Protected string readstring (networkstream netstream)
{
String sp = NULL;
Byte [] by = new byte [1024];
Int size = netstream. Read (by, 0, by. Length); // reads data streams
If (size> 0)
{
SP = encoding. Default. getstring (by); // convert to string
}
Return sp;
}
/// <Summary>
/// Issue a command and determine whether the returned information is correct
/// </Summary>
/// <Param name = "netstream"> stream from tcpclient </param>
/// <Param name = "command"> command </param>
/// <Param name = "state"> correct status code </param>
/// <Returns> correct </returns>
Protected bool command (networkstream netstream, string command, string state)
{
String sp = NULL;
Bool success = false;
Try
{
Writestring (netstream, command); // write command
SP = readstring (netstream); // accept the returned information
If (sp. indexof (state )! =-1) // check whether the status code is correct
Success = true;
}
Catch (exception)
{
// Ignore errors
}
Return success;
}
/// <Summary>
/// The string is encoded as base64.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> base64 encoded string </returns>
Protected string tobase64 (string Str)
{
Try
{
Byte [] by = encoding. Default. getbytes (Str. tochararray ());
STR = convert. tobase64string ();
}
Catch (exception)
{
// Ignore errors
}
Return STR;
}
/// <Summary>
/// Attachment Information
/// </Summary>
Public struct attachmentinfo
{
/// <Summary>
/// File Name of the attachment [if the input path is used, it is automatically converted to the file name]
/// </Summary>
Public String filename {
Get {return filename ;}
Set {filename = path. getfilename (value );}
} Private string filename;
/// <Summary>
/// The content of the attachment [consists of base64-encoded bytes]
/// </Summary>
Public String bytes {
Get {return bytes ;}
Set {If (value! = Bytes) bytes = value ;}
} Private string bytes;
/// <Summary>
/// Read the attachment content from the stream and construct
/// </Summary>
/// <Param name = "ifilename"> File Name of the attachment </param>
/// <Param name = "stream"> stream </param>
Public attachmentinfo (string ifilename, stream Stream)
{
Filename = path. getfilename (ifilename );
Byte [] by = new byte [stream. Length];
Stream. Read (by, 0, (INT) stream. Length); // read the file content
// Format conversion
Bytes = convert. tobase64string (by); // convert to base64 encoding
}
/// <Summary>
/// Construct the Attachment based on the given bytes
/// </Summary>
/// <Param name = "ifilename"> File Name of the attachment </param>
/// <Param name = "ibytes"> content of the attachment [byte] </param>
Public attachmentinfo (string ifilename, byte [] ibytes)
{
Filename = path. getfilename (ifilename );
Bytes = convert. tobase64string (ibytes); // convert to base64 encoding
}
/// <Summary>
/// Load and construct a file
/// </Summary>
/// <Param name = "path"> </param>
Public attachmentinfo (string path)
{
Filename = path. getfilename (PATH );
Filestream file = new filestream (path, filemode. Open );
Byte [] by = new byte;
File. Read (by, 0, (INT) file. Length); // read the file content
// Format conversion
Bytes = convert. tobase64string (by); // convert to base64 encoding
File. Close ();
}
}
}
}
--------------------------------------------------------------------------------
// Use:
Mailsender MS = new mailsender ();
Ms. From = "jovenc@tom.com ";
Ms. To = "jovenc@citiz.net ";
Ms. Subject = "subject ";
Ms. Body = "body text ";
Ms. Username = "#######"; // how can I tell you?
Ms. Password = "*********"; // how can I tell you?
Ms. Server = "smtp.tom.com ";
Ms. attachments. Add (New mailsender. attachmentinfo (@ "D: est.txt "));
Console. writeline ("mail sending ...");
Try
{
Ms. Sendmail ();
Console. writeline ("mail sended .");
}
Catch (exception E)
{
Console. writeline (E );
}