NET System.Web.Mail sending mail Use System.Web.Mail to send mail for. net1.1. net2.0 Please use System.Net.Mail Refer to System.Web first 1. Send a simple message [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]"; Mail. from = "[email protected]"; 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); Here the SmtpServer can only be those who do not need to verify the SMTP server, such as 126,sina,yahoo and so on the mailbox, all need to verify, so it is not available. Using these mailboxes, we'll talk about them later. 2, send HTML mail [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]"; Mail. from = "[email protected]"; Mail. Subject = "This is a test email."; Mail. BodyFormat = mailformat.html; Mail. Body = "This is my test e-mail body.<br><b>this part was in bold</b>"; Smtpmail.smtpserver = "localhost"; Your real server goes here Smtpmail.send (mail); 3, send attachment [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]"; Mail. from = "[email protected]"; 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, the sender's address is [email protected], we use Outlook to receive the letter, from a column will be displayed directly [email protected] Can you show a friendly name in the From column? Like showing Tony Gong. Here's how: [C #] MailMessage mail = new MailMessage (); Mail. to = "\" john\ "<[email protected]>"; Mail. from = "\" Tony gong\ "<[email protected]>"; 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, sent to multiple people [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]; [Email protected]; [email protected] "; Mail. from = "[email protected]"; 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 letter to the mailbox that requires SMTP authentication To prevent spam, the vast majority of SMTP servers need to verify The sending method is as follows: [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]"; Mail. from = "[email protected]"; 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 port of the SMTP server and use SSL encryption Most SMTP servers have ports of 25, but some are not At the same time, most SMTP servers do not require SSL login, while others require For example, the GMAIL,SMTP port is: 465, while supporting SSL The code is as follows: [C #] MailMessage mail = new MailMessage (); Mail. to = "[email protected]"; Mail. from = "[email protected]"; 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); |