Send Mail | tips
Send mail with System.Web.Mail, apply to. net1.1,.net2.0 Please use System.Net.Mail
Refer to System.Web first
1, send Simple Mail
[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 the I test email body"; Smtpmail.smtpserver = "localhost"; Your real server goes heresmtpmail.send (mail);
[VB.net]
Dim Mail as 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 heresmtpmail.send (mail) SMTP The server can only be those SMTP servers that do not need to be authenticated, such as mailboxes for 126,sina,yahoo, and so on, which require authentication and therefore cannot be used. Use these mailboxes to post to 2, send HTML mail [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 ' in bold</b> '; Smtpmail.smtpserver = "localhost"; Your real server goes heresmtpmail.send (mail);
[VB.net] Dim Mail as New mailmessage () mail. to = "me@mycompany.com" Mail. from = "you@yourcompany.com" Mail. Subject = "This is a test email." Mail. BodyFormat = MailFormat.Htmlmail.Body = "This is Me test email body.<br><b>this part is in bold</b>" Smtp Mail.smtpserver = "localhost" ' your Real server goes heresmtpmail.send (mail)
3, send the attachment
[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 the I test email body." MailAttachment attachment = new MailAttachment (Server.MapPath ("test.txt")); Create the Attachmentmail. Attachments.Add (attachment); Add the Attachmentsmtpmail.smtpserver = "localhost"; Your real server goes heresmtpmail.send (mail);
[VB.net]
Dim Mail as New mailmessage () mail. to = "me@mycompany.com" Mail. from = "you@yourcompany.com" Mail. Subject = "This is a test email." Mail. BODY = ' This is me test email body. ' Dim Attachment as New mailattachment (Server.MapPath ("test.txt")) ' Create the Attachmentmail. Attachments.Add (Attachment) ' Add the attachmentsmtpmail.smtpserver = ' localhost ' your real server goes Heresmtpmail.send (mail)
4, modify the sender and the recipient's name, such as the sender's address is abc@126.com, we use Outlook to receive the letter, from the column will directly display abc@126.com. Can you show a friendly name in the From column? For example, the Tony Gong method is shown as follows: [C #] MailMessage mail = new MailMessage (), mail. to = "\" john\ "<"; mail. From ' >me@mycompany.com> ', mail. from = "\" Tony gong\ "<"; mail. Subject ' >you@yourcompany.com> '; mail. Subject = "This is a test email."; Mail. BODY = "This is the I test email body." Smtpmail.smtpserver = "localhost"; Your real server goes heresmtpmail.send (mail);
[VB.net] Dim Mail as New mailmessage () mail. to = "" John "<" mail. From ' >me@mycompany.com> ' mail. from = "" "Tony Gong" "<" mail. Subject ' >you@yourcompany.com> ' mail. Subject = "This is a test email." Mail. BODY = ' This is me test email body. ' Smtpmail.smtpserver = "localhost" ' your Real server goes heresmtpmail.send (mail)
5, sent to more than one person
[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 the I test email body." Smtpmail.smtpserver = "localhost"; Your real server goes heresmtpmail.send (mail);
[vb.net]
Dim Mail as 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 me test email body. ' Smtpmail.smtpserver = "localhost" ' your Real server goes heresmtpmail.send (mail)
6, send a letter with a mailbox that requires SMTP authentication
Now, to prevent spam, most SMTP servers need to verify
The method of sending the letter 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);
[VB.net]
Dim Mail as 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 port of the SMTP server and use SSL to encrypt the port of most SMTP servers is 25, but some are not at the same time, most SMTP servers do not require SSL landing, and some need such as the GMAIL,SMTP port is: 465, while supporting the SSL 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); [VB.net] Dim Mail as 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)