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: