Question

I have a method returning an email address as a string to a chunk of code that sends an email, but I'm getting the error: "The specified string is not in the form required for an e-mail address."

Here's the method that returns the email address:

public static string getSignerByID(int signerID)
{
    using (dbPSREntities10 myEntities = new dbPSREntities10())
    {
        var thisId = myEntities.refAuthSigners.Where(x => x.refAuthSignerID == signerID).Select(x => x.NetworkEmail).ToList();

        var formattedValue = thisId.ToString();

        return formattedValue;

    }
}

Here's the code that sends the email:

//method to send approved email
    public static void sendApprovedEmail(string contactFirstName, string contactLastName, string contactEmail, int signer)
    {
        //Email one
        string fileName = HttpContext.Current.Server.MapPath("~/App_Data/ApprovedEmail.txt");
        string mailBody = File.ReadAllText(fileName);

        mailBody = mailBody.Replace("##ContactFirstName##", contactFirstName);
        mailBody = mailBody.Replace("##ContactLastName##", contactLastName);

        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "PSR has been approved";
        myMessage.Body = mailBody;

        string signerEmailAddress = getSignerByID(signer);

        myMessage.From = new MailAddress("no-reply@test.com", "no-reply@test.com");
        myMessage.To.Add(new MailAddress(contactEmail));
        myMessage.To.Add(new MailAddress(signerEmailAddress));// this is the email address from the database that errors out.

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Send(myMessage);
    }

any idea why the variable "signerEmailAddress" wouldn't work? The value int he database is a varchar(50) and is definitely a valid email address. Thanks!

Was it helpful?

Solution

thisId will relturn list of network emails:

var thisId = myEntities.refAuthSigners
                       .Where(x => x.refAuthSignerID == signerID)
                       .Select(x => x.NetworkEmail)
                       .ToList();

Calling ToString() will return you list's type name. Something like:

"System.Collections.Generic.List`1[Namespace.NetworkEmail]"

Which definitely not looks like email address. Possibly you want to use FirstOrDefault() instead of ToList(). That will return you network address of first signer with provided id. You should check whether returned value is not null, and then call ToString() (if you have ToString() overridden on network email type).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top