通过阅读ContactDetail发送电子邮件 [英] Sending an email by reading a ContactDetail

查看:120
本文介绍了通过阅读ContactDetail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从包含List< t>的对象中读取电子邮件地址。作为字符串存储的emailaddresses,然后,向收件人发送电子邮件,但是,我不太确定如何从硬编码跳转到面向对象的编程模型,该模型允许通过哈希生成动态读取电子邮件地址ContactID。



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;
    }

推荐答案

首先,该片段不适用于绝大多数系统:它没有'包括任何授权,大多数电子邮件系统都需要这些授权来阻止我在邮件中欺骗你的身份。



看看这里:使用或不带附件在C#中发送电子邮件:通用例程。 [ ^ ] - 这是我使用的代码。



第二个问题是你描述的问题,我们无法回答:我们不能告诉你什么是这里?因为你的代码中没有任何东西甚至试图存储电子邮件地址与整数id值 - 所以我们无法开始计算你将如何倒退。

此外,ContactDetail类确实如此不直接链接到您在代码主体中引用的ContatcDetailEmail类 - 它不能从它派生,因为每个电子邮件联系人都会拥有它自己的单独列表或联系人,这简直是愚蠢的。 />


我认为你需要坐下来,看看你想要实现的目标,并从那里开始工作。除非您为ContactDetailEmail添加ID值,因此您有一个独特的值来引用整个系统,否则您无法执行任何类似于您所查找的内容。 (顺便说一句:我不会自己使用int - 它是一个PITA组织它们并决定哪个是下一个自由值 - 考虑使用GUID代替 - 我这样做!)
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!)


这篇关于通过阅读ContactDetail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆