Advance Email Sending Option , with attachement.
Task: Sending
Email with attachment using c# code in asp.net website/ web application.
Description: Some
time we need to a website viewer can send a enquiry with some attachment
as some kind of career option where a
candidate can submit his/her resume and the website administrator receive the
same within email.
As in my previous post I discuss about a email notification http://hemantrautela.blogspot.com/2012/08/how-to-send-email-from-website-aspnet.html
without attachment option , Now we require a email with attachment, I use below
code for sending email with attachment . Here I use two fileupload control for
uploading attachment file (here I am not checking the extension of file / as
you can check it first before sending email ).
// Below Function can be used for sending a email with attachment option using c# code
// using gmail crdential
using System.Net.Mail;
public bool mailattachement2(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","Client Enquiry");
mail.Subject = subject;
mail.Body = body;
MailAddress rt = new MailAddress(replyto);
mail.ReplyTo = rt;
//mail.Sender = rt;
mail.IsBodyHtml = true;
if (f1.HasFile)
{
mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); //add the attachment
}
if (f2.HasFile)
{
mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); //add the attachment
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
// Set the one email id and its password for authentication )
// email goes via using above email id...
smtp.Port = 587;
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
return (true);
}
catch (Exception ex)
{
return (false);
}
}
Thanks for the script, very helpful.
ReplyDeleteVery Good Code ...
ReplyDeleteThank You So Much Buddy ... :-)
thanks
ReplyDeletecan u explain the coding pls..
ReplyDeleteThis blog is for developer, & I think above code is not a complicated code for explanation...
DeleteThanks alot! It worked.
ReplyDeleteCompiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
ReplyDeleteIf you just copy & paste this code then it will be give you error...:-)
Deleteyou have to add some namespaces also & a developer should be know that how can add these stuff...
:-)
please any idea on how i an create birthday update reminder in asp.net c#?
ReplyDeleteIn asp.net for webapplication is not found any solution, But with window application using Task Schedular you can create any kind of reminder service or any automated Task.
Deletehow to give the username & password in web.config
ReplyDelete