Sending emails with attachments in asp.net using C#

To send emails with attachment in asp.net using C# is quite easy. I have already written two tutorials that discuss sending emails in asp.net using C#. If you not read these tutorials then please read them first. If you want to send attachment with email in asp.net using C# then here’s the code that should be added in the sendEmail() function that I already discuss in my tutorials.

string FilePath;
FilePath = "c:\\yourfile.txt";
MailAttachment attach = new MailAttachment(strFilePath);
msg.Attachments.Add(attach);


So after adding these above mentioned lines of code, your sendEmail() function will be

public void sendEmail()

{
MailMessage msg = new MailMessage(txt_From.Text,txt_To.Text);

msg.CC.Add(txt_Cc.Text);

msg.Bcc.Add(txt_Bcc.Text);

msg.Subject = txt_Subject.Text;

msg.Body = Bodytext();

msg.Subject = txt_Subject.Text;

msg.IsBodyHtml = true;

string FilePath;

FilePath = "c:\\yourfile.txt";

MailAttachment attach = new MailAttachment(strFilePath);

msg.Attachments.Add(attach);

SmtpClient yourSmptpclient = new SmtpClient("localhost");

try{

yourSmptpclient.Send(msg);
}

catch(Exception Exp){

throw Exp;

}

}

So this is the simple way to send attachments with email in asp.net using C#.

0 comments: