sherazkhan
sherazkhan
Reputation Top 5%
Sheraz Khan
2 Snippets  (290th place)
Published
1 Channel
Created
1 Channel
Following
56 points  (465th place)
Reputation
Junior Code Generator
Junior Publisher
Junior Trend Maker
Junior Popular Coder
Junior Autobiographer
Senior Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by sherazkhan

public by sherazkhan  2093  1  7  0

How to Reads Outlook PST File & Displays Folder or Message Information in Android Apps

This technical tip shows how to get the messages information from the Outlook PST file and display it inside .NET Applications. Previously we have discussed how to load an Outlook PST file and browse through its folders to get folder names and the number of messages in each folder. This topic explains how to read all the folders and subfolders in
public void RetrievePSTFolderContents()
    {
        String strBaseFolder = Environment.getExternalStorageDirectory().getPath() + "/";                
        String pstFileName = strBaseFolder + "Outlook.pst";

        try
        {
            // Load the Outlook PST file
            PersonalStorage pst = PersonalStorage.fromFile(pstFileName);

            // Get the root folder
            FolderInfo folderInfo = pst.getRootFolder();

            // Call the recursive method to display the folder contents
            DisplayFolderContents(folderInfo, pst);
        }
        catch (Exception ex)
        {
            tx.setText(ex.getMessage());
        }
    }

    private static void DisplayFolderContents(FolderInfo folderInfo, PersonalStorage pst)
    {        // Display the folder name
        tx.setText(tx.getText() + "\n Folder: " + folderInfo.getDisplayName());
        // Display information about messages inside this folder
        MessageInfoCollection messageInfoCollection = folderInfo.getContents();
        for (int i = 0; i < messageInfoCollection.size(); i++)
        {
            MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get(i);
            tx.setText(tx.getText() + "\n Subject: " + messageInfo.getSubject());
            tx.setText(tx.getText() + "\n Sender: " + messageInfo.getSenderRepresentativeName());
            tx.setText(tx.getText() + "\n To: " + messageInfo.getDisplayTo());
            tx.setText(tx.getText() + "\n CC: " + messageInfo.getDisplayCC());
            tx.setText(tx.getText() + "\n EntryID: " + messageInfo.getEntryIdString());
        }

        // Call this method recursively for each subfolder
        if (folderInfo.hasSubFolders() == true)
        {
            for (int i = 0; i < folderInfo.getSubFolders().size(); i++)
            {
                FolderInfo subfolderInfo = (FolderInfo) folderInfo.getSubFolders().get(i);
                DisplayFolderContents(subfolderInfo, pst);
            }
        }
    }
          
;

public by sherazkhan  2316  1  6  0

How to Send Email Messages using DNS inside C# & VB.NET Apps

This technical tip explains how to send an email messages using DNS inside .NET applications. Sometimes sending mails using an SMTP server is not feasible for a project’s requirement. We may want to utilize the MX record of the recipient’s domain name.
// Enter here the actual content of the snippet.            
static void Main(string[] args)
{
    try
    {
        MailMessage msg = new MailMessage("add1@domain.com", "add1@domain.com", "test", "this is a test");
        msg.CC.Add(new MailAddress("add2@domain.com", "CC Display Name"));
        DnsSendMessage(msg);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

    Console.WriteLine("Program finished. Press any key to exit....");
    Console.ReadKey();
}

/// <summary>
/// Use for sent message
/// </summary>
/// <param name="msg">message to send</param>
/// <returns></returns>
private static void DnsSendMessage(MailMessage msg)
{
    // Get all the recipients in to, cc and bcc in one collection
    MailAddressCollection addresses = new MailAddressCollection();
    foreach (MailAddress to in msg.To)
    {
        addresses.Add(to);
    }
    foreach (MailAddress cc in msg.CC)
    {
        addresses.Add(cc);
    }
    foreach (MailAddress bcc in msg.Bcc)
    {
        addresses.Add(bcc);
    }

    // send mail using DNS to each address
    foreach (MailAddress addr in addresses)
    {
        //Find mail exchange servers with the help of DnsClient
        DnsClient dnsClient = new DnsClient();
        Question mxQuestion = new Question(addr.Host, QueryType.MX);
        if (dnsClient.Resolve(mxQuestion))
        {
            // try to send a message
            foreach (ResourceRecord record in dnsClient.ReceivedMessage.Answers)
            {
                MXResourceRecord cnRecord = record as MXResourceRecord;
                if (cnRecord != null)
                {
                    try
                    {
                        // Send message
                        SmtpClient client = new SmtpClient();
                        client.AuthenticationMethod = SmtpAuthentication.None;
                        client.Host = cnRecord.ExchangeName;
                        client.Port = 25;
                        client.Send(msg);
                        Console.WriteLine("Mail sent to " + addr.Address);
                    }
                    catch (SmtpException e)
                    {
                        Console.WriteLine(cnRecord.ExchangeName + ": " + e.Message + Environment.NewLine);
                        continue;
                    }
                }
            }
        }
    }
}
;