OpenPoup.NET - find email, subject and date from forwarded message - C# - c#

I am using OpenPoup.NET to read email from Gmail. I want to identify original sender, receiver, date and subject of forwarded message. My email looks like,
----------------------Email Body starts-----------------------------------
FYI, read below email to get idea of button.
Chirag Developer XYZ Limited
From: Rat Chanra [mailto:rat#chanra.com] Sent: Friday, June 5, 2015
9:48 PM To: Chirag Subject: FW: Copy Product Button
Hi Rat,
I have included a .png of the copy product button. Attached also is an
.eps of updated buttons if we feel like updating all the buttons!
Let's chat,
Ali Designer
----------------------Email Body Ends-----------------------------------
I want to find From, Date, Sublect and To of original message, which is in this case,
From = rat#chanra.com
Date = Friday, June 5, 2015 9:48 PM
To = Chirag
Subject = FW: Copy Product Button
Can I do this using OpenPopUp.net?
Do I have to parse email body using RegEx and fetch above detail. If yes, what is best way for that? I am using C#

You can use the following to match:
From:\s+[^[]+\s+\[mailto:([^\]]+)\]\s+Sent:\s+(\w+,\s+\w+\s+\d+,\s+\d{4}\s+[\d:]+\s+[aApP]M)\s+To:\s+(\w+)\s+Subject:\s+(.*)
And extract From as $1, Date as $2, To as $3 and Subject as $4.
See DEMO

Related

Incorrect UTC time in Calendar File for MS Outlook

In my application of event management, i create a calendar file which is sent with the registration confirmation email. The application is in .NET with C#. I use following code to get the time.
sdate = row1.StartTime.ToUniversalTime().ToString("yyyyMMddThhmmssZ");
edate = row1.EndTime.ToUniversalTime().ToString("yyyyMMddThhmmssZ");
...
and then i add it to calendar file
contents.Add("DTSTART:" + sdate);
contents.Add("DTEND:" + edate);
But the problem is that if my end time is 3:59 PM local time then it coverts correctly. But if it ends at 4:00 PM then end time becomes incorrect.
Can anybody guide me?
Please find below the Screens of Outlook Calendar Entry and Calendar File data.
Correct Output
Incorrect Output
I finally found the solution. It is such a simple one that its disappointing that I missed it.
Just to help others. I just need to put capital 'H' in the string format when converting to string. So the correct code is
sdate = row1.StartTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ");
edate = row1.EndTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ");

How to get sorted Values from HTML Table in C#?

Hello fellow Programmers.
I now spend a whole Day reading Threads to solve this Problem.
I am Parsing HTML from an automatic generated schedule, the same schedule programm was discussed 6 years ago on this thread: Parsing complex HTML tables
But this java / javascript solutions wont work for me. Also the mentioned Programs arent working anymore, I think they released a new Version of the Software. This is the Example I am trying to Parse: https://www.ostfalia.de/cms/de/b/studium/stundenplaene/download/ss19_b_stdgrp_ai_6.html
I need the Parsed Data in the right sequence because I want to generate an iCalendar file with it, or pass/send the data into an self written schedule App
I am using the HTML Agility Pack and Im already sucessful with parsing what I need but I cant get it in the right order its complete split because the HAP displays by row like any other parser. Im so desperate I was close to just count the emtpy trs to estimate when a new row begins but this doesnt work because the program has sometimes more sometimes less empty lines. Does somebody of you has an idea?
This is my Code to get the infos I need:
WebClient client = new WebClient();
string html = client.DownloadString("https://www.ostfalia.de/cms/de/b/studium/stundenplaene/download/ss19_b_stdgrp_ai_6.html");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var erg = doc.DocumentNode.SelectNodes("//td[#class='v']");
for (int i = 0; i < erg.Count; i++)
{
txt_check.Text = (erg[i].InnerText);
list_check.Items.Add(erg[i].InnerText);
}
"class v" is the event and "class t" is the time in this example its just class v
I get:
The output of class='v' looks like:
10:30 - 12:00 UhrAI 6.1 Komponentenbasierte SoftwareentwicklungB. RogallaB 109
13:00 - 14:30 UhrAI WPF-12 CCNA 2 Cisco Routing & SwitchingChr. HollmannA 201
13:00 - 14:30 UhrAI WPF Inst. u. Betrieb einer Datenb. a. B. OracleD. HeringA
14:45 - 16:15 UhrAI WPF Mathematik III für InformatikerT. WaldeerB 27 114
14:45 - 16:15 UhrAI WPF-18 AutomatisierungstechnikF. DziembowskiA 107
The output of class='t' looks like:
"Di, 05.03.2019"
"Mi, 06.03.2019"
"Do, 07.03.2019"
"Fr, 08.03.2019"
"Sa, 09.03.2019"
I hope someone has an idea how I can Sort and Match the informations in an Dictionary or List to get it in an ICS.
The Output should be like:
"MI, 06.03.2019 , 8:45 - 10:15 ,AI 6.1 Komponentenbasierte Softwareentwicklung B. Rogalla B109 , 10:30 - 12:00 AI 6.1 Komponentenbasierte Softwareentwicklung B. Rogalla B109"
...
So I can bring it in the ICS Format or DATE/TIME for an Calendar App or something.
Pastbin for the whole HTML:
https://pastebin.com/hHbJTujN
Some Pictures of the Output:
https://drive.google.com/open?id=16Y_hISdVEvzlrS6LCmBMcwAarGhz__t0

Encoding a line break for Twilio SMS using C#?

I'm using WebApi 2.2 to ramp up on the Twilio API. I have the Twilio C# libraries installed.
I'm using a form to capture a string in my web app, and I send that down to webAPI. I'm wondering how I can leverage the C# libraries to send a message with line breaks.
The example code shows the following:
var msg = twilio.SendMessage("+15084043345", "+15084043345", "Can you believe it's this easy to send an SMS?!");
However, I'm not sure how to include a line break in this message. Should I be inserting anything into this string clientside to represent a linebreak? Any guidance would be awesome.
Twilio Evangelist here.
This is actually super simple:
var msg = twilio.SendMessage("+15084043345", "+15084043345", "Hello.\nCan you believe it's this easy to send an SMS?!");
Will result in:
Hello.
Can you believe it's this easy to send an SMS?!
Just like printing strings to the console, \n, \r\n, and \r can be used to insert a new line. I can't say definitively what is best to use across all handsets, but I have found \n to be pretty reliable. (I can say all three of these work on my iOS 8 device perfectly...)
If you want to show new line in sms like
Account Name : suraj
Age : 24
then here is a code for asp.net VB
Dim value As String = "Account Name :" & "suraj" & vbCrLf & "Age :" & " 24"
it will show new line in SMS not in web page

Reading and parsing email from Gmail using C#, C++ or Python

I have to do a Windows application that from times to times access a Gmail account and checks if there is a new email. In case there is, it must read the email body and subject (a simple text email, without images or attachments).
Please, do not use paid libs, and in case of any other libs used, give the download path.
And I need the email body and subject only. So if the long and complex message that comes from Gmail could be parsed and only two strings containing the subject and the body, it would be perfect.
Finally, I only have to get the new messages arrived since the last execution. So the read messages could be marked as "read" and only the new ones (marked as "new") are considered.
The code can be written in Python or C++, but I prefer it in C#.
Related question:
Properly formatted example for Python iMAP email access?
This prints the subject and body of unseen messages, and marks those messages as seen.
import imaplib
import email
def extract_body(payload):
if isinstance(payload,str):
return payload
else:
return '\n'.join([extract_body(part.get_payload()) for part in payload])
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("user", "password")
conn.select()
typ, data = conn.search(None, 'UNSEEN')
try:
for num in data[0].split():
typ, msg_data = conn.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
subject=msg['subject']
print(subject)
payload=msg.get_payload()
body=extract_body(payload)
print(body)
typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
finally:
try:
conn.close()
except:
pass
conn.logout()
Much of the code above comes from Doug Hellmann's tutorial on imaplib.
Use one of the many C# IMAP libraries.
Note that there are some differences between Gmail-IMAP and IMAPA. For example, due to the fact that Gmail treats folders like labels, the code like the one below doesn't delete message if it's tagged with some other folder:
imap_instance.uid('store', uid, '+FLAGS', '\\Deleted')
imap_instance.expunge()
I know this is an old post but I wanted to add the following link to the Open Source ImapX 2 Library discussion: https://imapx.codeplex.com/ the developers seem to be keeping the project up to date. Great job to those all involved
Google has opened it's Gmail API for accessing your gmail account. You can check a quickstart sample with the basic functionalities at this link:
https://developers.google.com/gmail/api/quickstart/python
from imap_tools import MailBox, Q
# This prints the subject and body of unseen messages, and marks those messages as seen.
with MailBox('imap.mail.com').login('test#mail.com', 'password') as mailbox:
# *mark_seen param = True by default
print([(m.subject, m.html or m.text) for m in mailbox.fetch(Q(seen=False), mark_seen=True)])
imap_tools

C#, Lotus Interop: Getting Message Information

I'm using Interop.Domino.dll to retrieve E-mails from a Lotus "Database" (Term used loosely). I'm having some difficulty in retrieving certain fields and wonder how to do this properly. I've been using NotesDocument.GetFirstItem to retrieve Subject, From and Body.
My issues in this regard are thus:
How do I retrieve Reply-To address? Is there a list of "Items" to get somewhere? I can't find it.
How do I retrieve friendly names for From and Reply-To addresses?
When I retrieve Body this way, it's formatted wierdly with square bracket sets ([]) interspersed randomly across the message body, and parts of the text aren't where I expect them.
Related code:
string
ActualSubject = nDoc.GetFirstItem("Subject").Text,
ActualFrom = nDoc.GetFirstItem("From").Text,
ActualBody = nDoc.GetFirstItem("Body").Text;
Hah, got it!
Object[] ni = (Object[])nDoc.Items;
string names_values = "";
for (int x = 0; x < ni.Length; x++)
{
NotesItem item = (NotesItem)ni[x];
if (!string.IsNullOrEmpty(item.Name)) names_values += x.ToString() + ": " + item.Name + "\t\t" + item.Text + "\r\n";
}
This returned a list of indices, names, and values:
0: Received from example.com ([192.168.0.1]) by host.example.com (Lotus Domino Release 6.5.4 HF182) with ESMTP id 2008111917343129-205078 ; Wed, 19 Nov 2008 17:34:31 -0500
1: Received from example.com ([192.168.0.2]) by host2.example.com (Lotus Domino Release 6.5.4 HF182) with ESMTP id 2008111917343129-205078 ; Wed, 19 Nov 2008 17:34:31 -0500
2: X_PGRTRKID 130057945714t
3: X_PGRSRC IE
4: ReplyTo "example" <name#email.example.com>
5: Principal "example" <customerservice#email.example.com>
6: From "IE130057945714t"<service#test.email.example.com>
7: SendTo me#example.com
8: Subject (Message subject redacted)
9: PostedDate 11/19/2008 03:34:15 PM
10: MIME_Version 1.0
11: $Mailer SMTP DirectMail
12: $MIMETrack Itemize by SMTP Server on xxxPT02-CORP/example(Release 6.5.4 HF182|May 31, 2005) at 11/19/2008 05:34:31 PM;Serialize by Router on xxxPT02-CORP/example(Release 6.5.4 HF182|May 31, 2005) at 11/19/2008 05:34:32 PM;Serialize complete at 11/19/2008 05:34:32 PM;MIME-CD by Router on xxxPT02-CORP/example(Release 6.5.4 HF182|May 31, 2005) at 11/19/2008 05:34:32 PM;MIME-CD complete at 11/19/2008 05:34:32 PM;Itemize by Router on camp-db-05/example(Release 7.0.2 HF76|November 03, 2006) at 11/19/2008 05:34:32 PM;MIME-CD by Notes Client on MyName/Guest/example(Release 6.5.6|March 06, 2007) at 11/20/2008 12:46:25 PM;MIME-CD complete at 11/20/2008 12:46:25 PM
13: Form Memo
14: $UpdatedBy ;CN=xxxPT02-CORP/O=example
15: $ExportHeadersConverted 1
16: $MessageID <redacted#LocalDomain>
17: RouteServers CN=xxxPT02-CORP/O=example;CN=camp-db-05/O=example
18: RouteTimes 11/19/2008 03:34:31 PM-11/19/2008 03:34:32 PM;11/19/2008 03:34:32 PM-11/19/2008 03:34:32 PM
19: $Orig 958F2E4E4B666AB585257506007C02A7
20: Categories
21: $Revisions
22: DeliveredDate 11/19/2008 03:34:32 PM
23: Body []exampleexample
Now, who can tell me why the Body keeps getting messed up?
The Body item is a NotesRichTextItem, not a regular NotesItem. They are a different type of object in the Lotus Notes world (and often the source of much developer frustration!)
I don't have much experience with using COM to connect to Domino, and I know there are differences in what you have access to, but the Domino Designer Help should give you lots of information the classes, such as NotesRichTextItem.
Perhaps the method "GetFormattedText" would work better for you than accessing the item's Text property.
Here's an example of the method (taken from Domino Designer Help)
Dim doc As NotesDocument
Dim rtitem As Variant
Dim plainText As String
Dim fileNum As Integer
'...set value of doc...
Set rtitem = doc.GetFirstItem( "Body" )
If ( rtitem.Type = RICHTEXT ) Then
plainText = rtitem.GetFormattedText( False, 0 )
End If
' get a file number for the file
fileNum = Freefile
' open the file for writing
Open "c:\plane.txt" For Output As fileNum
' write the formatted text to the file
Print #fileNum, plainText
' close the file
Close #fileNum
It may not work depending on how your environment is set up, but the easiest way to deal with mail in domino is to leave them as MIME and get at the values via the NotesMIMEEntity and NotesMIMEHeader. This will only work if the mail came in from the web rather than native Notes and the environment has been set up to store mail in MIME format.
Otherwise you need to access the body as a NotesRichTextItem. From that item you need to get a NotesRichTextNavigator that will allow you move around the rich text structure if you need to.
If you think the struture should be relatively simple try calling NotesRichTextItem.GetFormattedText(). If that still isn't working then you're going to need to work out what is happeing by playing with an example doument and seeing what the structure looks like to the NotesRichTextNavigator.

Categories