Advertisement

08.21.2008 at 11:07PM PDT, ID: 23669377
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Can't send attachments bigger than 1400KB with SmtpClient

Asked by softwarea in .NET, C# Programming Language

Tags:

Hi guys,

my application sends emails including attachments via SMTP.

Everything works fine as long as the attached files do not exceed a size of 1.400KB in total. However, if I try to send an email with an attachment of 1.500KB or more the email will not reach it's receiver.

Here is some more background info:
- I'm using an SmtpClient object and the SendAsync() method.
- I added an event handler to the SmtpClient object in order to be notified when SendAsync() has been completed (see attached code). The weired thing: I get no error message. I seems like the emails really have been sent (including the attachments), because SmtpClient_OnCompleted() reports "E-MAIL SENT" (see attached code).
- The files I'm sending are dummies (created by a file creator that let's me generate files of any size). But I also tried "real" file types like PDF or GIF. No difference. So I believe the file type can not be the problem.
- Of course I've already tried different receiver email addresses to make sure it's not an issue of the receiver's mailbox limitations.

As I can send attachments with 1.400KB or less I believe that my code should be fine in general. Maybe there is some configuration setting that controls the maximum file sizes?

I would appreciate any help. It's quite urgent!

Thank you!
IngmarStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
private Exception SendEMail(string mailSubject, string mailBody, MailAddress mailFrom, MailAddressCollection mailReplyTo, MailAddressCollection mailTo, MailAddressCollection mailCC, MailAddressCollection mailBCC, List<string> mailAttachments)
        {
            Exception result = null;
 
            MailMessage EMail = new MailMessage();
 
            //Prepare EMail
            try
            {
                EMail.Subject = mailSubject;
                EMail.Body = mailBody;
                EMail.From = mailFrom;
 
                foreach (MailAddress address in mailTo)
                {
                    EMail.To.Add(address);
                }
                foreach (MailAddress address in mailCC)
                {
                    EMail.CC.Add(address);
                }
                foreach (MailAddress address in mailBCC)
                {
                    EMail.Bcc.Add(address);
                }
                foreach (MailAddress address in mailReplyTo)
                {
                    EMail.ReplyTo = address;
                }
                //Attachments
                foreach (string attFile in mailAttachments)
                {
                    if (File.Exists(attFile))
                    {
                        Attachment newAttachment = new Attachment(attFile);
                        EMail.Attachments.Add(newAttachment);
                    }
                }
 
                //Additional settings
                EMail.IsBodyHtml = true;
 
                //Send EMail
                //Read smtp host settings
                PvConfigurationEntity smtpHost = new PvConfigurationEntity();
                smtpHost.FetchUsingUCCfg_Key("SMTPHost");
                PvConfigurationEntity smtpUserName = new PvConfigurationEntity();
                smtpUserName.FetchUsingUCCfg_Key("SMTPUserName");
                PvConfigurationEntity smtpUserPassword = new PvConfigurationEntity();
                smtpUserPassword.FetchUsingUCCfg_Key("SMTPUserPassword");
 
                if (smtpHost.Cfg_Value != null && smtpHost.Cfg_Value != "")
                {
                    //Create smtp client
                    System.Net.Mail.SmtpClient SmtpClient = new System.Net.Mail.SmtpClient();
                    //Configute smtp client
                    //Smtp server
                    SmtpClient.Host = smtpHost.Cfg_Value;
                    //Smtp authentication
                    if (smtpUserName.Cfg_Value != null && smtpUserName.Cfg_Value != "")
                    {
                        //Use user credentials
                        SmtpClient.Credentials = new NetworkCredential(smtpUserName.Cfg_Value, smtpUserPassword.Cfg_Value);
                    }
                    //Wire up the event for when the Async send is completed
                    SmtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
                    //Result object
                    object userState = EMail;
                    //Send email
                    SmtpClient.SendAsync(EMail, userState);
                }
            }
            catch (Exception e)
            {
                result = e;
            }
            return result;
        }
 
        private void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //
            //Analyze the result of the email sending
            //
 
            //Get the Original MailMessage object
            MailMessage mail = (MailMessage)e.UserState;
            //Build protocol string
            //TO
            string summary = mail.Subject + ", To: " + mail.To.ToString();
            //CC
            if (mail.CC.ToString() != "")
                summary += ", CC: " + mail.CC.ToString();
            //BCC
            if (mail.Bcc.ToString() != "")
                summary += ", Bcc: " + mail.Bcc.ToString();
            //Attachments
            foreach (Attachment att in mail.Attachments)
            {
                summary += ", Attached: " + att.Name;
            }
            //FROM
            summary += ", From: " + mail.From + ", Date: " + DateTime.Now.ToString();
 
            //Evaluate result
            if (e.Cancelled)
            {
                LogEvent("E-MAIL CANCELED: " + summary, EventLogEntryType.Warning, 101, 100);
            }
            if (e.Error != null)
            {
                LogEvent("E-MAIL FAILED: " + summary + "***********" + e.Error.ToString(), EventLogEntryType.Error, 102, 100);
            }
            else
            {
                LogEvent("E-MAIL SENT: " + summary, EventLogEntryType.Information, 103, 100);
            }
            //Release all resources and handles on resources (e.a. handle on the attachments)
            mail.Dispose();
        }
[+][-]08.22.2008 at 04:25AM PDT, ID: 22288532

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, C# Programming Language
Tags: C#
Sign Up Now!
Solution Provided By: EricTViking
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08.22.2008 at 05:34AM PDT, ID: 22289095

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628