Email Attachment Code in C#
public bool sendemail(string to, string replyto, string body, string subject, FileUpload f1, FileUpload f2)
{
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress("xyz@gmail.com","Subject of Email");
mail.Subject = subject;
mail.Body = body;
MailAddress rt = new MailAddress(replyto);
mail.ReplyTo = rt;
mail.IsBodyHtml = true;
if (f1.HasFile)
{
mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); }
if (f2.HasFile)
{
mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); }
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
smtp.Port = 587;
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
return (true);
}
catch (Exception ex)
{
return (false);
}
}
Thanks
Source :
Email Sending with attachment asp.net c#[
^]