Microsoft Developer Network > Forums Home > Visual Studio Express Editions Forums > Visual C# Express Edition > E-Mail fails to attach larger (1 MB +/-) attachments?
Ask a questionAsk a question
 

QuestionE-Mail fails to attach larger (1 MB +/-) attachments?

  • Wednesday, October 21, 2009 7:32 AMMLyons10 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everyone.  I had some code that was working perfectly well, then stopped working the other day.  As I didn't change anything in this code, I'm thinking that perhaps something in the .Net 2.0 security update might have changed the way that SMTP in .Net works?  I can't find anything about what exactly this update did, so this is just a theory and certainly may be wrong.  I don't know why it would stop working suddenly though, and it just times out.

    I have this running in a backgroundWorker_DoWork event, which is where it was previously.

    Here is my code:
                string
    [] SendMail = (string
    [])e.Argument;
    string CustLastName = SendMail[0].ToString();
    string CustFirstName = SendMail[1].ToString();
    string User = SendMail[2].ToString();
    string UserEMail = SendMail[3].ToString();
    string SoftVersion = SendMail[4].ToString();
    string Notes = SendMail[5].ToString();
    string CCState = SendMail[6].ToString();
    string UrgentState = SendMail[7].ToString();
    string attachState = SendMail[8].ToString();

    //Send E-Mail on click of Button
    MailMessage theMailMessage = new MailMessage("//From" , "//To" );

    BackgroundWorker worker = sender as BackgroundWorker;
    worker.ReportProgress(25);

    //Generate the message body
    string messageBody = "New Information Added For: " + CustLastName + ", " + CustFirstName;
    messageBody += Environment.NewLine + "" ;
    messageBody += Environment.NewLine + "From: " + User;
    messageBody += Environment.NewLine + "E-Mail: " + UserEMail + " (CC'd = " + CCState + ")" ;
    messageBody += Environment.NewLine + "" ;
    messageBody += Environment.NewLine + "Software Version: " + SoftVersion;
    messageBody += Environment.NewLine + "" ;
    messageBody += Environment.NewLine + @"\\server\shared\" + CustLastName + ", " + CustFirstName + @"\" ;
    messageBody += Environment.NewLine + "" ;
    messageBody += Environment.NewLine + "Notes: " + Notes;

    worker.ReportProgress(50);

    //Set the property of the message body and subject body
    theMailMessage.Body = messageBody;
    theMailMessage.Subject = "New File for " + CustLastName + ", " + CustFirstName;

    //Set the CC Property
    if (CCState == "true" )
    {
    MailAddress copy = new MailAddress(UserEMail);
    theMailMessage.CC.Add(copy);
    }

    //Set the Urgent Property
    if (UrgentState == "true" )
    {
    theMailMessage.Priority = MailPriority.High;
    }

    //Set the Attachment Property
    if (Sub1.Text != "Skipped!" && attachState == "true" )
    //if (attachState == "true")
    {
    string tempFolder = Path.GetTempPath();
    theMailMessage.Attachments.Add(new Attachment(tempFolder + @"\JobFile.zip" ));
    }

    worker.ReportProgress(75);

    //E-Mail Credentials and Sending
    SmtpClient theClient = new SmtpClient("smtp.1and1.com" );
    System.Net.NetworkCredential theCredential = new

    System.Net.NetworkCredential("//From" , "xxxxx" );
    theClient.Credentials = theCredential;
    theClient.Send(theMailMessage);

    worker.ReportProgress(100);

    I'm open to any ideas.  I don't understand why it would be timing out.  First I thought that maybe the attachment was missing, but it IS there and it is listed in the debugger...

    Thanks again everyone.  I really appreciate the help.
    • Edited byMLyons10 Tuesday, November 03, 2009 11:22 AM
    •  

All Replies

  • Wednesday, October 21, 2009 1:04 PMkaymaf Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    SMTPException : exception that is thrown when the SMTPClient is not able to complete a Send or SendAsync operation

    Try this with sample code : http://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception.aspx

    kaymaf

    If that what you want, take it. If not, ignored it and no complain
  • Wednesday, October 21, 2009 4:34 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The much more likely reason is that 1and1 has changed their SMTP server setup.  A quick check with telnet confirms this, the server doesn't accept connections on port 25.  There are not a lot of servers left that accept unsecured connections.  Contact 1and1 for support.

    Hans Passant.
  • Wednesday, October 21, 2009 7:02 PMMLyons10 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you both for your responses.  I'll contact 1and1, but don't know how helpful they're going to be.  Hopefully they can tell me what's up.  I'm not setting a port when I am sending the mail, so should I be setting a port somewhere or securing the connection?  I have another application that sends mail the same way and that one works without any problems.  I just figured I would try that now given your suggestion.  I did read up on the SMTPException, but it is very general so I wasn't even sure where to start.  As the mail is working from my other application though, I'm thinking that maybe this isn't a 1and1 issue?

    Thanks Again,
  • Thursday, October 29, 2009 7:27 AMMLyons10 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, I contacted 1&1 and they said that it wasn't anything to do with them, and after putting together a test application in which this exact feature works (With the same code), I'm still trying to figure out what is wrong in my application.

    With that said, is there a way to view in Visual Studio 2008 Professional each line of code that executes without creating a hundred breakpoints?  This process executes a lot of code, so I would like to be able to see what's occurring to try to troubleshoot without the creation of so many breakpoints that I would have to then remove.

    Thanks Again,
  • Friday, October 30, 2009 7:21 AMMLyons10 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi again.  I am still trying to figure this out.  As I am compiling the zip prior to sending it, I figured I would make sure that I'm disposing the zip and was.  I also commented out the attachment and it does send the mail without a problem if it is not attaching the attachment, so it is deffinitely something to do with the attachment.  I don't understand though why the sample application would be able to send this attachment though.  That application wasn't creating the zip, it was just there already.  The code for sending the e-mail is run in the zip backgroundWorker_Completed event, so it should be finished before the e-mail tries to send it.

    Does anyone have any other ideas for what this might be that I could look into?  Since sending mail does work, it seems to be on my end...

    Thanks Again,

    **Edit:

    It seems to have something to do with the size of the attachment.  I am ziping up some images and text documents and such.  If the zip is small and only has the text documents in it, it works.  Larger zip packages are working in the test application though, so maybe even though I'm calling Dispose it isn't actually disposing the zip or something?
  • Tuesday, November 03, 2009 8:55 AMMLyons10 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everyone.  Does anyone have any ideas why this would not work?  I have tried everything that I can think of and am now out of ideas.  It times out when trying to send the mail if the attachment is about a MB or so, but when the attachment is a couple of KB's it works.  I don't understand why this would be.  The e-mails are able to be sent without any problem.  The attachments are able to be added to the message.  The zip file exists and even when it is a MB it is able to be attached to the mail message without a problem.  Sometimes It's able to be sent, but 99% of the time it is not.  I just don't understand what else the problem could be so I'm open to any ideas or suggestions.

    Thanks Again,