Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Im not able to send the attachments while sending SMTP mails in ASP.NET.

C#
using System.Web.Mail;

//In the send button event,i placed this code.
C#
Server.ScriptTimeout = 1000;
        MailMessage msg = new MailMessage();
        msg.From = this.Txtfrom.Text;
        msg.To = this.TxtTo.Text;
        msg.Cc = this.TxtCC.Text;
        msg.Subject = this.Txtsub.Text;
        msg.Body = this.Txtbody.Text;

        if (filatchmnt1.PostedFile != null)
        {
            HttpPostedFile atchfile = filatchmnt1.PostedFile;
            int atchlen = atchfile.ContentLength;
            if (atchlen > 0)
            {
                strfilename = Path.GetFileName(filatchmnt1.PostedFile.FileName);
                filatchmnt1.PostedFile.SaveAs(Server.MapPath(strfilename));
                MailAttachment atch = new MailAttachment(Server.MapPath(strfilename));
                msg.Attachments.Add(atch);
                
            }
        }
        try
        {
            SmtpMail.Send(msg);
            MessageBox.Show("Mail has been sent to" + msg.To + '&' + msg.Cc);
          
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception:" + ex.Message);
        }
        Response.Flush();


The error "the name strfilename doesn't exists in the current context;".
Kindly help..
Posted
Updated 1-Nov-11 23:38pm
v2

Declare the strfilename variable

and check this to send attachments in email
Sending Email with attachment in ASP.NET using SMTP Server[^]

ASP.NET email with multiple attachments[^]
 
Share this answer
 
v2
Comments
maestro_88 2-Nov-11 5:52am    
I declared the variable and executed.No error occured.But Im not able to receive the attachment in the receiver's mail.
P.Salini 2-Nov-11 5:58am    
Go through those links and check whether you are missing anything
maestro_88 2-Nov-11 9:07am    
Now i got it ..Thanks
P.Salini 3-Nov-11 0:48am    
If solution is helpful to you then accept it
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data;
using System.Web.Mail;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Drawing;
using System.Xml; 

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
  System.Net.Mail.MailMessage obj = new System.Net.Mail.MailMessage();
          
            SmtpClient serverobj = new SmtpClient();
            serverobj.Credentials = new NetworkCredential("anilmeets4u@gmail.com", "########");
            serverobj.Port = 587;
            serverobj.Host = "smtp.gmail.com";
            serverobj.EnableSsl = false;
            obj = new System.Net.Mail.MailMessage();
            obj.From = new MailAddress("anilmeets4u@gmail.com", "AgileLearning.com", System.Text.Encoding.UTF8);
            obj.To.Add(ASPxtxtToUser.Text);
            obj.CC.Add(txtCcUser.Text);
            obj.Priority = System.Net.Mail.MailPriority.High;
            obj.Subject = txtSubject.Text;
            string date = DateTime.Now.ToString();
            obj.Body = ASPxMemo1.Text;
            HttpFileCollection hfc = Request.Files;
            for (int z = 0; z < hfc.Count; z++)
            {
                HttpPostedFile hpf = hfc[z];
                if (hpf.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("MailFiles") + "\\" + Path.GetFileName(hpf.FileName));
                    string FileName = Server.MapPath("MailFiles") + "\\" + Path.GetFileName(hpf.FileName);

                    obj.Attachments.Add(new Attachment(FileName));
                    obj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;


                    serverobj.Send(obj);
                    Page.RegisterClientScriptBlock("pageClose", "<script>alert('Your Request Send Sucessfully');</script>");
                }
            }

           

        }


        catch (Exception ex)
        {
            ex.ToString();
        }


    }


Try this it will work
 
Share this answer
 
v3
Comments
maestro_88 2-Nov-11 6:04am    
Thanks Archana.But im using the code to send SMTP mails within a domain (using IIS)and not gmail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900