here is the example to save outlook mail attachment into our system folder or server folder
you can uplaod the file with the below paradigm, the below programme is to upload the jpg file formate.
Step 1 : Open visual studio --> Start New Project (using C# or VB)
Step 2: In the solution explorer 'Right Click', add new item --> add web page into your website
Step 3: search Microsoft.Exchange.WebServices from Manage NuGate
Step 3 : install Microsoft.Exchange.WebServices library to your project
Step 4 : after install the Microsoft.Exchange.WebServices Library , write your code as below
Step 5 : Need to connect to your exchange server
Write below method to your website to connect to the exchange server and outlook email
using Microsoft.Exchange.WebServices.Data;
public void ConnectToExchangeServer()
{
try
{
exchange = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
exchange.Credentials = new WebCredentials("Your Account Name on Exchange Server", "Password", "Domain");
exchange.AutodiscoverUrl("Outlook Email Address");
Response.Write( "Connected to Exchange Server : " + exchange.Url.Host );
}
catch (System.Exception ex)
{
Response.Write( "Error Connecting to Exchange Server!!" + ex.Message);
}
}
you can uplaod the file with the below paradigm, the below programme is to upload the jpg file formate.
Step 1 : Open visual studio --> Start New Project (using C# or VB)
Step 2: In the solution explorer 'Right Click', add new item --> add web page into your website
Step 3: search Microsoft.Exchange.WebServices from Manage NuGate
Step 3 : install Microsoft.Exchange.WebServices library to your project
Step 4 : after install the Microsoft.Exchange.WebServices Library , write your code as below
Step 5 : Need to connect to your exchange server
Write below method to your website to connect to the exchange server and outlook email
using Microsoft.Exchange.WebServices.Data;
using System.IO
public void ConnectToExchangeServer()
{
try
{
exchange = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
exchange.Credentials = new WebCredentials("Your Account Name on Exchange Server", "Password", "Domain");
exchange.AutodiscoverUrl("Outlook Email Address");
Response.Write( "Connected to Exchange Server : " + exchange.Url.Host );
}
catch (System.Exception ex)
{
Response.Write( "Error Connecting to Exchange Server!!" + ex.Message);
}
}
Step 5 : write the code behind on button click event or page load as per requirement
the below event will read your outlook email and get access to every function
using the namespace
using Microsoft.Exchange.WebServices.Data;
using System.IO
ConnectToExchangeServer();
TimeSpan ts = new TimeSpan(0, -1, 0, 0);
DateTime date = DateTime.Now.Add(ts); // It will read only the current date email
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);
if (message.HasAttachments)
{
foreach (Microsoft.Exchange.WebServices.Data.Attachment attach in
message.Attachments)
{
if (attach is FileAttachment)
{
string file = message.Sender.Name.ToString();
string FileFolder = @"C:\\" + file + "\\"; // save your attachment here
if (!System.IO.Directory.Exists(FileFolder)) // it will create sender name directory
{
System.IO.Directory.CreateDirectory(FileFolder);
}
FileAttachment fileAttachment = attach as FileAttachment;
fileAttachment.Load();
FileStream theStream = new FileStream(FileFolder + fileAttachment.Name,
FileMode.OpenOrCreate, FileAccess.ReadWrite);
fileAttachment.Load(theStream);
theStream.Close();
theStream.Dispose();
}
}
}
}
if (findResults.Items.Count <= 0)
{
lblMsg.Text = "No Messages found!!";
}
}
thank you for viewing this.
this will allow you to create directory in disk folder and save the sender attachments in to disk folder.
No comments:
Post a Comment