Solved

CC and BCC not working when passing empty string

Posted on 2008-10-09
9
523 Views
Last Modified: 2013-12-17
Hi
I have attached some code, problem is this section:
 if (strCC != string.Empty || strCC != "")
        {
            MailAddress cc = new MailAddress(strCC.ToString());
            msg.CC.Add(cc);
       }
        if (strBCC != string.Empty || strBCC != "")
        {
            MailAddress bcc = new MailAddress(strBCC.ToString());
            msg.Bcc.Add(bcc);
      }

When i pass an empty string to CC or BCC, no email is sent, but if i remove the 'if blocks' and give CC and BCC a valid email it works ok.

Not all webpages will send a CC or BCC hence the 'if' blocks.
Anyone any idea on how to get the code to when the CC or BCC is an empty string.
Regards
George

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
 
/// <summary>
/// Summary description for PublicEmails
/// </summary>
public static class PublicEmails
{
    public static void PublicSendEmail(string strEmailFrom,
                                       string strEmailFromName,
                                       string strEmailTo,
                                       string strEmailToName,
                                       string strCC,
                                       string strBCC,
                                       string strSubject, 
                                       string strBody)
    {
        MailAddress from = new MailAddress(strEmailFrom.ToString(), strEmailFromName.ToString());
        MailAddress to = new MailAddress(strEmailTo.ToString(),strEmailToName.ToString());
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = strSubject.ToString();
        msg.Body = strBody.ToString();
        //if (strCC != string.Empty || strCC != "")
        //{
            MailAddress cc = new MailAddress(strCC.ToString());
            msg.CC.Add(cc);
        //}
        //if (strBCC != string.Empty || strBCC != "")
        //{
            MailAddress bcc = new MailAddress(strBCC.ToString());
            msg.Bcc.Add(bcc);
        //}
        SmtpClient mailClient = new SmtpClient("LocalHost");
        try
        {
            mailClient.Send(msg);
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    }
}

Open in new window

0
Comment
Question by:BeginningWebDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22676458
can you please put a light on what exception comes when you run your code and i will request to try my code block to send the email again

if (strCC != null && strCC != string.Empty)
{
            MailAddress cc = new MailAddress(strCC.ToString());
            msg.CC.Add(cc);
}

if (strBCC != null && strBCC != string.Empty)
{
            MailAddress bcc = new MailAddress(strBCC.ToString());
            msg.Bcc.Add(bcc);
}
0
 

Author Comment

by:BeginningWebDesign
ID: 22676484
Hi ragi0017:

Its not throwing an error, everything goes ok and the data is inserted into the database, i'm just not receiving the email

George
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22676532
sendmail does not throw an error when you try the if blocks for cc and bcc for empty strings and you also dont get the emails
thats quite surprising - if there are not exceptions then you should be getting the mails definitely
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22676560
Here's yours,

All email addresses must be matched with email expression before sending

string strPattern = @"[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}";


public static void PublicSendEmail(string strEmailFrom,
                                   string strEmailFromName,
                                   string strEmailTo,
                                   string strEmailToName,
                                   string strCC,
                                   string strBCC,
                                   string strSubject,
                                   string strBody)
{
    string strPattern = @"[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}";
 
    if (!Regex.IsMatch(strEmailFrom, strPattern) || !Regex.IsMatch(strEmailTo, strPattern))
    {
        return;
    }
 
    MailAddress from = new MailAddress(strEmailFrom, strEmailFromName);
    MailAddress to = new MailAddress(strEmailTo, strEmailToName);
    MailMessage msg = new MailMessage(from, to);
 
    msg.Subject = strSubject;
    msg.Body = strBody;
 
    if (Regex.IsMatch(strCC, strPattern))
    {
           MailAddress cc = new MailAddress(strCC.ToString());
           msg.CC.Add(cc);
    }
 
    if (Regex.IsMatch(strBCC, strPattern))
    {
        MailAddress bcc = new MailAddress(strBCC.ToString());
        msg.Bcc.Add(bcc);
    }
 
    SmtpClient mailClient = new SmtpClient("LocalHost");
    try
    {
        mailClient.Send(msg);
    }
    catch (Exception ex)
    {
        throw new ApplicationException(ex.Message);
    }
}

Open in new window

0
 

Author Comment

by:BeginningWebDesign
ID: 22676912
Hi
Found problem, its seems to be with Windows XP, i can send 2 test emails and they then end up in my Yahoo spam folder, then for some reason XP will not send anymore, i've looked in the C:\Inetpub\mailroot folders and no messages are there, restart XP and the test emails are then sent
George
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22680211
Hi BeginningWebDesign, do you happen to have any anti-virus software installed, such as Symantec?
0
 

Author Comment

by:BeginningWebDesign
ID: 22682452
Hi prairiedog:
I have McAfee, but i have opened the ports for sending mail
Regards
George
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22682498
Next time check the Queue folder under mailroot, you may find them sitting there.
I have Symantec installed and it keeps my outgoing emails in Queue folder until I reboot my machine. I end up with temporarily disabling Symantec real-time protection when I test my email function. Then turn the real-time protection back on after the test.
0
 

Author Closing Comment

by:BeginningWebDesign
ID: 31504564
Hi prairiedog:
That worked, still seems a bit odd for that to happen even though i have opened up port 110 and 25
Thanks
George
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question