views:

81

answers:

1

Hi All,

I have added a bcc field to my email that I am sending out and getting the error in the subject line. Everything works fine when I comment out the BCC line so I am not sure what is wrong. Here is the code I am using (it is in Delphi .NET but the principal is the same as C# and VB):

procedure TEmail.SendEmail(From, SendTo, CC, Subject, Body, BCC: String; Html: Integer);
var
  Mail: MailMessage;
  Smtp: SMTPClient;
begin
  Mail := MailMessage.Create(From, SendTo);
  Smtp := SMTPClient.Create;

  if CC <> '' then Mail.CC.Add(CC);
  if BCC <> '' then Mail.Bcc.Add(BCC);
  Mail.Subject := Subject;
  Mail.Body := Body;
  if Html = -1 then Mail.IsBodyHtml := True else Mail.IsBodyHtml := False;

  Smtp.DeliveryMethod := SmtpDeliveryMethod.PickupDirectoryFromIis;
  try
     Smtp.Send(Mail);
  except
     Smtp.DeliveryMethod := SmtpDeliveryMethod.Network;
     try
        Smtp.Send(Mail);
     except
        raise;
     end;
  end;
end;

I have tried playing around using MailAddress and MailAddressCollection instead but still recieve the same error. WHen I debug it, the params passed through are:

SendEmail([email protected], [email protected], [email protected], [email protected], Thanks for registering, Some Body Text);

As you can see, the CC and BCC email addresses are the same but I only get an error when the Mail.Add.Bcc line is in there.

EDIT: Part of the Stack Trace:

[FormatException: The specified string is not in the form required for an e-mail address.]
   System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +1296
   System.Net.Mail.MailAddressCollection.ParseValue(String addresses) +102

Does anyone have any ideas why this is the case?

+1  A: 

I count 6 parameters in the call, but 7 in the signature - and the order in the call doesn't even seen to match that in the signature; the method expects the subject and the body to be before the BCC address, but they're placed after the BCC address instead.

If we assume that the discrepancy in the number of parameters is due to an overloaded version which just calls this function with a default value, then it seems most likely that the problem is the parameter order: you're passing the method body where you should be passing the BCC address, and obviously the body won't work as a BCC address.

Michael Madsen
Sorry, I missed part of my debug out, there should be a -1 at the end; I trimmed some of the HTML out of the body on the debug to avoid a longer post so the paramters actually have 7 each and there is no overloaded versions. However, I see my silly mistake now .. Oh dear *blush*. Thanks :)
webnoob
p.s I guess my brain is still (trying) to wake up :)
webnoob
@webnoob: Don't worry; happens to the best of us :)
Michael Madsen