Adding text to an email using an Outlook addin C# 2010 - c#

I want to add some text to the body of my email. But i keep getting this error:
Error 1 'EmailHelper.EmailHelperRibbon' does not contain a definition for 'Application' and no extension method 'Application' accepting a
first argument of type 'EmailHelper.EmailHelperRibbon' could be found
(are you missing a using directive or an assembly reference?)
My code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
namespace EmailHelper
{
public partial class EmailHelperRibbon
{
private void EmailHelperRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Your Ribbon Works!");
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)
this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
{
mailItem.Subject = "This text was added by using code";
mailItem.Body = "This text was added by using code";
}
}
}
}

Try this one:
var inspector = this.Context as Outlook.Inspector;
var currentMailItem = inspector.CurrentItem as Outlook.MailItem;
currentMailItem.Body = "your text";

I am guessing this is a Ribbon based on the name, so you need to access the application off of the Globals class:
Globals.ThisAddIn.Application.CreateItem(...)

Related

C# How to upload any file to a fileuploader?

I need upload my file to "https://file.io" and i tried this:
`private void buttonInput_Click(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
var resStr = client.UploadFile("https://file.io", #"C:\data\test.txt");
var jObjResult = JObject.Parse(Encoding.UTF8.GetString(resStr));
var linkToFile = jObjResult["link"];
}
}`
It does upload target txt file to url.
But im taking that error:
Severity Code Description Project File Line Hide Status
Error CS01 'JObject' scope valid03
How to Fix it?
Btw im using them:
using System;
using System.Net;
using System.Text;
using System.Collections.Generic;

COMException C# Microsoft.Office.Interop.Excel

I am trying to solve an issue I am having with a COMException. This is my code:
The error occurs at Workbook Original = new Workbook(result[0]);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Data;
using Microsoft.Office.Interop.Excel;
namespace KPI_Generator_v3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
string [] result;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
public MainWindow()
{
InitializeComponent();
}
private void exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void browse_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
instructionslbl.Visibility = Visibility.Hidden;
dlg.Multiselect = true;
dlg.ShowDialog();
result = dlg.FileNames;
dlg.DefaultExt = ".xls";
dlg.Filter = "XLS Files (*.xls)|*.xls";
foreach (string fileName in result)
{
displayFilesBox.Text += fileName + System.Environment.NewLine;
}
SelectedFileslbl.Visibility = Visibility.Visible;
displayFilesBox.Visibility = Visibility.Visible;
generateBtn.Visibility = Visibility.Visible;
}
private void generate_Click(object sender, RoutedEventArgs e)
{
Workbook Original = new Workbook(result[0]);
for (int i = 1; i <= result.Length; i++)
{
Workbook next = new Workbook(result[i]);
Worksheet newOGsheet = Original.Worksheets.Add();
Worksheet nextSheet = next.Worksheets[0];
nextSheet.Copy(newOGsheet);
}
}
}
}
Now.. Although the above code will probably not work I am getting an error that states
'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
with additional information saying
HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
After googling for quite some time I have downloaded dependency walker and have the following output:
I have no idea what I am doing and some help would be much appreciated! Thank you
EDIT: REGEDIT picture of HKEY CLASSES_ROOT (if needed)
EDIT2: Picture of error
This error is a COM interop flaw unfortunately. Otherwise, you would be getting a compile error, since Workbook is an interface and doesn't have a constructor. This interface isn't meant to be instantiated, which is why you are getting this exception. (Not sure why you're passing in the filename here as well...)
To fix this, use Microsoft.Office.Interop.Excel.Application:
using Microsoft.Office.Interop.Excel;
Application excelApp = new Application();
excelApp.Workbooks.Open(result[0]);
Other notes:
You should call dlg.ShowDialog() last - that way, your filters will take effect.
For your filter, I suggest adding an option for .xlsx files, to support newer Excel files
Add result.Length > 0 before using it, just to make sure the user actually opened something.
Change your for loop condition to i < result.Length; otherwise, you'll get an IndexOutOfRangeException
Also, just to add more information: you may be asking: "but wait, ryrich, Application is an interface too! How come I can instantiate it but not Workbook??"
I wondered this too. Apparently this works because it is decorated with a CoClassAttribute (and some other attributes). For more detailed information about this, see this stack overflow post.

C# MailMessage Display email instead of Send

I have been following the example below to send email using C# code with success:
MSDN Mail Message
However, I would like the code to display the composed email message on user machine, so that user can have a final check before hitting send button on outlook.
In the VBA world, I can use mail.Display in place of mail.Send.
Can anyone provide some advice to achieve that in C#?
Thanks.
How about this...
private void btnEmail_Click(object sender, EventArgs e)
{
string command = "mailto:somebody#domain.com?subject=The Subject&bcc=another#codegaim.com&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/";
Process.Start(command);
}
Found a great solution to my problem i.e. to use Microsoft Office Interop Outlook instead of System.Net.MailMessage
followed this:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
//using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
using Outlook = Microsoft.Office.Interop.Outlook;
public static class Program
{
static void Main(string[] args)
{
SendUsingAccountExample();
}
private static void SendUsingAccountExample()
{
var application = new Application();
var mail = (_MailItem)application.CreateItem(OlItemType.olMailItem);
mail.Body = "Hello";
mail.Subject = "Good Bye";
mail.To = "hello#google.com";
// Next 2 lines are optional. if not specified, the default account will be used
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mail.SendUsingAccount = account;
mail.Display(false); // To Display
//mail.Send(); // To Send
}
}
}

Call a 'host' callback from a 'compiled assembly from source'

I'm compiling code on-the-fly using System.CodeDom.Compiler. Everything inside the compiled source works well, whatever I'm putting inside this source. I know how to call my functions:
o = results.CompiledAssembly.CreateInstance("Foo.Bar");
MethodInfo mi = o.GetType().GetMethod("SayHello");
mi.Invoke(o, null);
But let's say I'm using a WebClient to retrieve a string asynchronously using WebClient.DownloadStringAsync. Or any other context where I want my compiled source to tell to the host "Hey, I got a nice string ready for you." For the example, I've used a WebBrowser. Basically, I know how to deal with each of the two instances: My hosting program and the compiled program, but I want my compiled program to communicate with the host. By the way, I'm not a super-experimented programmer, so no obvious method comes to my mind.
What I've tried:
1 . I don't really need to try it because it would work, but I could use a timer reading a strings stack or tasks queue inside the compiled source, but the purpose of my application is to have +- 60 scripts able to execute ponctual tasks, not continuous background processes, so it wouldn't be efficient on the CPU.
2 . I've passed the handler to the compiled source like if it was in the hosting app:
//In the hosting app
MethodInfo mi2 = o.GetType().GetMethod("attachCallbackToHost");
mi2.Invoke(o2, new object[] { new WebBrowserNavigatedEventHandler (wb_navigated) });
//... And the handler
public static void wb_navigated(object sender, WebBrowserNavigatedEventArgs e)
{
string browserHtmlFromCompiledSource = ((WebBrowser)sender).DocumentText;
MessageBox.Show(browserHtmlFromCompiledSource);
}
// Plain text from the compiled source code
public void attachCallbackToHost(WebBrowserNavigatedEventHandler handlerFromTheHost)
{
wb.Navigated += handlerFromTheHost;
}
And it did nothing.
3 . Maybe I could share a class or variable by passing it to the compiled assembly?
So, the question is either this or the other:
How to watch efficiently for change inside a specific variable or property inside the compiled program?
How to attach a callback to the host?
Ok. I got it: In order to access the host from the compiled source, the only thing required is to add the host assembly to the refered assemblies in the compiler parameters:
compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
So no need for any special callback or INotifier.
Here's the full code that strictly answers my question and nothing more:
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 Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace MamaProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string source =
#"
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;
using MyMama = MamaProgram;
namespace Baby
{
public class Program
{
public WebBrowser wb = new WebBrowser();
public void navigateTo(string url)
{
wb.Navigated += wb_navigated;
wb.Navigate(url);
}
public void wb_navigated(object sender, WebBrowserNavigatedEventArgs e)
{
MyMama.Form1.getResult(wb.DocumentText);
}
}
}
";
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v3.5"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
TreatWarningsAsErrors = false
};
compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
compilerParams.ReferencedAssemblies.Add("System.Data.dll");
compilerParams.ReferencedAssemblies.Add(typeof(System.Linq.Enumerable).Assembly.Location); // Trick to add assembly without knowing their name
compilerParams.ReferencedAssemblies.Add(typeof(System.ComponentModel.Component).Assembly.Location); // Trick to add assembly without knowing their name
compilerParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
if (results.Errors.Count != 0)
throw new Exception("Compilation failed");
object o = results.CompiledAssembly.CreateInstance("Baby.Program");
MethodInfo mi2 = o.GetType().GetMethod("navigateTo");
mi2.Invoke(o, new object[] { "http://www.google.com" });
}
public static void getResult(string result)
{
MessageBox.Show(result);
}
}
}

C#: execute a function stored in a string variable

Is it possible to write a simple and fast function in C# that will execute arbitrary methods from a string? For example, if I set MyString="MessageBox.Show("Some Message")" and then call ExecuteString(MyString), a message box would pop up with "Some Message" in it.
(I've probably made some sort of error in the above code. I don't yet know C#; I'm trying to evaluate whether it would be appropriate for a specific project.)
You should be able to use this and wrap the code required to run a string into a function.
Essentially what you're doing is wrapping the small bit of C# code in a Program.Mainstyle function, referencing some assemblies for basic functionality (maybe including your own assembly) then run the compiled program in memory.
It's likely a bit of more overhead than you need to simply run one or two lines of code mind you.
http://support.microsoft.com/kb/304655
what you appear to be looking for is CS-Script
Alas, C# is not a dynamic language in that way. You can't really do this easily, and if it's really something you need to do, consider using a .Net language more in line with your needs, like IronPython or IronRuby.
Your best available alternative is to use the CodeDom namespace, as this truly convoluted and heinous example from this forum thread shows:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SampleLib.SampleType test = new SampleLib.SampleType();
private void button1_Click(object sender, EventArgs e)
{
// Dynamically build and call the method
label1.Text = test.MyText;
}
private void button2_Click(object sender, EventArgs e)
{
StringBuilder DynamicCode = new StringBuilder();
DynamicCode.Append("namespace TestDynamic");
DynamicCode.Append("{");
DynamicCode.Append("public class DynamicCode");
DynamicCode.Append("{");
DynamicCode.Append("public static void EditText(SampleLib.SampleType t)");
DynamicCode.Append("{");
DynamicCode.Append("t.MyText = \"Goodbye!\";");
DynamicCode.Append("}");
DynamicCode.Append("}");
DynamicCode.Append("}");
string CodeString = DynamicCode.ToString();
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
CompilerParameters CompileParams = new CompilerParameters(new string[] { fi.DirectoryName + "\\SampleLib.dll" },
fi.DirectoryName + "\\Dynamic.dll");
CompileParams.MainClass = "DynamicCode";
CompileParams.GenerateExecutable = false;
//CompileParams.GenerateInMemory = true;
CompilerResults r = provider.CompileAssemblyFromSource(CompileParams, new string[] {CodeString});
foreach (CompilerError er in r.Errors)
{
Console.WriteLine(er.ErrorText);
}
}
private void button3_Click(object sender, EventArgs e)
{
// Dynamically call assembly
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
Assembly dynAsm = Assembly.LoadFile(fi.DirectoryName + "\\Dynamic.dll");
if (dynAsm != null)
{
object o = dynAsm.CreateInstance("TestDynamic.DynamicCode", true);
Type t = dynAsm.GetType("TestDynamic.DynamicCode");
t.GetMethod("EditText").Invoke(o, new object[]{test});
}
}
}
}

Categories