I have a process that will sit on an Automated Emailing machine to send emails based on a flat file. The file provides the To, CC, Subject, Body, Attachments, and Sender. When the email is sent, I want any failed emails or bounce backs to go to the sender rather than the automated service. I have tried two solutions that work...partially.
First using SMTP, I am able to set the Return-Path and the From addresses and get bounced emails to go back to the sender. However, company policy is that I can not hard code user-ids and/or passwords. I have tried to use the "UseDefaultCredentials" flag with no luck. Our AD login and Exchange login are different and the password is the same. Here is the code:
private void SendEmail(EmailProperties pEmail)
{
try
{
MailMessage mail = new MailMessage();
mail.Headers.Add("Return-Path", AddressLookup(pEmail.Sender));
foreach (string strTo in pEmail.To)
{
mail.To.Add(AddressLookup(strTo));
}
foreach (string strCC in pEmail.CC)
{
mail.CC.Add(AddressLookup(strCC));
}
mail.Subject = pEmail.Subject;
mail.Body = pEmail.Body;
mail.From = new MailAddress(AddressLookup(pEmail.Sender));
//mail.ReplyToList.Add(AddressLookup(pEmail.ReplyTo));
SmtpClient smtpClient = new SmtpClient("server", 25);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new NetworkCredential("user", "pw");
smtpClient.EnableSsl = false;
smtpClient.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
private string AddressLookup(string pstrAddr)
{
try
{
if (pstrAddr.Contains("@")) return pstrAddr;
Outlook.Application OutlookApp = new Outlook.Application();
Outlook.AddressLists addrLists = OutlookApp.Session.AddressLists;
Outlook.AddressList gal = addrLists["Global Address List"];
Outlook.AddressEntry entry = gal.AddressEntries[pstrAddr];
return entry.GetExchangeUser().PrimarySmtpAddress;
}
catch (Exception ex)
{
throw ex;
}
}
Second: I have tried using the Outlook Interop and I am able to send the email fine but any bounce backs go the the automated service. Here is the Code:
private void SendEmail(EmailProperties pEmail)
{
try
{
Outlook.Application OutlookApp = new Outlook.Application();
Outlook.MailItem mail = OutlookApp.CreateItem(
Outlook.OlItemType.olMailItem) as Outlook.MailItem;
Outlook.AddressEntry currentUser =
OutlookApp.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
mail.To = pEmail.To;
mail.CC = pEmail.CC;
mail.Subject = pEmail.Subject;
mail.Body = pEmail.Body;
mail.ReplyRecipients.Add(pEmail.ReplyTo);
mail.Sender = AddressLookup(pEmail.Sender);
mail.Recipients.ResolveAll();
mail.ReplyRecipients.ResolveAll();
foreach (string e in pEmail.Attachments)
{
mail.Attachments.Add(e, Outlook.OlAttachmentType.olByValue,
Type.Missing, Type.Missing);
}
mail.Send();
}
}
catch (Exception ex)
{
throw ex;
}
}
Is there something I am missing or another direction I can go with this. I would prefer to use the interop to send the emails and just change the email header. If there is a better way of doing this, I am open to suggestions.
Thanks!