Introduction to smtpclient

Source: Internet
Author: User
Tags mailmessage smtpclient

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 ");

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.