Monthly Archives: September 2010

Sending Mail to Multiple Recipients

This topic illustrates how to compose an email message and send it to multiple recipients with a single line of code. ComponentPro UltimateMail makes sending to multiple recipients easy to manage, by representing these types as collections of the MailMessage object.

If you are using the component to send emails through Gmail SMTP, you need to set the server name to smtp.gmail.com, port to 465, and security mode to Implicit. The following image shows you the configuration in the SMTP Send Mail sample project:

Ultimate SMTP Send Mail

Using Ultimate Mail Library to send mails:

First, you need to connect to the server with a specified port and security mode, authenticate the user if required and call the Send method to send the message to the recipients. Before using the SmtpClient class, you need to add using directive for the namespace ComponentSoft.Net. The following example takes advantage of the easy-to-use features of the library.

C#

const string serverName = “myserver”;
const string user = name@domain.com;
const string password = “mytestpassword”;
const int port = 465;
const SecurityMode securityMode = SecurityMode.Implicit;
SmtpClient client =
 new SmtpClient();
try
{
MailMessage mmMessage = new MailMessage();
mmMessage.From.Add(
from@thedomain.com);
mmMessage.To.Add(
name@domain.com);
mmMessage.To.Add(
someone@domain.com);
mmMessage.Cc.Add(
someone2@domain.com);
mmMessage.Bcc.Add(
someone3@domain.com);
mmMessage.Subject =
 “Test Subject”;
mmMessage.BodyText =
 “Test Content”;
Console.WriteLine(
“Connecting SMTP server: {0}:{1}…”, serverName, port);
// Connect to the server. (i.e. componentsoft.net)
client.Connect(serverName, port, securityMode);
// Login to the server.
Console.WriteLine(“Logging in as {0}…”, user);
client.Authenticate(user, password);
Console.WriteLine(
“Sending mail message…”);
client.Send(mmMessage);
Console.WriteLine(
“Message sent…”);
// Disconnect.
Console.WriteLine(“Disconnecting…”);
client.Disconnect();
}
catch (SmtpException smtpExc)
{
MessageBox.Show(
string.Format(“An SMTP error occurred: {0}, ErrorStatus: {1}”, smtpExc.Message, smtpExc.Status));
}
catch (Exception exc)
{
MessageBox.Show(
string.Format(“An error occurred: {0}”, exc.Message));
}

VB.NET

Const serverName As String = “myserver”
Const user As String = “name@domain.com”
Const password As String = “mytestpassword”
Const port As Integer = 465
Const securityMode As SecurityMode = securityMode.Implicit
Dim client As New SmtpClient()
Try
Dim mmMessage As New MailMessage()
mmMessage.From.Add(“from@thedomain.com”)
mmMessage.To.Add(“name@domain.com”)
mmMessage.To.Add(“someone@domain.com”)
mmMessage.Cc.Add(“someone2@domain.com”)
mmMessage.Bcc.Add(“someone3@domain.com”)
mmMessage.Subject = “Test Subject”
mmMessage.BodyText = “Test Content”
Console.WriteLine(“Connecting SMTP server: {0}:{1}…”, serverName, port)
‘ Connect to the server.
client.Connect(serverName, port, securityMode)
‘ Login to the server.
Console.WriteLine(“Logging in as {0}…”, user)
client.Authenticate(user, password)
Console.WriteLine(“Sending mail message…”)
client.Send(mmMessage)
Console.WriteLine(“Message sent…”)
‘ Disconnect.
Console.WriteLine(“Disconnecting…”)
client.Disconnect()
Catch smtpExc As SmtpException
MessageBox.Show(String.Format(“An SMTP error occurred: {0}, ErrorStatus: {1}”, smtpExc.Message, smtpExc.Status))
Catch exc As Exception
MessageBox.Show(String.Format(“An error occurred: {0}”, exc.Message))
End Try

Handling Bounced Message on a POP3 Account with Ultimate Bounce Inspector

You can download and process messages from either IMAP server or POP3 server easily with Ultimate Bounce Inspector component. To download and process messages from a POP3 server, you can use the following code:

Downloading and processing multiple EML files from a POP3 server

  1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
    C#  
    using ComponentSoft.Net.Mail;
    VB.NET  
    Imports ComponentSoft.Net.Mail
  2. Create a new instance of the Pop3Client class.
  3. Connect to the POP3 server with the Connect methods and authenticate the user with the Authenticate method. The code looks similar to the following:
    C#  
    // POP3 server information.
    const string serverName = “myserver”;
    const string user = name@domain.com;
    const string password = “mytestpassword”;
    const int port = 995;
    const SecurityMode securityMode = SecurityMode.Implicit;
    // Create a new instance of the Pop3Client class.
    Pop3Client client = new Pop3Client();
    Console.WriteLine(
    “Connecting Pop3 server: {0}:{1}…”, serverName, port);
    // Connect to the server.
    client.Connect(serverName, port, securityMode);
    // Login to the server.
    Console.WriteLine(“Logging in as {0}…”, user);
    client.Authenticate(user, password);
    VB.NET  
    ‘ POP3 server information.
    Const serverName As String = “myserver”
    Const user As String = “name@domain.com”
    Const password As String = “mytestpassword”
    Const port As Integer = 995
    Const securityMode As SecurityMode = securityMode.Implicit
    ‘ Create a new instance of the Pop3Client class.
    Dim client As New Pop3Client()
    Console.WriteLine(“Connecting Pop3 server: {0}:{1}…”, serverName, port)
    ‘ Connect to the server.
    client.Connect(serverName, port, securityMode)
    ‘ Login to the server.
    Console.WriteLine(“Logging in as {0}…”, user)
    client.Authenticate(user, password)
  4. Create a new instance of the BounceInspector class.
    C#  
    // Create a new instance of the BounceInspector class.
    BounceInspector inspector = new BounceInspector();
    VB.NET  
    ‘ Create a new instance of the BounceInspector class.
    Dim inspector As New BounceInspector()
  5. Now pass the newly created instance of the Pop3Client class and optional destination path that is used to store downloaded EML files to the ProcessMessages method. The code looks similar to the following:
    C#
    // Download messages from Pop3 Inbox to ‘c:test’ and process them.
    BounceResultCollection result = inspector.ProcessMessages(client, “c:\test”);
    VB.NET
    ‘ Download messages from Pop3 Inbox to ‘c:test’ and process them.
    Dim result As BounceResultCollection = inspector.ProcessMessages(client, “c:test”)
  6. After completing your work, call the Disconnect method to close the POP3 session.

Final example code

C#
// POP3 server information.
const string serverName = “myserver”;
const string user = name@domain.com;
const string password = “mytestpassword”;
const int port = 995;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the Pop3Client class.
Pop3Client client = new Pop3Client();
Console.WriteLine(
“Connecting Pop3 server: {0}:{1}…”, serverName, port);
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
Console.WriteLine(“Logging in as {0}…”, user);
client.Authenticate(user, password);
// Initialize BounceInspector.
BounceInspector inspector = new BounceInspector();
inspector.AllowInboxDelete = false;
// true if you want BounceInspector automatically delete all hard bounces.
// Register processed event handler.
inspector.Processed += inspector_Processed;
// Download messages from Pop3 Inbox to ‘c:test’ and process them.
BounceResultCollection result = inspector.ProcessMessages(client, “c:\test”);
// Display processed emails.
foreach (BounceResult r in result)
{
// If this message was identified as a bounced email message.
if (r.Identified)
{
// Print out the result
Console.Write(“FileName: {0}nSubject: {1}nAddress: {2}nBounce Category: {3}nBounce Type: {4}nDeleted: {5}nDSN Action: {6}nDSN Diagnostic Code: {7}nn”,
System.IO.Path.GetFileName(r.FilePath),
r.MailMessage.Subject,
r.Addresses[0],
r.BounceCategory.Name,
r.BounceType.Name,
r.FileDeleted,
r.Dsn.Action,
r.Dsn.DiagnosticCode);
}
}
Console.WriteLine(
“{0} bounced message found”, result.BounceCount);
// Disconnect.
Console.WriteLine(“Disconnecting…”);
client.Disconnect();

 

VB.NET
‘ POP3 server information.
Const serverName As String = “myserver”
Const user As String = “name@domain.com”
Const password As String = “mytestpassword”
Const port As Integer = 995
Const securityMode As SecurityMode = securityMode.Implicit
‘ Create a new instance of the Pop3Client class.
Dim client As New Pop3Client()
Console.WriteLine(“Connecting Pop3 server: {0}:{1}…”, serverName, port)
‘ Connect to the server.
client.Connect(serverName, port, securityMode)
‘ Login to the server.
Console.WriteLine(“Logging in as {0}…”, user)
client.Authenticate(user, password)
‘ Initialize BounceInspector.
Dim inspector As New BounceInspector()
inspector.AllowInboxDelete = False ‘ true if you want BounceInspector automatically delete all hard bounces.
‘ Register processed event handler.
AddHandler inspector.Processed, AddressOf inspector_Processed
‘ Download messages from Pop3 Inbox to ‘c:test’ and process them.
Dim result As BounceResultCollection = inspector.ProcessMessages(client, “c:test”)
‘ Display processed emails.
For Each r As BounceResult In result
‘ If this message was identified as a bounced email message.
If r.Identified Then
‘ Print out the result
Console.Write(“FileName: {0}” & Constants.vbLf & “Subject: {1}” & Constants.vbLf & “Address: {2}” & Constants.vbLf & “Bounce Category: {3}” & Constants.vbLf & “Bounce Type: {4}” & Constants.vbLf & “Deleted: {5}” & Constants.vbLf & “DSN Action: {6}” & Constants.vbLf & “DSN Diagnostic Code: {7}” & Constants.vbLf + Constants.vbLf, System.IO.Path.GetFileName(r.FilePath), r.MailMessage.Subject, r.Addresses(0), r.BounceCategory.Name, r.BounceType.Name, r.FileDeleted, r.Dsn.Action, r.Dsn.DiagnosticCode)
End If
Next r
Console.WriteLine(“{0} bounced message found”, result.BounceCount)
‘ Disconnect.
Console.WriteLine(“Disconnecting…”)
client.Disconnect()