New Outlook window Using C# in IIS - c#

I tried to open outlook window with body to send a message.
When I works with my local it's opening perfectly. But when I work with IIS the outlook window is not opening.
public static void SendMailInOutlook(Page CurrentPage, Type ScriptType,
string Subject, string Recipients, Dictionary<string, string> emailInputs,
string emailTemplatePath)
{
static Microsoft.Office.Interop.Outlook.Application outlookApp;
static Microsoft.Office.Interop.Outlook._MailItem mailItem;
outlookApp = new Microsoft.Office.Interop.Outlook.Application();
mailItem = (Microsoft.Office.Interop.Outlook._MailItem)outlookApp.CreateItem(
Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.Subject = Subject;
mailItem.To = Recipients;
mailItem.HTMLBody = SendMail.GetEmailContent(emailInputs, emailTemplatePath);
Thread newThread = new Thread(SendEmailToOutLook);
newThread.Start();
}
static void SendEmailToOutLook()
{
string sMsg = "Email sent successfully.";
try
{
mailItem.Display(true);
}
catch (Exception ex)
{
sMsg = ex.Message;
}
}

And it won't.
Your codebehind is executed on the web server, NOT on the client machine. So when you develop locally, and run the web app locally, that code is executed on the same machine, hence "working".
But once you deploy, the "Outlook code" is executed on a different machine than the one the browser is on. It cannot work.
The most common approach to this requirement is a "mailto link", the likes of <a href='mailto:a#b.com'...

Related

Interact between Outlook and my WPF application

So, this may be a silly question (or something that's impossible), but I just wanted to ask in case anyone knows something. I'm trying to open Outlook (either through Office 365- Browser or through the Outlook Desktop Application) to compose an email. I got that working fine. What I want to know is, if there's a way that I can capture what was composed (like, Body, To, Subject, Attachments) in my WPF application so that I can update it on my end. Do you guys think if this is possible?
Here's the sample code I have: (For opening this in the browser)
string To = "abc#ftr.com";
string subject = "Test Email";
string body = "This is a test email, Please ignore";
string url = #"https://outlook.office.com/?path=/mail/action/compose&to=" + To + "&subject=" + subject + "&body=" + body;
System.Diagnostics.Process.Start(url);
And here's the code for opening it in Outlook Desktop App:
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.Subject = "subject something";
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = "Test Email";
oMsg.Attachments.Add("c:/temp/test.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
oMsg.Display(true);
Thank you!
You can hook the ItemSend event, which will give you a reference to the MailItem object that is about to be send. Here is some sample code I copied from the Microsoft Community Forums
public void SendEnMail(Office.IRibbonControl control) //OnAction Function
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
myMail.Display(true);
Outlook.Application application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
System.Windows.Forms.MessageBox.Show(a);
Cancel = true;
}

getting permission excheption at Application.CreateItem(OlItemType.olMailItem)

I want to open outlook 2010 for creating a new message , but I get the following exception.
I use visual studio 2012 , on windows server 2008 R2 standard , 64-bit operating system.
This is my code:
private void SendMail()
{
try
{
Microsoft.Office.Interop.Outlook.Application application =
new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem =
application.CreateItem(
Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as
Microsoft.Office.Interop.Outlook.MailItem;
mailItem.To = "someone#example.com";
mailItem.Subject = "my subject";
mailItem.Body = "message body";
mailItem.Display();
}
catch (System.Exception ex)
{
throw ex;
}
}
The exception:
System.Runtime.InteropServices.COMException was caught HResult=-2147287035
Message=you do not have appropriate permission to perform this operation.
Source=Microsoft Outlook
ErrorCode=-2147287035
StackTrace:
at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType)

Trying to Save Outlook Email in a Folder

I have a WinForms app that at the click of a button automatically produces an Outlook mail as follows:
public static void CreateOutlookEmail(string pFileName, string pCaseFolder, string pEmail, string pSubject, string pMessage)
{
try
{
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = pSubject;
mailItem.To = pEmail;
mailItem.Body = pMessage;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(false);
string fileDetails = pCaseFolder + "\\" + pFileName + #".eml";
mailItem.SaveAs(fileDetails);
}
catch (Exception eX)
{
throw new Exception("cDocument: Error occurred trying to Create an Outlook Email"
+ Environment.NewLine + eX.Message);
}
}
The code succesfully opens a new Outlook email, and populates it with the details sent into the method e.g. email address, subject and the body of the message.
Also when I locate the folder (sent in as a paramater) I can see the email document has been saved.
The issue is, that when I open the email from the folder, the email document is totaly blank ii.e. no email address, subject or message.
What am I doing wrong?
Your code is fine. Just use extension ".msg" instead of ".eml". Also the eml format does not exist under Outlook.OlSaveAsType

how to allow permanent access to outlook through C#

I have tried to access and read the outlook mail. I have tried following code but it gives me an security warning popup by saying "A program is trying to access e-mail address information stored in outlook express." when I try to access Microsoft.Office.Interop.Outlook.MailItem in the foreach.
const string OUTLOOK_PROCESSNAME = "OUTLOOK";
const string OUTLOOK_APPLICATIONNAME = "Outlook.Application";
private static Microsoft.Office.Interop.Outlook.Application StartOutlookApplication()
{
return StartApplication(OUTLOOK_PROCESSNAME, OUTLOOK_APPLICATIONNAME) as Microsoft.Office.Interop.Outlook.Application;
}
private static object StartApplication(string processName, string applicationName)
{
// Application object
object app = null;
try
{
// is there an existing application object ?
if (Process.GetProcessesByName(processName).Length > 0)
{
// use the GetActiveObject method to attach an existing application object
app = Marshal.GetActiveObject(applicationName);
}
if (app == null)
{
// create a new instance
Type t = Type.GetTypeFromProgID(applicationName);
app = Activator.CreateInstance(t);
}
}
catch (System.Exception ex)
{
// Some Logging
Trace.WriteLine(string.Format("Error while starting the Application: {0}", applicationName));
}
return app;
}
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
Microsoft.Office.Interop.Outlook.Application app = StartOutlookApplication();
Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
lastupdateddate = getmostrecentupdatetime();
DateTime lastupdated=Convert.ToDateTime(lastupdateddate);
subFolder = inboxFld.Folders[Inboxpath];
foreach (Microsoft.Office.Interop.Outlook.MailItem t in subFolder.Items)
{
if (t.SenderEmailAddress.Contains(senderemail))
{
Please some one help me.I need to run my program without showing this warning message.
The redemption library will get around most of them.
http://www.dimastr.com/redemption/

Uploading files to file server using webclient class

Currently I have an application that receives an uploaded file from my web application. I now need to transfer that file to a file server which happens to be located on the same network (however this might not always be the case).
I was attempting to use the webclient class in C# .NET.
string filePath = "C:\\test\\564.flv";
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential(uName, password);
Uri addy = new Uri("\\\\192.168.1.28\\Files\\test.flv");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, filePath);
Console.WriteLine(arrReturn.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The machine located at 192.168.1.28 is a file server and has a share c:\Files.
As of right now I am receiving an error of Login failed bad user name or password, but I can open explorer and type in that path login successfully. I can also login using remote desktop, so I know the user account works.
Any ideas on this error?
Is it possible to transfer a file directly like that? With the webclient class or maybe some other class?
Just use
File.Copy(filepath, "\\\\192.168.1.28\\Files");
A windows fileshare exposed via a UNC path is treated as part of the file system, and has nothing to do with the web.
The credentials used will be that of the ASP.NET worker process, or any impersonation you've enabled. If you can tweak those to get it right, this can be done.
You may run into problems because you are using the IP address instead of the server name (windows trust settings prevent leaving the domain - by using IP you are hiding any domain details). If at all possible, use the server name!
If this is not on the same windows domain, and you are trying to use a different domain account, you will need to specify the username as "[domain_or_machine]\[username]"
If you need to specify explicit credentials, you'll need to look into coding an impersonation solution.
namespace FileUpload
{
public partial class Form1 : Form
{
string fileName = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "";
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Attach customer proposal document";
fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
fileName = System.IO.Path.GetFileName(fDialog.FileName);
path = Path.GetDirectoryName(fDialog.FileName);
textBox1.Text = path + "\\" + fileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("erandika1986", "123");
Uri addy = new Uri(#"\\192.168.2.4\UploadDocs\"+fileName);
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, textBox1.Text);
MessageBox.Show(arrReturn.ToString());
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
}
}
when you manually open the IP address (via the RUN command or mapping a network drive), your PC will send your credentials over the pipe and the file server will receive authorization from the DC.
When ASP.Net tries, then it is going to try to use the IIS worker user (unless impersonation is turned on which will list a few other issues). Traditionally, the IIS worker user does not have authorization to work across servers (or even in other folders on the web server).

Categories