Home > Code > C# > sending emails with attachments. use in string builder in asp.net with c#

sending emails with attachments. use in string builder in asp.net with c#

by javed akhtar ansari   on Dec 25, 2013   Category: C#   |  Views: 2663    |  Points: 25   |  Starter 


protected void BtnSubmit_Click(object sender, EventArgs e)
{

bool confirmation = false;

System.Net.Mail.MailAddress mm = new MailAddress(" abc@gmail.com", "PERK ALLIED");

string Name = txtName.Text;

string email = txtEmail.Text;
string Phone = txtPhone.Text;
string Message = txtMessage.Text;

string body = string.Format("<table width=\"600px\" height=\"400px\" border=\"3\"style=\"font-family:Courier New, Courier, monospace; border-style: solid; border-color:#591434; font-size:25px;\"><tr bgcolor=\"#8E3AC9\" style=\"color: #FFFFFF\"><td colspan=\"2\" align=\"center\" >Send Enquery<br></td></tr><tr><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">Name :</td><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">{0}</td></tr><tr><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">Email-Id :</td><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">{1}</td></tr><tr><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">Phone No:</td><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">{2}</td></tr><tr bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\"></tr><tr><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">Comments:</td><td bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\">{3}</td></tr><tr bgcolor=\"#BA7E0D\" style=\"color: #FFFFFF\"></tr></table>", Name, email, Phone, Message);
string mailbody = body;
string mailto = " abc@gmail.com";
string mailsubject = "PERK ALLIED";
MailMessage mailmessage = new MailMessage(" abc@gmail.com", mailto);
mailmessage.To.Add(mailto);
mailmessage.Subject = mailsubject;
mailmessage.Body = mailbody;
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

mailmessage.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));

}
mailmessage.IsBodyHtml = true;

string emailsendfrom = " abc@gmail.com";
string emailsendpassword = "abc123";

NetworkCredential credential = new NetworkCredential(emailsendfrom, emailsendpassword);

SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 200000;
mailClient.Credentials = credential;
mailClient.Send(mailmessage);

confirmation = true;
if (confirmation == true)
{

Response.Write("<script language='javascript'>alert(' Successfully Registered!!!')</script>");
txtName.Text = "";
txtEmail.Text = "";
txtPhone.Text = "";

txtMessage.Text = "";


}
}


Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts