Emailing the contents of a Textbox with Outlook - c#

I have a simple application written that allows users to pick inventory items with checkboxes. When the items are checked a textbox is populated showing the user's input. I would like to have a class that would take the contents of the textbox and copy it to a new outlook email with the TO address pre-populated with myemail#gmail.com. ASP.Net is foreign to me and I am a very new C# coder SO I have no idea how to do this. Any ideas.
I have seen an example online as follows...
System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
but I have errors everywhere and think that I'm not implementing this right. Is there a simpler way to do this or just a way I might be able to understand. Thanks to any and all answers. I can only check one but I appreciate them all.

http://support.microsoft.com/kb/310263
I am guessing you are not using the Outlook object library. If you want to, then the code is right there.
The only change you will have to make will be
oMsg.Body = TextBox1.text;
where TextBox1 has the all the contents that you wanted to send as the message body.

Related

C# Attach to open outlook email, edit and send

I am trying get some C# to attach to an open reply-email (triggered manually by user), on the already running instance of Outlook (opened manually by user). The code should identify the open reply email, edit the subject line and body of the email and send the email.
The problem is that I get as far as identifying the running instance of Outlook and assigning it to an object using one of the Marshal methodsoutApp = Marshal.GetActiveObject("Outlook.Application") as Application, but then I cannot cast it to a MailItem type in order to manipulate its elements e.g. the subject line, body, etc...something like MailItem mailItem = (MailItem)outApp.CreateItem((OlItemType.olMailItem)); throws an invalid cast exception at runtime.
Apologies if I am wrong, but could not find a single example close to this exact sequence of events, one of the closer ones is this post c# outlook open existing instance and reply to email
but then it goes a whole different way. There are tons of posts on how to use the Microsoft.Office.Interop.Outlook to OPEN and then use an instance of Outlook, but hardly anything (that I could find) on how to use an open instance. Any help is appreciated, thank you.
EDIT 08102019:
The code is used from an RPA platform, so there is no risk of it being picked up as malware. The "user" is just a virtual user on an account with purpose-made permissions and a controlled environment...sorry, nothing dark here :-). Anyway, here is the code I am using at the moment which creates a new instance and saves it to drafts in Outlook. It is not what I set out to do, as I explained above, this is just a temporary fix:
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.To = "test#test.com";
mailItem.Subject = "Test Email Generation";
mailItem.HTMLBody = "<html><body>This is the body of the email.</strong>.<br/> This is another line in the body of the email.</body></html>";
mailItem.Display(false);
System.Threading.Thread.Sleep(3000);
mailItem.Close(OlInspectorClose.olSave);
Marshal.ReleaseComObject(outlookApp);
To get the opened mail item in the inspector window you need:
Use the ActiveInspector method to get an instance of the Inspector class.
The Inspector.CurrentItem property returns an Object representing the current item being displayed in the inspector.
Set any properties like Subject, Body, Recipients and etc.
To get the inline response in the Explorer window you need to use the Explorer.ActiveInlineResponse property which returns an item object representing the active inline response item in the explorer reading pane.

Outlook.MailItem custom properties issue

i have the following situation for my Add-In (Office >= 2010):
I want to add some custom properties to an Outlook.MailItem (property must be mail associated) while the mailtext is written.
If this mail is sent i want to grap the send event and get the previously set properties again, doing something and removing the properties and continue sending.
Problem if i use PropertyAccessor:
I use it as follows to save the property while writing the mail:
string propTag = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/test_property"
mailItem.PropertyAccessor.SetProperty(propTag, value);
And to read the property again on sending the mail:
string propTag = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/test_property"
string readProperty = mailItem.PropertyAccessor.GetProperty(propTag);
works if cached mode is enabled on exchange
works NOT if cached mode isn't enabled... i can't find the previously setted properties anymore (Exception with unknown property is thrown)
OutlookSpy (http://www.dimastr.com/outspy/home.htm) can find the property on sending so does anyone knows how to read the properties in a different way?
I would thank you very much for every help.
You need to call Save if you want your changes to be persisted.

Read Undelivered email body in C# windows form

I am developing one tool to keep track of emails and would like to trace the emails undelivered to sender.
I dont want to use any third party tool or any external reference
What I have tried?
-To read report Item but Body is in Chinese like language
-Googled on some solutions but nothing is working i.e. solution is related to
//PR_TRANSPORT_MESSAGE_HEADERS
link to get property name "http://schemas.microsoft.com/mapi/proptag/0x0C1A001F"
outlook PropertyAccessor class
PropName link is not working anymore.
Could anyone here please help me I would like to get from which sender the email delivery is failed.
Thank you in advance.
The PR_TRANSPORT_MESSAGE_HEADERS property contains the same Diagnostic information shown in the body. You just need to read it using the PropertyAccessor class and parse the string with From and To entries.
Outlook.PropertyAccessor oPA = reportItem.PropertyAccessor;
string transportHeaders = (string)oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");

Outlook Interop ClearSelection Method

I am having difficulties with Interop in a WPF application.
What I actually want to do is drag and drop an Outlook file into my application and extract the attachments and store them. Apart from that I want to read the subject and search for a 4-digit-number which will then be the name of the folder the attachments are to be stored to.
I have been searching the web for solutions that don't use Interop, but I wasn't able to find anything that worked for me.
So I thought 'let's give it a shot' and it sounded pretty simple, because I found so many examples that followed this pattern:
if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.Selection selection = app.ActiveExplorer().Selection;
foreach (object mi in selection)
{
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)mi;
string subject = "Untitled";
if (!string.IsNullOrEmpty(mailItem.Subject))
{
subject = mailItem.Subject;
MessageBox.Show(subject);
}
}
}
This works, but I have one problem: the selection keeps on growing. I tried the methods RemoveFromSelection and ClearSelection, but they don't work. Everytime I drag a new Outlook item to the surface it keeps displaying all the previous items as well.
Can anybody help me? I'm at a complete loss
Do you handle the Drag event in your application?
If so, try to call the following code in the event handler:
e.Data.GetData(“RenPrivateMessages”);
See Outlook, custom task pane and drag-drop problem for more information.

Converting System.Web.Mail to System.Net.Mail [duplicate]

This question already has an answer here:
How I can transform my C# code : System.Web.Mail" transform by "System.Net.Mail"
(1 answer)
Closed 8 years ago.
I need some help converting System.Web.Mail to System.Net.Mail to help
send out e-mail notifications appropriately on some forms.
The old code snippet used is below:
MailMessage NotificationEmail = new MailMessage();
NotificationEmail.BodyFormat = MailMessage.IsBodyHtml;
NotificationEmail.From = "RCHIPM#ttuhsc.edu";
//NotificationEmail.To = user;
NotificationEmail.To = "RCHIPM#ttuhsc.edu";
NotificationEmail.Subject = "Notification : ";
NotificationEmail.BodyFormat = MailFormat.Html;
And some of the errors I'm getting include:
1.) 'System.Net.Mail.Message' does not contain a definition for 'BodyFormat' and no extension method 'BodyFormat' accepting a first argument of type 'System.Net.Mail.MailMessage' could be found.
2.) An object reference is required for the non-static field, method, or property 'System.Net.Mail.MailMessage.IsBodyHTML.get'
3.) Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress
4.) Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only.
...amongst other similar errors going on as well.
I'm quite certain most of these are inter-related to the main problem that I'm trying to solve which is just trying to convert the systems use of System.Web.Mail to System.Net.Mail.
I've done all the MVC and Telerik Updates I possibly could through NuGet, so those are up-to-date. I've Updated all the references as well so I know that's no longer an issue.
There may be a few things I need to change on Web.Config, but I'm not quite too sure what should be added or modified to it. I'm always weary of editing system files and assemblies, and people's tips on them as well so I try to avoid making those changes unless they are absolutely necessary.
Currently I'm using both System.Web.Mail and System.Net.Mail at the top of the file so it currently reads:
using System.Web.Mail;
using System.Net.Mail;
...as part of it's library includes.
The system uses .NET Framework 4 by the way. Should I try updating it to
.NET Framework 4.5?
Does anyone have any possible solutions for updating this block of code?
It looks like from searching through here that a bunch of people are having similar issues, but everyone's questions appear to have mixed results/answers from forum moderators.
So that's why I submitted this question just to see what kind of responses I'd get.
Hopefully, this is enough information for you guys.
If not, please let me know if you need additional info so I can provide it for you.
Thanks!
There are a couple of issues with your usage. This is how html emails are usually sent:
var mail = new MailMessage();
//set address (not just a string)
mail.From = new MailAddress("RCHIPM#ttuhsc.edu");
mail.To.Add("destination#destination.com");
//set the subject
mail.Subject = "This is the subject";
//set the body
mail.Body = "some html <b>in</b> here".
mail.IsBodyHtml = true;
//send the message
var smtp = new SmtpClient("127.0.0.1"); // Loopback address
smtp.Send(mail);

Categories

Resources