Sending an Email Through C#

  • Thread starter Thread starter IndigoMontoya
  • Start date Start date
I

IndigoMontoya

Guest
I am sure this is due to scope - but I'm not certain on how to remedy my issue. What my desired output is to have two emails generated. One saved for redfern1@test.com; hottomales2@gmail.com
and a second email saved for: redfern2@test.com; hottomales4@gmail.com - Altho, what I'm getting is two emails
addressed to all of the emails.


using System;
using System.Data;

public class Program
{
private static DataTable table = new DataTable();
public static void Main()
{
table.Columns.Add("Company", typeof(string));
table.Columns.Add("ContactEmails", typeof(string));
table.Columns.Add("Month", typeof(string));
table.Rows.Add("Company1", "redfern1@test.com; hottomales2@gmail.com", "March");
table.Rows.Add("Company2", "redfern2@test.com; hottomales4@gmail.com", "March");

foreach (DataRow r in table.Rows)
if (r["Month"].ToString() == DateTime.Now.ToString("MMMM"))
CreateEmail();
}

public static void CreateEmail()
{
Microsoft.Office.Interop.Outlook.Application aApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;

foreach (DataRow r in table.Rows)
{
if (r["Month"].ToString() == DateTime.Now.ToString("MMMM"))
{
string email = r["ContactsEmail"].ToString();
Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(email);
oRecip.Resolve();
}
}

oMsg.Save();
}
}

Continue reading...
 
Back
Top