Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to read an email address from an object which contains a List<t> of emailaddresses stored as strings, and, send an email to the recipient, however, I am not quite sure how to make the leap from hard coding to an object oriented programming model which allows the dynamic reading of the email address through a hash-generated ContactID.

public void NotifyByEmail (String messageToSend, ulong ContactID)
    {
        //1. read ContactEmail and Name through ContactID, temporarily hard coded.
        String emailAddress = "ben@contoso.com";
        ContactDetailEmail ce = new ContactDetailEmail("ben@contoso.com");

        //2. prepare email
        String senderFirstName = "Jon";
        String senderSecondName = "C.";
        SmtpClient client = new SmtpClient();
        MailAddress from = new MailAddress(emailAddress, 
               senderFirstName + (char)0xD8 + senderSecondName, System.Text.Encoding.UTF8);
        /* See http://en.wikipedia.org/wiki/UTF-8  */
        MailAddress to = new MailAddress("ben@contoso.com");
        MailMessage message = new MailMessage(from, to);
        message.Body = "This is a test e-mail message sent by an application. ";
        // Include some non-ASCII characters in body and subject. 
        string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
        message.Body += Environment.NewLine + someArrows;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "test message 1" + someArrows;
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        // Set the method that is called back when the send operation ends.
        string userState = "test message1";
        client.SendAsync(message, userState);

        #if DEBUG
        Console.WriteLine("DEBUG: Sending message");
        #endif
        /* Code snippet sourced from http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx */

    
    }

ContactDetail.cs
public static List<ContactDetailEmail> ContactDetailEmail = new List<ContactDetailEmail>();
    public String getContactDetailEmail(Int32 ContactID)
    {
        //what goes here?
        return null;
    }
Posted

1 solution

First off, that snippet isn't going to work in the huge majority of systems: it doesn't include any authorisation, which most email systems require to prevent me spoofing your id in messages.

Have a look here: Sending an Email in C# with or without attachments: generic routine.[^] - it's the code I use.

The second problem is the one you describe, and we can't answer that: We can't tell you "what goes here?" because there is nothing in your code which even tries to store email addresses against an integer id value - so we can't begin to work out how you would go backwards.
In addition, the ContactDetail class does not directly link to the ContatcDetailEmail class that you refer to in the main body of the code - it can't be derived from it, as each email contact would then have it's own separate list or contacts, which is just plain silly.

I think you need to sit down, look at what you are trying to achieve, and work back from there. Unless you add an ID value to your ContactDetailEmail so you have a unique value to refer to across teh whole system, you can't do anything like the look up you are after. (BTW: I wouldn't use an int for this myself - it's a PITA organising them and deciding which is the "next" free value - consider using a GUID instead - I do!)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900