Send Email To Multiple Recipients In C#, VB, C++/CLI, JScript.NET
Có thể bạn quan tâm
Initializes a new instance of the AddressCollection class.
[Visual Basic] Public Sub New() Public Sub New( _ addresses As String _ ) [C#] public AddressCollection(); public AddressCollection( string addresses ); [C++] public: AddressCollection(); public: AddressCollection( String^ addresses ); [JScript] public function AddressCollection(); public function AddressCollection( addresses : String );Parameters
addresses A comma or semicolon-delimited list of e-mail addressesExample
[Visual Basic, C#, C++, JScript.NET] The following example demonstrates how to send email with EASendMail SMTP Component, but it doesn't demonstrates the events usage. To get the full samples of EASendMail, please refer to Samples section.
[VB - Send Email Example] Imports EASendMail Public Sub SendMail(sender As String, toRecipients As String, ccRecipients As String, subject As String, bodyText As String, server As String, user As String, password As String, useSsl As Boolean) Try ' From is a MailAddress object, it also supports implicit converting from string directly. ' The syntax is like this: "[email protected]" or "Tester <[email protected]>" ' oMail.From = New MailAddress("Tester", "[email protected]") ' oMail.From = New MailAddress("Tester <[email protected]>") ' oMail.From = New MailAddress("[email protected]") ' To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string directly. ' multiple addresses should be separated with (,;) ' The syntax is like this: "[email protected], [email protected]" ' oMail.To = New AddressCollection("[email protected], [email protected]") ' oMail.To = New AddressCollection("Tester1 <[email protected]>, Tester2 <[email protected]>") ' You can add more recipient by Add method ' oMail.To.Add(New MailAddress("tester", "[email protected]")) Dim oMail As SmtpMail = New SmtpMail("TryIt") oMail.From = New MailAddress(sender) oMail.To = New AddressCollection(toRecipients) oMail.Cc = New AddressCollection(ccRecipients) oMail.Subject = subject ' If bodyText contains html tags, please use ' oMail.HtmlBody = bodyText oMail.TextBody = bodyText ' Add attachment ' oMail.AddAttachment("c:\test.gif") ' Set server address Dim oServer As SmtpServer = New SmtpServer(server) If user.Length > 0 And password.Length > 0 Then ' Set user/password for ESMTP authentication oServer.User = user oServer.Password = password End If ' Most mordern SMTP servers require SSL/TLS connection now. ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = If(useSsl, SmtpConnectType.ConnectSSLAuto, SmtpConnectType.ConnectTryTLS) Dim oSmtp As SmtpClient = New SmtpClient() ' To generate a log file for SMTP transaction, please use ' oSmtp.LogFileName = "c:\smtp.txt" oSmtp.SendMail(oServer, oMail) Console.WriteLine("The message has been submitted to server successfully!") Catch ex As Exception Console.WriteLine("Exception: {0}", ex.Message) End Try End Sub [C# - Send Email Example] using System; using System.Collections; using EASendMail; public static void SendMail(string sender, string toRecipients, string ccRecipients, string subject, string bodyText, string server, string user, string password, bool useSsl) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "[email protected]" or "Tester <[email protected]>" // oMail.From = new MailAddress("Tester", "[email protected]"); // oMail.From = new MailAddress("Tester <[email protected]>"); // oMail.From = new MailAddress("[email protected]"); // To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple addresses should be separated with (,;) // The syntax is like this: "[email protected], [email protected]" // oMail.To = new AddressCollection("[email protected], [email protected]"); // oMail.To = new AddressCollection("Tester1<[email protected]>, Tester2<[email protected]>"); // You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "[email protected]")); SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = sender; oMail.To = toRecipients; oMail.Cc = ccRecipients; oMail.Subject = subject; // If bodyText contains the html tags, please use // oMail.HtmlBody = bodyText; oMail.TextBody = bodyText; // Add attachment // oMail.AddAttachment("c:\\test.gif"); // Set server address SmtpServer oServer = new SmtpServer(server); if (user.Length != 0 && password.Length != 0) { // Set user/password for ESMTP authentication oServer.User = user; oServer.Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = (useSsl) ? SmtpConnectType.ConnectSSLAuto : SmtpConnectType.ConnectTryTLS; SmtpClient oSmtp = new SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp.LogFileName = "c:\\smtp.txt"; oSmtp.SendMail(oServer, oMail); Console.WriteLine("The message has been submitted to server successfully!"); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } [C++/CLI - Send Email Example] using namespace System; using namespace System::Collections; using namespace EASendMail; static void SendMail(String ^sender, String ^toRecipients, String ^ccRecipients, String ^subject, String ^bodyText, String ^server, String ^user, String ^password, bool useSsl) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "[email protected]" or "Tester <[email protected]>" // oMail->From = gcnew MailAddress("Tester", "[email protected]"); // oMail->From = gcnew MailAddress("Tester <[email protected]>"); // oMail->From = gcnew MailAddress("[email protected]"); //To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple address should be separated with (,;) //The syntax is like this: "[email protected], [email protected]" // oMail->To = gcnew AddressCollection("[email protected], [email protected]"); // oMail->To = gcnew AddressCollection("Tester1 <[email protected]>, Tester2 <[email protected]>"); // You can add more recipient by Add method // oMail->To->Add(gcnew MailAddress("tester", "[email protected]")); SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew EASendMail::MailAddress(sender); oMail->To = gcnew EASendMail::AddressCollection(toRecipients); oMail->Cc = gcnew EASendMail::AddressCollection(ccRecipients); oMail->Subject = subject; // If bodyText contains html tags, please use // oMail->HtmlBody = bodyText; oMail->TextBody = bodyText; // Add attachment // oMail->AddAttachment("c:\\test.gif"); // Set server address SmtpServer ^oServer = gcnew SmtpServer(server); if (user->Length != 0 && password->Length != 0) { // Set user/password for ESMTP authentication oServer->User = user; oServer->Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = (useSsl) ? SmtpConnectType::ConnectSSLAuto : SmtpConnectType::ConnectTryTLS; SmtpClient ^oSmtp = gcnew SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp->LogFileName = "c:\\smtp.txt"; oSmtp->SendMail(oServer, oMail); Console::WriteLine("The message has been submitted to server successfully!"); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } } [JScript.NET - Send Email Example] public function SendMail(sender:String, toRecipients:String, ccRecipients:String, subject:String, bodyText:String, server:String, user:String, password:String, useSsl:Boolean) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "[email protected]" or "Tester <[email protected]>" // oMail.From = new MailAddress("Tester", "[email protected]"); // oMail.From = new MailAddress("Tester<[email protected]>"); // oMail.From = new MailAddress("[email protected]"); // To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple addresses should be separated with (,;) // The syntax is like this: "[email protected], [email protected]" // oMail.To = new AddressCollection("[email protected], [email protected]"); // oMail.To = new AddressCollection("Tester1 <[email protected]>, Tester2 <[email protected]>"); // You can add more recipient by Add method // oMail.To.Add(new MailAddress("tester","[email protected]")); var oMail:SmtpMail = new SmtpMail("TryIt"); oMail.From = new MailAddress(sender); oMail.To = new AddressCollection(toRecipients); oMail.Cc = new AddressCollection(ccRecipients); oMail.Subject = subject; // If bodyText contains html tags, please use // oMail.HtmlBody = bodyText; oMail.TextBody = bodyText; // Add attachment // oMail.AddAttachment("c:\\test.gif"); // Set server address var oServer:SmtpServer = new SmtpServer(server); if(user.Length != 0 && password.Length != 0) { // Set user/password for ESMTP authentication oServer.User = user; oServer.Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = (useSsl)? SmtpConnectType.ConnectSSLAuto : SmtpConnectType.ConnectTryTLS; var oSmtp:SmtpClient = new SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp.LogFileName = "c:\\smtp.txt"; oSmtp.SendMail(oServer, oMail); Console.WriteLine("The message has been submitted to server successfully!"); } catch(exp:System.Exception) { Console.WriteLine("Exception: {0}", exp.Message); } }See Also
SmtpMail.To Property SmtpMail.Cc Property SmtpMail.Bcc Property
Từ khóa » Visual Basic Send Email Multiple Recipients
-
Sending Email To Multiple Recipients In VB.NET - Stack Overflow
-
Thread: Sending Email To Multiple Addresses - VBForums
-
[Solved] Send Mail To Multiple Recipient - CodeProject
-
.Net 2.0 Send Email With Multiple “To” Recipients - MSDN - Microsoft
-
Sending Email To Multiple Recipients Using ASP.Net - C# Corner
-
Send Email From Excel To Multiple Recipients Using VBA And Outlook
-
Send Email To Multiple Recipients Using VBA | MrExcel Message Board
-
How To Send Email To Multiple Recipients In A List From Excel Via ...
-
Outlook Send Same Email To Different Recipients | VBA Macro #31
-
Sending Email To Multiple Recipients, Vba - Excel
-
How To Send Email To Multiple Recipients With Addresses Stored ...
-
Sending Emails To Multiple Recipients - Visual Basic .NET
-
Send Email To Multiple Recipients | Blog - Limilabs