Use System. Web. Mail to send emails. For. net1.1 and. net2.0, use system. net. Mail.
First reference system. Web
1. Send a simple email
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "me@mycompany.com ";
Mail. From = "you@yourcompany.com ";
Mail. Subject = "this is a test email .";
Mail. Body = "this is my test email body ";
Smtpmail. smtpserver = "localhost"; // your real server goes here
Smtpmail. Send (Mail );
The smtpserver can only be SMTP servers that do not require verification, such as 126, Sina, Yahoo, and so on. Use these mailboxes to send emails.
2. Send an HTML email
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "me@mycompany.com ";
Mail. From = "you@yourcompany.com ";
Mail. Subject = "this is a test email .";
Mail. bodyformat = mailformat. html;
Mail. Body = "this is my test email body. <br> <B> This part is in bold </B> ";
Smtpmail. smtpserver = "localhost"; // your real server goes here
Smtpmail. Send (Mail );
3. Send attachments
[C #]
mailmessage mail = new mailmessage ();
mail. to = "me@mycompany.com";
mail. from = "you@yourcompany.com";
mail. subject = "this is a test email. ";
mail. body = "this is my test email body. ";
mailattachment attachment = new mailattachment (server. mappath ("test.txt"); // create the attachment
mail. attachments. add (Attachment); // Add the attachment
smtpmail. smtpserver = "localhost"; // your real server goes here
smtpmail. send (Mail);
4. Modify the sender and recipient names.
For example, if the sender's address is a abc@126.com, we receive a message using Outlook, And the abc@126.com will be displayed directly in the from column.
Can I display a friendly name in the from column?
For example, Tony Gong
The method is as follows:
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "\" John \ "<me@mycompany.com> ";
Mail. From = "\" Tony Gong \ "<you@yourcompany.com> ";
Mail. Subject = "this is a test email .";
Mail. Body = "this is my test email body .";
Smtpmail. smtpserver = "localhost"; // your real server goes here
Smtpmail. Send (Mail );
5. Send to multiple users
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "me@mycompany.com; him@hiscompany.com; her@hercompany.com ";
Mail. From = "you@yourcompany.com ";
Mail. Subject = "this is a test email .";
Mail. Body = "this is my test email body .";
Smtpmail. smtpserver = "localhost"; // your real server goes here
Smtpmail. Send (Mail );
6. Send a mail with an email address that requires SMTP Verification
To prevent spam, most SMTP servers need to verify
The mail method is as follows:
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "me@mycompany.com ";
Mail. From = "abc@126.com ";
Mail. Subject = "this is a test email .";
Mail. Body = "some text goes here ";
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic Authentication
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "ABC"); // set your username here
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); // set your password here
Smtpmail. smtpserver = "smtp.126.com"; // your real server goes here
Smtpmail. Send (Mail );
7. Modify the SMTP server port and use SSL encryption
Most SMTP servers use port 25, but some are not
At the same time, most SMTP servers do not require SSL login, but some require
For example, for Gmail, the SMTP port is 465, and SSL is also supported.
Code As follows:
[C #]
Mailmessage mail = new mailmessage ();
Mail. To = "me@mycompany.com ";
Mail. From = "abc@126.com ";
Mail. Subject = "this is a test email .";
Mail. Body = "some text goes here ";
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic Authentication
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "ABC"); // set your username here
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); // set your password here
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465 );
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true ");
Smtpmail. smtpserver = "smtp.126.com"; // your real server goes here
Smtpmail. Send (Mail );