I have a tiny problem with my idea ;)
I want to read som emails from gmail (using Mailkit) and save them into multiple files.
Ok you may think "wtf is going on",... I want to extract the orders via mail and save them as single files for sort off log/protocoll. I think I can handle it better
Properties.Settings.Default.status = "Status: Online";
using (var client = new ImapClient())
{
client.Connect(Properties.Settings.Default.Server, Properties.Settings.Default.Port, true);
client.Authenticate(Properties.Settings.Default.Email, Properties.Settings.Default.Passwort);
client.Inbox.Open(FolderAccess.ReadWrite);
for (int i = 0; i < client.Inbox.Count; i++)
{
var message = client.Inbox.GetMessage(i);
var html = message.HtmlBody;
var text = message.TextBody;
var header = message.Subject + " " + message.From + " " + message.Date + " " + message.MessageId;
File.AppendAllLines(Properties.Settings.Default.pathmail + #"\" + "MailMain.txt", new[] { message.HtmlBody });
string line = File.ReadLines(Properties.Settings.Default.pathmail + #"\" + "MailMain.txt").Skip(i).Take(1).First();
dataGridView1.Rows.Add(message.Subject, message.From, message.Date, message.MessageId);
The MailMain.txt contains every email in gmail line after line, so it canĀ“t be difficult to filter them nicely.
Problem 1:
I need to get (e.g) the first line of the txt file, then create new txt with specific name (Properties.Settings.Default.pathmail). Line after line
For example: copy Line#1 from MailMain.txt to Thisisyourfirstline.txt
copy Line#2 from MailMain.txt to Thisisyoursecondline.txt.
Problem 2:
The body of the email contains a bit of HTML ( < /b>). I need to filter that all. Any suggestions ?
greeetings
I would use the StreamReader and StreamWriter to get and write things line by line. And String.Replace to strip the html tags.
Related
I'm using MailKit for received messages via Imap.
Now I need a search-logic for the software and I got a some troubles.
For ex, I using:
var query = SearchQuery.BodyContains("SomeTextfromEmail");
( or var query = SearchQuery.MessageContains("SomeTextfromEmail"); )
foreach (UniqueId uid in imapfolder.Search(query)){
//some logic
}
So, I can't get messages with text contains on it with Search filter.
I'm downloaded message ( message.WriteTo(string.Format(#"C:{0}.eml", uid)); )
and see the message content. Text of message in a base-64 format.
How do it correctly? I need decode message text from base64?
Problem are here:
var query = SearchQuery.BodyContains("Iphone").And(SearchQuery.All); //this construction find messages in folder
foreach (var uid in imapfolder.Search(query))
{
Console.WriteLine(message.From + " : " + uid.ToString());
}
var query = SearchQuery.BodyContains("Received from Iphone").And(SearchQuery.All); //this construction can't find message in folder
foreach (var uid in imapfolder.Search(query))
{
Console.WriteLine(message.From + " : " + uid.ToString());
}
Your question is a jumbled mess of confusion (your question starts out saying you are trying to figure out how to search and then you end with asking how to base64 decode the raw message that you saved to a file) so I have no idea exactly what you want to know.
If all you want is the decoded text body, you can do this:
var message = imapfolder.GetMessage (uid);
var text = message.TextBody;
This will be the decoded text string (either base64 decoded or quoted-printable decoded if it needs to be).
The MimeKit README and FAQ are both full of useful information on the basics of how to use them. I would highly recommend reading them as they may be helpful.
Update:
Based on the comment added to my answer, it sounds like what you want to do is this?
var matched = new UniqueIdSet ();
foreach (var uid in folder.Search (SearchQuery.BodyContains ("iPhone"))) {
var message = folder.GetMessage (uid);
var body = message.TextBody;
if (body != null && body.Contains ("Received from iPhone"))
matched.Add (uid);
}
I want to create a textbox for my string input, and then separate it into an array if the input has ".". For example:
The answer lies in machine translation. The best machine translation technology cannot always provide translations tailored to a site or users like a human. Simply copy and paste a code snippet anywhere.
In that case, that input will consist of 3 arrays.
Please take a look at the following code from Microsoft. I want to change the hard code from the input using the textbox. Then pass each array to be translated.
class TranslateArraySample
{
public static async Task Run(string authToken)
{
var from = "en";
var to = "es";
** var translateArraySourceTexts = new []
{
"The answer lies in machine translation.",
"the best machine translation technology cannot always provide translations tailored to a site or users like a human ",
"Simply copy and paste a code snippet anywhere "
};
var uri = "https://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
var body = "<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</From>" +
"<Options>" +
" <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{1}</ContentType>" +
"<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"</Options>" +
"<Texts>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{2}</string>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{3}</string>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{4}</string>" +
"</Texts>" +
"<To>{5}</To>" +
"</TranslateArrayRequest>";
string requestBody = string.Format(body, from, "text/plain", translateArraySourceTexts[0], translateArraySourceTexts[1], translateArraySourceTexts[2], to);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(uri);
request.Content = new StringContent(requestBody, Encoding.UTF8, "text/xml");
request.Headers.Add("Authorization", authToken);
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
switch (response.StatusCode)
{
case HttpStatusCode.OK:
Console.WriteLine("Request status is OK. Result of translate array method is:");
var doc = XDocument.Parse(responseBody);
var ns = XNamespace.Get("http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2");
var sourceTextCounter = 0;
foreach (XElement xe in doc.Descendants(ns + "TranslateArrayResponse"))
{
foreach (var node in xe.Elements(ns + "TranslatedText"))
{
** Console.WriteLine("\n\nSource text: {0}\nTranslated Text: {1}", translateArraySourceTexts[sourceTextCounter], node.Value);
}
sourceTextCounter++;
}
break;
default:
Console.WriteLine("Request status code is: {0}.", response.StatusCode);
Console.WriteLine("Request error message: {0}.", responseBody);
break;
}
}
}
}
use (StringObject).Split("<separator>")
sample code:
var translateArraySourceTexts = new[]
{
"The answer lies in machine translation.",
"the best machine translation technology cannot always provide translations tailored to a site or users like a human ",
"Simply copy and paste a code snippet anywhere "
};
var array = string.Join(",",translateArraySourceTexts).Split('.');
Here is some example code. Replace string s, with your .Text from your textbox.
string s = #"Changing your development practice to introduce an automated testing strategy can revolutionise your deployments. If you approach the software release date with a sense of dread, this technique is for you.
Implementing a test-driven development strategy using tSQLt leads to robust, modular code that becomes a pleasure to work with. As more tests are created, trust builds that releases will provide beneficial new functionality with no negative side-effects.";
var translateArraySourceTexts = s.Split( Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries ).ToArray();
You need String.Split(charArray, stringSplitoptions) to get only three strings in your resulting array.
in your example
string translatableString = "The answer lies in machine translation. The best
machine translation technology cannot always provide translations tailored to
a site or users like a human. Simply copy and paste a code snippet anywhere.";
string[] arr = translatableString.Split(new char[] { '.' },
StringSplitOptions.RemoveEmptyEntries);
you would get a array of 4 strings with
translatableString.Split('.') because one will be empty. This is why I provided the overloaded method.
I'm creating an Outlook Add In, which has a subform. The form has a button on it, through which I would like to generate a mailitem, if the user clicks it. I'd like to auto-populate some info in the email, and then leave it for the user to send at their leisure.
My code looks like the following:
private void btnMailDocNotice_Click(object sender, EventArgs e)
{
string clientInfo = string.Empty;
string matInfo = string.Empty;
string author = string.Empty;
string dType = string.Empty;
string fLocation = string.Empty;
string keyWords = string.Empty;
string docName = string.Empty;
clientInfo = this.mCboClient.Text + " " + lblClient;
matInfo = this.mCboMatter.Text + " " + lblMatter;
author = this.txtAuthor.Text;
dType = this.mCboDocType.Text.ToUpper();
fLocation = this.txtSavePath.Text;
keyWords = this.txtKeyWords.Text;
docName = this.txtDocName.Text;
this.sendDocNotice = true;
this.Hide();
CreateMailItem(clientInfo, matInfo, author, dType, this.operatorCode.ToUpper(), fLocation, keyWords, docName);
this.Show();
}
private void CreateMailItem(string clientInfo, string matInfo, string author, string dType, string profiledBy, string fLocation, string keyWords, string docName)
{
this.DNoticeItem = (Outlook.MailItem)ThisAddIn.myApp.CreateItem(Outlook.OlItemType.olMailItem);
this.DNoticeItem.Subject = "Document: " + docName;
this.DNoticeItem.HTMLBody = "<span style=\"font-family:Calibri; font-size: 11pt;\">KeyWords: " + keyWords + "</span>";
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
this.DNoticeItem.HTMLBody += "<br />Matter: " + matInfo;
this.DNoticeItem.HTMLBody += "<br />Author: " + author;
this.DNoticeItem.HTMLBody += "<br />Doc Type: " + dtClient;
this.DNoticeItem.HTMLBody += "<br />Profiled by: " + profiledBy;
this.DNoticeItem.HTMLBody += "<br />File://" + fLocation;
this.DNoticeItem.Importance = Outlook.OlImportance.olImportanceNormal;
this.DNoticeItem.Display(false);
}
The problem that I'm running into, is it fires an exception on the mailitem.display function, whether I use true or false (doing a bit of research says that determines if the user can access the main Outlook window or not while the mailitem is open). The exception is a COM Exception of "A dialog box is open. Close it and try again". I've tried hiding the WinForm prior to the function call that creates the mail item, then show it again after the function is exited, but it didn't work. I've tried a version of the code where I use System.Diagnostics.Process.Start() to try and open the file after saving it to disk, and while it doesn't fire an exception from the add in, Outlook prompts the user with a message box of the same message from the ComException. I even tried creating a field to see if the doc notice email should be drafted, and thought to have the code take care of that after a form.close() call, thinking the close call would at least dispose of the dialog box that was locking Outlook, and I still got the same exception.
Is there a way to achieve what I want? Does anyone have any suggestions? I'm kind of stuck at the moment, and would appreciate any help/pointers/suggestions anyone has to offer in this issue. My sincere apologies if this is a duplicative question - I couldn't find a good answer to the question. Thank you in advance for your time.
Firstly, why not display yoru own form modelessly?
Secondly (and this is pretty important) do not use code like the following
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
Every time you run a line like that, you retrieve HTMLBody, add some stuff to it (making the HTML malformed), then set HTMLBody again and force Outlook to make sense of your (malformed) HTML. This (assuming Outlook can parse and fix your HTML) will result in HTML being returned to be different from what you set it to.
Build the HTML body once using a regular string, and set HTMLBody property only once.
I've developed an Outlook plugin using C# where for each new mail received, i get(and save) the sender/subject/the body of email & the attachments. Well, the last 2 gave me a headache. I can see the sender and the subject of the new mail but for the body&attachments it seems that is a problem. I've used NewMailEx for getting the new mails in Inbox. The function looks like this:
private void Application_NewMailEx(string EntryIDCollection)
{
string[] entryIdArray = EntryIDCollection.Split(',');
foreach (string entryId in entryIdArray)
{
try
{
Outlook.MailItem item = (Outlook.MailItem)Application.Session.GetItemFromID(EntryIDCollection, null);
string subj = item.Subject; //works
string to = item.To; //works
string bec = item.BCC; //does not work but dont care
string body = item.Body; //DOES NOT SAVE THE BODY OF THE NEW MAIL RECEIVED
string final = "Sender: " + item.SenderEmailAddress + "\r\n" + "Subject: " + subj + "\r\n" + "BCC: " + bec + "\r\n" + "TO: " + to + "\r\n\n" + "Body: " + body + "\r\n\n";
System.IO.File.AppendAllText(#"D:\tmp\atr.txt", final);
//the result of item.attachments.count is always 0 , even though I've
//sent mails with a different number of attachments. So the if
//statement is false
if (item.Attachments.Count > 0)
{
for (int i = 1; i <= item.Attachments.Count; i++)
{
item.Attachments[i].SaveAsFile(#"D:\tmp\" + item.Attachments[i].FileName);
}
}
Marshal.ReleaseComObject(item);
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
}
}
Where does the Item variable come from? You need to initialize it using Application.Session.getItemfromID().
I'm writing in VBA, so I feel like posting my code here would be a faux pas, but I have come to a solution using similar Object Libraries from the Outlook application that I think transfer well to your C++ intentions.
First of all, switching to POP3 certainly fixes this problem, but you're stuck using POP3, which is only ideal from a programming standpoint.
The solution I have found follows this algorithm:
//Generate Outlook MailItem Object, "item"
//If item.DownloadState is not 1,
//item.Display
//item.Close(1)
//Perform end of code operations
//Call Function that is identical to Application_NewMailEx but is not Application_NewMailEx because Application_NewMailEx is a function that is thrown during the incoming mail event.
//Else,
//Perform Intended Code
You see how calling a function identical to Application_NewMailEx creates a kind of loop because if item.DownloadState is not 1, you'll be calling that function again? I get that this isn't the most ideal coding practice, but I have scoured the internet and Outlook Application and Outlook Object Library experts have no idea how to solve this problem any other way (in fact, no one even proposes THIS solution)
For my full VBA Solution Check out:
https://superuser.com/questions/894972/outlook-strange-item-attachments-error/990968#990968
I'm putting together a script to send out a preview html mail to hotmail etc, and I utlising an existing script - I'm new to c# so this is as much a learning expereince forme too.
The problem is I have made a few ammends, and it's fine up to the point I drop HTML source into the comments box - it then fails the sending.
I have looked into it and tried changing the new MailMessage(); to new MailDefinition(); and also adding in faqMsg.IsBodyHtml = true; but it's still failing. it's fine just on regular text. Any thoughts what I need to look into ?
SmtpClient smtpCli = new SmtpClient("localhost");
MailMessage faqMsg = new MailMessage();
//MailDefinition faqMsg = new MailDefinition();
//faqMsg.BodyFileName = "email.htm";
//faqMsg.IsBodyHtml = true;
faqMsg.From = new MailAddress("");
faqMsg.To.Add("");
faqMsg.Subject = "Mail test :" + subject.Text;
//Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("Enquiry Type:" + enquirytype.SelectedValue + "\r\nFrom:" + "\r\nEmail:" + email.Text + "\r\n\r\nComments\r\n" + comments.Text + "\r\n[EOF]", null, "text/plain");
//HTML part
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<strong>Enquiry Type:</strong>" + enquirytype.SelectedValue + "<br><br><strong>Email:</strong>" + email.Text + "<br><br><hr><br><strong>Comments</strong><br>" + HttpUtility.HtmlEncode(comments.Text) + "<br>EOF", null, "text/html");
faqMsg.AlternateViews.Add(plainView);
faqMsg.AlternateViews.Add(htmlView);
//Add Header Markers
faqMsg.Headers.Add("X-Company", "");
faqMsg.Headers.Add("X-Location", "");
faqMsg.Headers.Add("X-Brand", "");
smtpCli.Send(faqMsg);
lbReport.Text = "Your Message was Sent";
//Response.Redirect("email-thanks.aspx");
Hey, what error are you getting?
Looking at the code, you may need to add an # symbol before any strings with the \ inside. But that's a guess without the error.
Without the exact exception text you are seeing, Im going to take a stab at this.
This doesn't sound like an email error.
this sounds like the generic error that won't allow you to enter HTML content into a textbox.
Be sure VaildateRequest=false in the #Page attribute at the top of the page.