C #2.0 uses its own class to send and receive emails ................

Source: Internet
Author: User
Tags mailmessage smtpclient

Using system. net. mail;
Using system. Threading;

// ************************** Send an emailCode:
System. net. Mail. smtpclient client = new smtpclient ();
Client. Host = "smtp.qq.com ";
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential ("Account", "password ");
// Use an asterisk to change the password of your mailbox
Client. deliverymethod = smtpdeliverymethod. Network;

System. net. Mail. mailmessage message = new mailmessage ("uuu@qq.com", "uu@qq.com ");
Message. Subject = "test ";
Message. Body = "Use your own software to send emails! ";
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;

// Add an attachment
// Attachment DATA = new attachment (@ "attachment address: e: \ a.jpg", system. net. Mime. mediatypenames. application. octet );
// Message. attachments. Add (data );

Try
{
Client. Send (Message );
MessageBox. Show ("email successfully sent .");
}
Catch (exception ex)
{
MessageBox. Show ("failed to send email." + ex. tostring ());
}

// ***** Whether the email is successfully sent depends on whether the POP3 or SMTP function is allowed or enabled in the target email address ?? Check.

// ------------------------------------------- The code sent to the Gmail mailbox:
//***************------------------------------------------------------------------------

System. net. Mail. smtpclient client = new smtpclient ();
Client. Host = "smtp.gmail.com ";
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential ("Account", "password ");
// Use an asterisk to change the password of your mailbox
Client. deliverymethod = smtpdeliverymethod. Network;

system. net. mail. mailmessage message = new mailmessage ("hh00@gmail.com", "hh00@gmail.com");
message. subject = "test";
message. body = "emails sent with your own software! ";
message. bodyencoding = system. Text. encoding. utf8;
message. isbodyhtml = true;

client. enablessl = true; // ** after security authentication is enabled for gmial mailbox settings, set this attribute to true
// Add an attachment
// attachment DATA = new attachment (@ "attachment address: e: \ a.jpg ", system. net. mime. mediatypenames. application. octet);
// message. attachments. add (data);

Try
{
Client. Send (Message );
MessageBox. Show ("email successfully sent .");
}
Catch (exception ex)
{
MessageBox. Show ("failed to send email." + ex. tostring ());
}

//-----------------------------------------------------------

========================================================== ======================================

//********Send mail asynchronously by thread
Private void form2_load (Object sender, eventargs E)
{
Thread thread = new thread (New threadstart (updating ));
Thread. Start ();
}

private void updating ()
{< br> system. net. mail. smtpclient client = new smtpclient ();
client. host = "smtp.gmail.com";
client. usedefaultcredentials = false;
client. credentials = new system. net. networkcredential ("ddddd", "password");
// use an asterisk to change the password of your mailbox
client. deliverymethod = smtpdeliverymethod. network;

System. net. Mail. mailmessage message = new mailmessage ("ddddd@gmail.com", "ddddd@gmail.com ");
Message. Subject = "test 9999999999999999 ";
Message. Body = "Use your own software to send emails! ";
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;

Client. enablessl = true;

// Add an attachment
// Attachment DATA = new attachment (@ "attachment address: e: \ a.jpg", system. net. Mime. mediatypenames. application. octet );
// Message. attachments. Add (data );

Try
{
Client. Send (Message );
// MessageBox. Show ("email successfully sent .");
}
Catch (exception ex)
{
// MessageBox. Show ("failed to send email." + ex. tostring ());
}
}
========================================================== ======================================
// ************************** Code for receiving emails:

Related Article

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.