Use System.Web.Mail to send emails, suitable for .net1.1, please use System.Net.Mail for .net2.0
First quote System.Web
1. Send a simple email
[ 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);
[ VB.NET ] Dim mail As 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)
The smtpserver here can only be those smtp servers that do not require verification. Mailboxes such as 126, sina, yahoo, etc. all require verification, so they cannot be used. Using these email addresses to send letters will be discussed later.
2. Send Html email
[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 email body.<br><b>this part is in bold</b>";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send(mail);
[VB.NET]
Dim mail As 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 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 = "[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);
[ VB.NET ] Dim mail As 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."
Dim attachment As 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 names of the sender and recipient. For example, if the sender's address is [email protected], and we receive the letter using Outlook, [email protected] will be directly displayed in the From column.
Can you display a friendlier name in the From column?
For example, show 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);
[VB.NET]
Dim mail As 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. Send 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);
[ VB.NET ] Dim mail As 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 letters using an email address that requires Smtp verification. Now, in order to prevent spam, most Smtp servers require verification. The method for sending letters 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);
[VB.NET]
Dim mail As 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. The port of most SMTP servers is 25, but some are not at the same time. Most SMTP servers do not require SSL login, but some do. For example, Gmail, the SMTP port is: 465, and at the same time Support 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);
[VB.NET]
Dim mail As 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)