I'm trying to create a desktop based mailing software where one can attach files and then mail it to any mail address. I'm having 2 problems.

1. I cannot attach files which are greater than 2mb in size although I use my G-mail account to send a mail and g-mail allows 15mb maximum file size. I searched the web and found that there may be a bug in Microsoft .net Framework 4.0. Is that true and please suggest any ways to rectify if possible.

2. While attaching smaller files, it works fine but if I select a path other than the C: drive for the attached file, it shows an error, hence it can send a file as an attachment only when the file is present in the C: drive.

Please help me solve these problems.
Thanks in advance. :)

Is this a web or win app?

My answer,

(1) - No bug at all.
(2) - Please post exception trace here.

This a win app.

for 1. the exception is:

System.Net.Mail.SmtpException:The operation has timed out at System.Net.Mail.SmtpClient.Send(MailMessage message) at Mail_Try.Form1.button1_click(Object Sender,EventArgs e) in D:\B.Tech\Visual Studio\C# codes\Mail Try\Mail_Try\Form:1.cs:line 48

Can you please suggest any remedy for the problem.

I have been able to solve the 2nd part of my problem.
Thank you for your reply :)

Note sure about your code but I'd suggest set TimeOut=200000 property.

System.Net.Mail.SmtpClient client;
 client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
  try
   {
   client.Credentials = new System.Net.NetworkCredential("user@gmail.com", "gmail_password");
   client.EnableSsl = true;
 //client.TimeOut=200000;

             

   System.Net.Mail.MailMessage msg;
   msg = new System.Net.Mail.MailMessage(from, to,subject,body);

   string file=@"x:\file.ext";            
   if (System.IO.File.Exists(file))
      {
        System.IO.Stream stream = System.IO.File.OpenRead(file);
        string filename = System.IO.Path.GetFileName(file);
        System.Net.Mail.Attachment att;
        att=new System.Net.Mail.Attachment(stream,filename);
        msg.Attachments.Add(att);
       }

      client.Send(msg);
      stream.close();
       // "Sent";
  }
 catch (Exception ex)
 {
    //Error 
 }

This is my code:

try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                SmtpServer.Timeout = 200000;                
                mail.From = new MailAddress(label1.Text);
                mail.To.Add(textBox1.Text);
                mail.Subject = textBox3.Text;
                mail.Body = richTextBox1.Text;

                System.Net.Mail.Attachment attachment1;
                
                attachment1 = new System.Net.Mail.Attachment(textBox4.Text);
                
                mail.Attachments.Add(attachment1);
                                

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new                          System.Net.NetworkCredential(label1.Text, label2.Text);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Mail Send!");
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

When I executed this code it gave

System.Net.SmtpException: Failure sending mail ->
System.IO.IOException:Unable to write data to the Transport connection.

>Unable to write data to the Transport connection.

I think you are sending large email or attach a large file. Please read this thread.

Yes,I'm trying to send a file which is about 10mb in size, I was hoping I could keep the limit of my application attachment size to 10mb...I see it is not possible,as the server either keeps saying timeout or it gives me a write failure...
Is there at all any other way?
My present code sends upto 5mb of data at a time...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.