we have to add following namespaces in our namespace area
Import System.Net
Import System.Net.Mail
Now the function below which i have written, you just pass the information in arguments and call that function to send email.
public static int SendEmail(string strFrom, string strTo, string strSubject, string strMsg)
{
try
{
// Object for mail
MailMessage objMailMsg = new MailMessage(strFrom, strTo);
objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = strSubject;
objMailMsg.Body = strMsg;
Attachment at = new Attachment(Server.MapPath("~/Files/TempFile.txt"));
objMailMsg.Attachments.Add(at);
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
//Email send through SMTP Protocol
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
objSMTPClient.Send(objMailMsg);
}
catch (Exception ex)
{
throw ex;
return 1;
}
finally
{
return 0;
}
}
if this function return 0 then email sent successfully with attachments
or is it return other than 0,then there might be some problems with server or attachments or Exception fired.
I hope this articles will be helpful for developers
Thanks
No comments:
Post a Comment