Sending a mail with Attachment.
We need to send mail with an attachment below is the sample code to send mail.The code used c# as language
Want to include the following Header
using
System.Web.Mail;
string
strfrom="";
string
strGenericId=Session["GenericId"].ToString();
string
strQuery="Update ANC_D_EnduserProblem set EmailStatus='Y' where Logid='"+strGenericId.ToString()+"'";
SqlHelper.ExecuteNonQuery(ConnStr,CommandType.Text,strQuery);
strsql="select * from UserMaster where UserId=" + Session["UID"].ToString() ;
SqlCommand cmd=new SqlCommand(strsql,objConn);
IDataReader drChapterHeader=cmd.ExecuteReader();
if
(drChapterHeader.Read())
{
strfrom=drChapterHeader["EMail"].ToString();
}
drChapterHeader.Close();
MailMessage mail = new MailMessage();
MailAttachment Attachhtml;
Creating a Variable for MailMessage and MailAttachment
The Following line shows from which mail-id we want to send a mail.
mail.From = "bhaskaran@invosystems.com";
The Following line shows to whom we want to send a mail.
mail.To = txtto.Text;
The Following line describes the subject of the mail.
mail.Subject = txtsubject.Text;
string
strFileName="";
if
(File.Exists(Server.MapPath("DocumentFiles\\wdoc\\" + strcampcode + ".doc"))==true)
{
The Following line describes how we want to attach a file with the mail.
strFileName=Server.MapPath("DocumentFiles\\wdoc\\" + strcampcode + ".doc");
Attachhtml = new MailAttachment(strFileName);
mail.Attachments.Add(Attachhtml);
}
else
{
strFileName=FileMailAttach.PostedFile.FileName;
if(strFileName=="" || strFileName==null)
{
}
else
{
Attachhtml = new MailAttachment(strFileName);
mail.Attachments.Add(Attachhtml);
}
}
The Following line describes the body of mail to send
mail.Body = txtbody.Text ;
mail.Priority = MailPriority.High;
mail.BodyFormat = MailFormat.Text;
SmtpMail.SmtpServer = "mail.invosystems.com";
//string mailservername=ConfigurationSettings.AppSettings["mailservername"].ToString();
//
//SmtpMail.SmtpServer = mailservername;
Sending the Mail
SmtpMail.Send(mail);