是否可以让用户从Windows中保存的联系人导入联系人?

时间:2011-10-13 04:18:54

标签: c# windows email contacts

我最近写了一个短信程序(通过电子邮件发送短信,而不是),我想让它比现在更有用......目前它只不过是一个垃圾邮件程序,尽管在我辩护中文本数量的默认值是1.无论如何......不是让用户输入电话号码,而是可以让我的程序打开一个对话框,允许用户从某处保存的联系人列表中进行选择视窗? (这是否存在?Outlook联系人可能?)

下面我提供了我到目前为止的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace TextingThroughEmail
{
    public partial class Form1 : Form
    {
        // message elements
        MailAddress mTo = null;
        MailAddress mFrom = new MailAddress("{You ain't seeing this either.}","<< Your Imaginary Friend >>");
    private string mFromPassword = "{lol, you ain't seeing this}";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        var result = MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes){ }
        else { e.Cancel = true; }
    }

    private void ExitButton_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void SendButton_Click(object sender, EventArgs e)
    {

        //This section will retrieve data from the combo box.
        EmailInfo data = (EmailInfo)Carrier.SelectedItem;

        //This section goes through the PHONE NUMBER and validates it            
        long number1 = 0;
        bool canConvert = long.TryParse(PhoneNumber.Text, out number1);
        if (canConvert == false)
        {
            MessageBox.Show("Please enter a valid 10 digit phone number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        if(PhoneNumber.Text.Trim().Length != 10)
        {
            MessageBox.Show("Please enter a valid 10 digit phone number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        //Vaidation for the message section
        if (Message.Text.Trim().Length > 160)
        {
            MessageBox.Show("Please shorten your message.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        mTo = new MailAddress(PhoneNumber.Text + data.EmailAddress, "");
        MailAddress mTo2 = new MailAddress(PhoneNumber.Text + "@txt.att.net", "");
        MailAddress mTo3 = new MailAddress(PhoneNumber.Text + "@vtext.com", "");
        MailAddress mTo4 = new MailAddress(PhoneNumber.Text + "@tmomail.net", "");
        MailAddress mTo5 = new MailAddress(PhoneNumber.Text + "@messaging.sprintpcs.com", "");

        //The mail message setup
        MailMessage message = new MailMessage(mFrom, mTo);
        message.Subject = Subject.Text;
        message.Body = Message.Text;

        //messages for Unknown
        //At&t
        MailMessage message2 = new MailMessage(mFrom, mTo2);
        message.Subject = Subject.Text;
        message.Body = Message.Text;
        //verizon
        MailMessage message3 = new MailMessage(mFrom, mTo3);
        message.Subject = Subject.Text;
        message.Body = Message.Text;
        //t mobile
        MailMessage message4 = new MailMessage(mFrom, mTo4);
        message.Subject = Subject.Text;
        message.Body = Message.Text;
        //sprint
        MailMessage message5 = new MailMessage(mFrom, mTo5);
        message.Subject = Subject.Text;
        message.Body = Message.Text;


            //smtp stuff
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(mFrom.Address, mFromPassword);

            long j;
            canConvert =  long.TryParse(NumberOfTexts.SelectedItem.ToString(), out j);

            try
            {
                if (Carrier.SelectedIndex < 4)
                {
                    for (int i = 0; i < j; i++)
                    {
                        client.Send(message);
                    }
                    MessageBox.Show("Sent all messages", "Hooray!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else //this is for the unknown bit.
                {
                    for (int i = 0; i < j; i++)
                    {
                        client.Send(message2);
                        client.Send(message3);
                        client.Send(message4);
                        client.Send(message5);
                    }
                    MessageBox.Show("Sent all messages", "Hooray!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                  ex.ToString());
            }             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Carrier.Items.Add(new EmailInfo("@txt.att.net","AT&T"));
            Carrier.Items.Add(new EmailInfo("@vtext.com","Verizon"));
            Carrier.Items.Add(new EmailInfo("@tmomail.net", "T-Mobile"));
            Carrier.Items.Add(new EmailInfo("@messaging.sprintpcs.com", "Sprint"));
            Carrier.Items.Add(new EmailInfo("@randomnezzle.com", "Unknown (A LOT Slower)"));           
            Carrier.SelectedIndex = 0;
            NumberOfTexts.SelectedIndex = 0;
        }

        private void Carrier_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

Outlook是人们存储联系人的典型地方。

这是一个代码片段,用于通过C#枚举所有Outlook联系人:

Enumerating Outlook ContactItem properties

和另一个:

http://dotnet-snippets.com/dns/get-all-outlook-contacts-SID611.aspx

此外,这里是outlook对象模型的概述:

http://msdn.microsoft.com/en-us/library/ms268893(v=vs.80).aspx