You Try This for the sendmail with attachment for.
public static void SendMailWithAttachment(string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject, string FilePath)
{
SmtpClient smtp = new SmtpClient();
MailMessage mailmsg = new MailMessage();
mailmsg.From = new MailAddress(FromMail);
mailmsg.To.Add(ToMail);
if (Cc != "")
{
mailmsg.CC.Add(Cc);
}
if (Bcc != "")
{
mailmsg.Bcc.Add(Bcc);
}
else
{
string bccAddress = GetConfigValue("TestEmailID");
if (!bccAddress.IsNullOrEmpty())
mailmsg.Bcc.Add(bccAddress);
}
mailmsg.Body = Body;
mailmsg.Subject = Subject;
mailmsg.IsBodyHtml = true;
//mailmsg.Priority = MailPriority.High;
if (File.Exists(FilePath))
{
FileInfo objFileInfo = new FileInfo(FilePath);
Attachment objAttachment = new Attachment(FilePath);
string strFileName = Subject.Replace(" ", "_");
objAttachment.Name = strFileName + objFileInfo.Extension;
mailmsg.Attachments.Add(objAttachment);
}
//Check SMTPUserName and SMTPPassword does not blank, it's black then Use Default Credentials...
if (GetApplicationValue("SMTPUserName").ToString() != String.Empty && GetApplicationValue("SMTPPassword").ToString() != String.Empty)
{
NetworkCredential basicAuthenticationInfo = new NetworkCredential(GetApplicationValue("SMTPUserName").ToString(), GetApplicationValue("SMTPPassword").ToString());
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicAuthenticationInfo;
}
smtp.Host = GetApplicationValue("SMTPHost");
smtp.Port = GetApplicationValue("SMTPPort").ParseNativeInt();
try
{
smtp.Send(mailmsg);
mailmsg.Dispose();
}
catch (Exception)
{
//throw ex;
}
}