Vidhi Patel

Vidhi Patel

  • NA
  • 44
  • 17.6k

The Url property on the ExchangeService object must be set.

Sep 13 2016 1:18 AM
here is my code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Exchange.WebServices.Data;
namespace ReadMailFromExchangeServer
{
public partial class Form1 : Form
{
ExchangeService exchange = null;
public Form1()
{
InitializeComponent();
lstMsg.Clear();
lstMsg.View = View.Details;
lstMsg.Columns.Add("Date", 150);
lstMsg.Columns.Add("From", 250);
lstMsg.Columns.Add("Subject", 400);
lstMsg.Columns.Add("Has Attachment", 50);
lstMsg.Columns.Add("Id", 100);
lstMsg.FullRowSelect = true;
}
public void ConnectToExchangeServer()
{
lblMsg.Text = "Connecting to Exchange Server..";
lblMsg.Refresh();
try
{
exchange = new ExchangeService(ExchangeVersion.Exchange2013);
exchange.Credentials = new WebCredentials("[email protected]", "Gani@love", "AutodiscoverUrl");
exchange.AutodiscoverUrl("[email protected]");
lblMsg.Text = "Connected to Exchange Server : " + exchange.Url.Host;
lblMsg.Refresh();
}
catch (Exception ex)
{
lblMsg.Text = "Error Connecting to Exchange Server!!" + ex.Message;
lblMsg.Refresh();
}
}
private void button1_Click(object sender, EventArgs e)
{
ConnectToExchangeServer();
TimeSpan ts = new TimeSpan(0, -1, 0, 0);
DateTime date = DateTime.Now.Add(ts);
SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
if (exchange != null)
{
FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
foreach (Item item in findResults)
{
EmailMessage message = EmailMessage.Bind(exchange, item.Id);
ListViewItem listitem = new ListViewItem(new[]
{
message.DateTimeReceived.ToString(), message.From.Name.ToString() + "(" + message.From.Address.ToString() + ")", message.Subject, ((message.HasAttachments) ? "Yes" : "No"), message.Id.ToString()
});
lstMsg.Items.Add(listitem);
}
if (findResults.Items.Count <= 0)
{
lstMsg.Items.Add("No Messages found!!");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (exchange != null)
{
if (lstMsg.Items.Count > 0)
{
ListViewItem item = lstMsg.SelectedItems[0];
if (item != null)
{
string msgid = item.SubItems[4].Text.ToString();
EmailMessage message = EmailMessage.Bind(exchange, new ItemId(msgid));
if (message.HasAttachments && message.Attachments[0] is FileAttachment)
{
FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
//Change the below Path
fileAttachment.Load(@"C:\\Users\\Admin\\Documents\\Visual Studio 2012\\Projects\\ReadMailFromExchangeServer\\ReadMailFromExchangeServer\\Attachments\\" + fileAttachment.Name);
lblAttach.Text = "Attachment Downloaded : " + fileAttachment.Name;
}
else
{
MessageBox.Show("No Attachments found!!");
}
}
else
{
MessageBox.Show("Please select a Message!!");
}
}
else
{
MessageBox.Show("Messages not loaded!!");
}
}
else
{
MessageBox.Show("Not Connected to Mail Server!!");
}
}
}
}
Here is my error how to slved it can you any one help me plez::
 
 

Answers (2)