Friday, April 26, 2013

Send email asynchronously second time in case of first time failure


public class Mailer : ComposeMail
{
public StringBuilder m_ExecutionStack = null;
public StringBuilder ExecutionStack
{
get
{
if (m_ExecutionStack == null)
{
m_ExecutionStack = new StringBuilder();
}
return m_ExecutionStack;
}
}

public bool IsSecondAttemptDone { get; set; }

public SmtpClient SMTPClient
{
get;
set;
}

public bool IsMailSent
{
get;
set;
}

public MailMessage Message
{
get;
set;
}

public Mailer(string siteUrl)
: base(siteUrl)
{
try
{
ExecutionStack.AppendLine("Initilizing Mailer Object");
Message = new MailMessage();
SMTPClient = new SmtpClient(base.SMTP);
SMTPClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

ExecutionStack.AppendLine("Initilizing Mailer Object Done");
}
catch (Exception ex)
{
ExecutionStack.AppendLine("Error : Initilizing Mailer Object");
throw ex;
}
}

public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
ExecutionStack.AppendLine("Send Mail completed event :" + token);

if (e.Error != null)
{
IsSecondAttemptDone = true;

ExecutionStack.AppendLine("Error Sending Mail");
ExecutionStack.AppendLine("Details : " + token + " ->" + e.Error.InnerException.ToString());

System.Diagnostics.EventLog.WriteEntry("Application", "Mailer : Send Mail completed event :  -> " + ExecutionStack.ToString(), System.Diagnostics.EventLogEntryType.Error);

ExecutionStack.AppendLine("Trying to re send mail ");
SMTPClient.SendAsync(Message, token);
ExecutionStack.AppendLine("Re send mail code execution done");
}

IsMailSent = true;
}

public void SendMail(string from, string to)
{
ExecutionStack.AppendLine("Inside SendMail Function");

try
{
if (!String.IsNullOrEmpty(FROMEMAIL))
{
ExecutionStack.AppendLine("Setting From Email");
Message.From = new MailAddress(FROMEMAIL);
Message.Priority = EmailPriority;
string[] toAddresses = to.Split(';');
for (int i = 0; i < toAddresses.Length; i++)
{
if (toAddresses[i] != string.Empty)
{
string[] nameID = toAddresses[i].Split(',');
if (nameID.Length == 2)
Message.To.Add(new MailAddress(nameID[0], nameID[1]));
else
Message.To.Add(new MailAddress(toAddresses[i]));
}
}
if (CC != null)
{
ExecutionStack.AppendLine("Setting CC");
string[] ccAddresses = CC.Split(';');
for (int i = 0; i < ccAddresses.Length; i++)
{
if (ccAddresses[i] != string.Empty)
{
string[] nameID = ccAddresses[i].Split(',');
if (nameID.Length == 2)
Message.CC.Add(new MailAddress(nameID[0], nameID[1]));
else
Message.CC.Add(new MailAddress(ccAddresses[i]));
}
}
}

if (BCC != null)
{
ExecutionStack.AppendLine("Setting BCC");
string[] bccAddresses = BCC.Split(';');
for (int i = 0; i < bccAddresses.Length; i++)
{
if (bccAddresses[i] != string.Empty)
{
string[] nameID = bccAddresses[i].Split(',');
if (nameID.Length == 2)
Message.Bcc.Add(new MailAddress(nameID[0], nameID[1]));
else
Message.Bcc.Add(new MailAddress(bccAddresses[i]));
}

}
}

ExecutionStack.AppendLine("Setting Subject, Body ");
Message.Subject = Subject;
Message.Body = Body;
Message.IsBodyHtml = ISBODYHTML;

if (ISIMAGEEMBED)
{
ExecutionStack.AppendLine("Setting Embedded Images");
try
{
AlternateView aView = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);
Message.IsBodyHtml = true;

LinkedResource[] linkedResources = new LinkedResource[IMAGEPATH.Count];
for (int i = 0; i < IMAGEPATH.Count; i++)
{
linkedResources[i] = new LinkedResource(IMAGEPATH[i].ToString());
linkedResources[i].ContentId = CONTENTID[i].ToString();
}
for (int i = 0; i < IMAGEPATH.Count; i++)
{
aView.LinkedResources.Add(linkedResources[i]);
}
Message.AlternateViews.Add(aView);
}
catch (Exception ex1)
{
System.Diagnostics.EventLog.WriteEntry("Application", "Mailer : Error: Inside SendMail Function : Setting Embedded Images : " + ex1.Message + " -> " + ExecutionStack.ToString(), System.Diagnostics.EventLogEntryType.Error);
ExecutionStack.AppendLine("Error Setting Embedded Images");
throw ex1;
}
}

if (ISATTACHMENTS)
{
ExecutionStack.AppendLine("Setting Attachments");
try
{
StreamReader fileReader = new StreamReader(ATTACHMENTPATH);
if (ValidateAttachment(fileReader.BaseStream))
{
Attachment attachment = new Attachment(fileReader.BaseStream, ATTACHMENTNAME);
Message.Attachments.Add(attachment);
}
}
catch (Exception ex2)
{
ExecutionStack.AppendLine("Error Setting Attachments");
throw ex2;
}
}

ExecutionStack.AppendLine("SMTP Server :" + SMTP);
if (!String.IsNullOrEmpty(SMTP))
{
ExecutionStack.AppendLine("Sending mail in SMTP Server :" + Message.Headers);
SMTPClient.Timeout = int.MaxValue;

SMTPClient.Send(Message);
IsMailSent = true;
ExecutionStack.AppendLine("Sending mail done" + Message.Headers);
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", "Mailer : Error: Inside SendMail Function " + ex.Message + " -> " + ExecutionStack.ToString(), System.Diagnostics.EventLogEntryType.Error);
ExecutionStack.AppendLine("Error: Inside SendMail Function" + ex.InnerException.ToString());
ExceptionHandler.Publish(ex, ex.Message, Severity.Fatal);
IsMailSent = false;
//throw ex;
}
}

private bool ValidateAttachment(Stream attachmentStream)
{
bool status = false;
try
{
if (attachmentStream.Length >= ATTACHMENTSIZE * 1024)
{
status = false;
}
else
{
status = true;
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", "Mailer : ValidateAttachment Method " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
//throw ex;
}
return status;
}
}

Sunday, April 21, 2013

Received-SPF: SoftFail


Received-SPF: SoftFail (msc1-1-bnov.domainname.net: domain of
transitioning abc@anotherdomain.com discourages use of 127.0.0.0 as permitted sender)

The SoftFail is being thrown because of an invalid address in the FROM field of the message. SPF is a specialized check email servers do to protect themselves from being used as spam relays

Solution:
$list = (Get-ContentFilterConfig).BypassedSenders
$list.add("mail@domain.com")
Set-ContentFilterConfig -BypassedSenders $list