Results 1 to 4 of 4

Thread: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    1

    [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??

    am using .NET 2.0 Visual Studio 2005 using C#.

    I'm attaching files using System.Net.Mail.Attachment.Add(...)
    My problem is, that this is working fine, when I'm debugging with visual studio.
    but in Livemode the file-attachments seems not to work. (no error is thrown but I don't get the mail)

    Calling the site via localhost also sends no mail.
    Sending Mails using this System.Net.Mail-Class always works fine (without attachments).

    Please help!!

    here is the code
    c# Code:
    1. xxxxxxxxxxxxxxxxxxxxxxxx
    2.  
    3. public void sendmail(string frm, string to, string bcc, string cc, string sub, string bdy, string smtpserver, string smtpusername, string smtppassword)
    4.  
    5. {
    6.  
    7. int fileLen=0;
    8.  
    9. string pathAndFile = "";
    10.  
    11. try
    12.  
    13. {
    14.  
    15. if (smtpusername != "" && smtppassword != "")
    16.  
    17. {
    18.  
    19. System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(frm, to, sub, bdy);
    20.  
    21. if (bcc != "")
    22.  
    23. msg.Bcc.Add(bcc);
    24.  
    25. if (cc != "")
    26.  
    27. msg.CC.Add(cc);
    28.  
    29. if (txtFileUpload.HasFile)
    30.  
    31. {
    32.  
    33. pathAndFile = txtFileUpload.PostedFile.FileName;
    34.  
    35. fileLen = (txtFileUpload.PostedFile.ContentLength / 1000);
    36.  
    37. if (fileLen > 500)
    38.  
    39. {
    40.  
    41. CreateMessageAlert(this, "Maximum File Size Limit is 500 kbps", "alert", "MyMessages.aspx?link=3");
    42.  
    43. return;
    44.  
    45. }
    46.  
    47. System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(pathAndFile);
    48.  
    49. msg.Attachments.Add(attachment);
    50.  
    51.  
    52. }
    53.  
    54. msg.IsBodyHtml = true;
    55.  
    56. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpserver);
    57.  
    58. smtpClient.UseDefaultCredentials = false;
    59.  
    60. smtpClient.Credentials = new System.Net.NetworkCredential(smtpusername, smtppassword);
    61.  
    62. smtpClient.Send(msg);
    63.  
    64. if (pathAndFile != "")
    65.  
    66. {
    67.  
    68. msg.Attachments.Dispose();
    69.  
    70. }
    71.  
    72.  
    73. }
    74.  
    75. else
    76.  
    77. {
    78.  
    79. System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
    80.  
    81. msg.From = frm;
    82.  
    83. msg.To = to;
    84.  
    85. msg.Bcc = bcc;
    86.  
    87. msg.Cc = cc;
    88.  
    89. msg.Subject = sub;
    90.  
    91. if (pathAndFile!="")
    92.  
    93. {
    94.  
    95. msg.Attachments.Add(new MailAttachment(pathAndFile));
    96.  
    97. }
    98.  
    99. msg.BodyFormat = MailFormat.Html;
    100.  
    101. msg.Body = bdy;
    102.  
    103. SmtpMail.SmtpServer = smtpserver;
    104.  
    105. SmtpMail.Send(msg);
    106.  
    107. if (pathAndFile != "")
    108.  
    109. {
    110.  
    111. msg.Attachments.Clear();
    112.  
    113. // msg.Attachments.Dispose();
    114.  
    115. }
    116.  
    117. }
    118.  
    119. }
    120.  
    121. catch
    122.  
    123. {}
    124.  
    125. }
    126.  
    127. sendmail(Session["userid"].ToString(), email, tempbcc, tempcc, txtSubject.Text, body, smtpserver, smtpusername, smtppassword);
    Last edited by Hack; Dec 13th, 2007 at 07:57 AM. Reason: Added Highlight Tags

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??

    Hmm, you say no email arrives but there isn't an "error". I beg to differ. You never, ever use empty catch blocks as you have in lines 121 through 123 of your sample.
    Code:
    catch
    {}
    Out of interest - kbps is "kilobits per second"; it doesn't make sense to use this as a file size limit.

  3. #3
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??

    i agree with axion, your code should not have run in the first place, your catch should contain somthing like:

    catch (Exception ex) { <imo this can be left empty but for user-friendliness always try and inform the user that there is an error...> };

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??

    If you remove the try-catch block. what exception do you get?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width