This is a migrated thread and some comments may be shown as answers.

how to send radupload attachments through smtp

1 Answer 131 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Saimadhukar
Top achievements
Rank 1
Saimadhukar asked on 21 Oct 2011, 02:27 PM
hello,

i am working on email functionality and recently i  am using rad uploader to upload files and send to mails through smtp connection ,the problem is if i send notepad  files it is uploading and sending to specified mailid ,but if we upload .pdf or .doc means  pdf or wordformat files e.t.c it is not sending  we are getting an error that file cannot found at path specified .i am sending a code block please reply soon
void SendEMail()
   {
       if (Session["UserID"] != null) //from home.aspx
       {
           newmailuserid = Convert.ToInt32(Session["UserID"]);
       }
       DbLayer objDB = new DbLayer();
       System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
       Message.To.Add(rdtxtTo.Text);
       if (rdtxtCC.Text != string.Empty)
       {
           Message.CC.Add(rdtxtCC.Text);
       }
       Message.Subject = rdtxtSubject.Text;
       Message.Body = rdtxtBody.Text;
        
        
       System.Net.Mail.SmtpClient SmtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
       SmtpClient.EnableSsl = true;
       SmtpClient.Credentials = new System.Net.NetworkCredential("abc.hits.com", "hits@123");
       System.Net.Mail.MailAddress FromEmail = new System.Net.Mail.MailAddress(TextBox1.Text);
       Message.From = FromEmail;
             try
       {
           System.Net.Mail.Attachment attachment;
           foreach (UploadedFile file in RadUpload1.UploadedFiles)
           {
               try
               {
                   string strFileName = null;
                   strFileName = System.IO.Path.GetFileName(file.FileName);
                   attachment = new System.Net.Mail.Attachment(Server.MapPath(strFileName));
                   Message.Attachments.Add(attachment);
               }
               catch 
               {
               }
           }
           SmtpClient.Send(Message);

1 Answer, 1 is accepted

Sort by
0
Accepted
Genady Sergeev
Telerik team
answered on 25 Oct 2011, 01:36 PM
Hello Saimadhukar,

The approach that is used is wrong. The FileName property returns the FileName that is on the client-computer. Since your code is executing on the server, there will be no such file as:

Server.MapPath(strFileName)

I suggest that you save the file the hard drive first using its SaveAs method and then, using the name you have saved it under, create new Attachment.

Best wishes,
Genady Sergeev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Upload (Obsolete)
Asked by
Saimadhukar
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Share this question
or