Note: Two namespaces need to be introduced.
Using system. net. mail;
Using system. net;
Procedure:
Private void button2_click (Object sender, eventargs E)
{
Application. Exit (); // exit the program
}
Private void button3_click (Object sender, eventargs E)
{
If (openfiledialog1.showdialog () = dialogresult. OK)
{
G_attachment.text = openfiledialog1.filename; // obtain the attachment.
}
}
Private void button#click (Object sender, eventargs E)
{
String mypsd = g_password.text;
String myemail = g_email.text.trim () + "@ gmail.com ";
Mailmessage mmsg = new mailmessage ();
Mmsg. Subject = g_subject.text.trim ();
Mmsg. From = new mailaddress (myemail );
Mmsg. to. Add (New mailaddress (g_address.text.trim ()));
Mmsg. isbodyhtml = true; // here isbodyhtml is enabled to support HTML in the content.
Mmsg. bodyencoding = encoding. utf8; // set the text encoding format to utf8.
Mmsg. Body = g_content.text;
Smtpclient sclient = new smtpclient ();
Sclient. Host = "smtp.gmail.com"; // SMTP address of Google
Sclient. Port = 587; // SMTP port of Google
Sclient. enablessl = true; // because Google uses an SSL (Secure Socket Layer) encrypted link, enablessl must be set to true.
Sclient. Credentials = new networkcredential (myemail, mypsd );
If (g_attachment.text.length> 0)
{
Mmsg. attachments. Add (new attachment (g_attachment.text); // you can check whether an attachment exists.
}
Try
{
Sclient. Send (mmsg );
MessageBox. Show ("the email has been sent successfully ");
}
Catch (exception ERR)
{
MessageBox. Show (ERR. message, "error prompt ");
}
}
Instance 2:
String to = "get@tom.com ";
String from = "set@tom.com ";
Mailmessage message = new mailmessage (from, );
Message. Subject = "using the new SMTP client test email .";
Message. Body = @ "test email .";
Smtpclient client = new smtpclient ("smtp.tom.com", 25 );
// Credentials are necessary if the server requires the client
// To authenticate before it will send e-mail on the client's behalf.
Client. Credentials = new networkcredential ("set@tom.com", "***"); // specify the user name and password for sending
Client. Send (Message );
MessageBox. Show ("sent successfully ");