c# save sent email in Lotus Sent folder (domino interop) - c#

I'm trying to solve a little stuck in my code. I'm sending email via Domino server, all mails were successfully sent, but mail is not visible in Sent email in Lotus Notes at all. Can you help me? Thanks
NotesSession notesSession = new NotesSession();
notesSession.Initialize(passw);
NotesDatabase nd = notesSession.GetDatabase("","names.nsf", bCreateonfail: false);
if (!nd.IsOpen)
{
nd.Open();
}
NotesDocument notesDocument = nd.CreateDocument();
notesDocument.SaveMessageOnSend = true;
notesDocument.ReplaceItemValue("Form", "Main Topic");
// set notes memo fields (To: CC: Bcc: Subject etc)
notesDocument.ReplaceItemValue("SendTo", emailSup);
notesDocument.ReplaceItemValue("CopyTo", copyTo);
// Subject is the name of pdf file without .pdf
notesDocument.ReplaceItemValue("Subject", pdfFiles[i].Remove(pdfFiles[i].Length - 4, 4));
// Create the body of the email. This allows you to use the appendtext
NotesRichTextItem richTextItem = notesDocument.CreateRichTextItem("Body");
//Path of attachment (pdf file)
string AttachPath = #"c:\...\PDF\" + pdfFiles[i];
string txtPath = #"c:\...\emailtxt.txt";
System.IO.StreamReader txt = new System.IO.StreamReader(txtPath, Encoding.GetEncoding("windows-1250"));
// Add email text from txt file.
richTextItem.AppendText(txt.ReadToEnd());
// Attach file to e-mail
NotesEmbeddedObject obj_notesEmbeddedObject = richTextItem.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT, "", AttachPath, "Attachment");
notesDocument.SaveMessageOnSend = true;
notesDocument.Save(true, false);
notesDocument.Send(false);

You are creating a database object:
NotesDatabase nd = notesSession.GetDatabase("","names.nsf", bCreateonfail: false);
Then you are creating a document object in that database object:
NotesDocument notesDocument = nd.CreateDocument();
And then you are saving the document object in that database object:
notesDocument.Save(true, false);
Do you see the problem?
You are saving the document in names.nsf on the local machine where your code is running. If you look in a view in names.nsf that selects #All, you will find it there.

Related

How to paste image from clipboard to Outlook email Body?

Here is my situation.
I've made a Application Windows Form which is editing an Excel sheet depending on what the user is doing (buttons / toggle / text box / etc..).
Once the edtion is complete the new Excel file is generated.
My programm then selects a range of cells, and copies it.
It goes to Clipboard.
I've done multiple tests (if(){}else{} / saving into a .jpg / etc..) and everything is checked true.
I don't want to ATTACH my image.
I don't want to save it, even temporarily, then paste it in the body through the saved .jpg file.
I "just" want to make a Ctrl+V, into my Outlook eMail Body.
here's how i get the image from my clipboard ("MessageBody" is declared at the top as a public string so that i can call it through different regions):
public void ReadData()
{
Excel excel = new Excel(#"E:\c#\Project#XX\Resources\TEST.xlsx", 1);
excel.CopyRange(0, 0, 32, 12);
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap MessageBody = (iData.GetData(DataFormats.Bitmap, true) as Bitmap);
//pbx.Image = Image;
//image.Save(#"E:\c#\Project#XX\Resources\bitmap1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//MessageBody = image;
}
excel.Close();
excel.Quit();
}
Here's my message building code :
private void flatCustButton013_Click(object sender, EventArgs e)
{
ReadData();
string MessageSubject = $"SomeSubject";
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
newMail.Subject = MessageSubject;
newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
MessageHTMLBody = "<html><body>SomeText.<img src="cid:MessageBody"</img></body></html>";
newMail.HTMLBody = MessageHTMLBody;
//newMail.Body = System.Windows.Input.ApplicationCommands.Paste;
//newMail.Display(false);
//newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; ;
//newMail.HTMLBody = System.Windows.Input.ApplicationCommands.Paste;
//newMail.Body = MessageBody;
newMail.To = ToAddress;
newMail.SentOnBehalfOfName = FromAddress;
newMail.CC = CcAddress;
newMail.BCC = BccAddress;
//System.Windows.Input.ApplicationCommands.Paste;
//wrdEdit = application._Inspector.WordEditor
newMail.Send();
}
In the previous version, people had to open an Excel file, change the Cells manually, and then click a button (with a macro in VB).
It would open a Outlook eMail item (display:true) with the image pasted already, then just needed to click "Send" and it was ok.
My 2.0 version is meant to automatise this by just click a "Send //Generated Excel Sheet//"Button.
Here's the macro code in VB :
Sub SendMail()
Dim Messager As New Outlook.Application
Dim Mail As Outlook.MailItem
Dim WrdEdit
Range("my_range").CopyPicture
Set Messager = New Outlook.Application
Set Mail = Messager.CreateItem(olMailItem)
With Mail
.To = "some folks"
.CC = "some folks"
'.BCC = ""
.Subject = "SomeSubject"
.Display
.HTMLBody = Mess + .HTMLBody
End With
Set WrdEdit = Messager.ActiveInspector.WordEditor
WrdEdit.Application.Selection.Paste
Set WrdEdit = Nothing
Set Messager = Nothing
Set Mail = Nothing
ActiveWorkbook.Close SaveChanges:=False
End Sub
But i don't get why i would pass through the Inspector (which i have no knowledge about) if i'm succeeding in retrieving the clipboard image.
I found a way by adding an attachment from a local saved file to the mail and displaying it into the body with HTML.
Like so :
newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Outlook.Attachment attachment = newMail.Attachments.Add(#"path.imageformat", Outlook.OlAttachmentType.olEmbeddeditem, null, $"someTitle");
string imagecid = "whatever";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3613041E", imagecid);
newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid);
the "http://schemas.microsoft.com/mapi/proptag/0x3712001E" code is here : http://www.outlookcode.com/codedetail.aspx?id=1915.
And it works.
However, i really don't like having urls in my code, is there another way ?
And also, it looks like whatever i put into my 'imagecid' string nothing changes.
I'm trying hard to understand what i did with the code, and not just letting it work as it is.
Looks like i need some authorisation from Office to work through an application ? And especially when i want to paste image in a body (which can contain other stuff than just pixel data).
If i did not need this autorization with the VB script, i suppose, it is because i had to "physicly/humanly" press "Send Mail" button in Outlook ?
Are there any other possibilities ? I'm still using a file though to paste in my body, can't i just make a Ctrl+C Ctrl+V programmatically ? :(

Fetching attachment from given .eml file and using that attachment to other mail

I am working on code in C#,but not going through on thing that is I have saved some .eml file on my disk now I am parsing each eml file and creating a new mail adding the eml file data to the new mail but I am unable to attach the attachments present in the .eml file to the new mail , can anybody please help?
I am using the follwing code but it shows the error ex = {"The process cannot access the file because it is being used by another process.\r\n":null}
foreach (CDO.IBodyPart attach in msg.Attachments)
{
i++;
string filenm = "C:\\mail_automation\\attachments\\xyz" + i +".eml";
if (File.Exists(filenm))
{
string fn = attach.FileName;
attach.SaveToFile("C:\\mail_automation\\attachments\\xyz" + i + ".eml");
Attachment data = new Attachment(filenm);
mailMessage.Attachments.Add(data);
}
else
{
File.Create(filenm);
string fn = attach.FileName;
attach.SaveToFile("C:\\mail_automation\\attachments\\xyz" + i + ".eml");
Attachment data = new Attachment(filenm);
mailMessage.Attachments.Add(data);
}
You have to extract the attachments and save them to disk first, then fetch it again into the new mail.
Code example here
MailMessage message = new MailMessage();
MemoryStream ms = new MemoryStream(); //store the mail into this ms 'memory stream'
ms.Position = 0;
message.Attachments.Add(new Attachment(ms, attachmentName));

Attachment is not going with email

And i am sending mails from below details :
// language -- C#
// import namespace
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Mails are going properly with above details and now i want to add attachment on email.And for that i have added below code :
String sFile = "http://www.demo.com/abc.pdf";
var oAttch = new System.Web.Mail.MailAttachment(sFile);
oMail.Attachments.Add(oAttch);
But it's not adding the attachment in mail.
It's giving error that "URI formats are not supported".
Mail attachment supports only files from the local drive.
If you want to attach a file that is hosted on the web you should download it to your local drive first.
If you have the file in your local drive you can do something like this:
String sFile = "abc.pdf";
var oAttch = new System.Web.Mail.MailAttachment(Server.MapPath(sFile));
oMail.Attachments.Add(oAttch);
You cant use a file from the web. It must be located on your local drive.
Check out MSDN - MailAttachment Constructor (String)

Save attach of attached mail

I'm using Mail.dll from limilabs to manage an IMAP folder.
There is one mail with an attachment that is an eml file, so a mail.
It has in turn one attached eml file that I need to extract.
So the email structure is as follows:
Email
|- Attachment: file.eml
|- Attachment file2.eml
This is my code:
IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
// save all attachments to disk
foreach(MimeData mime in email.Attachments)
{
if (uid == 1376)
{
System.IO.Directory.CreateDirectory(string.Format(#"c:\EMAIL\{0}", uid));
mime.Save(#"c:\EMAIL\" + uid + "\\" + mime.SafeFileName);
MimeData help;
if (mime.ContentType.ToString() == "message/rfc822")
{
//i need to cast this attach in a imail
}
}
}
How can I extract the inner-most eml file (file2.eml in the structure mentioned above)?
From this link, it looks like you should be able to do the following:
if (attachment.ContentType == ContentType.MessageRfc822)
{
string eml = ((MimeText)attachment).Text;
IMail attachedMessage = new MailBuilder().CreateFromEml(eml);
// process further
}
If you only need to extract all attachments from all inner messages, you can use IMail.ExtractAttachmentsFromInnerMessages method:
IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));
ReadOnlyCollection<MimeData> attachments = mail.ExtractAttachmentsFromInnerMessages();
foreach (MimeData mime in attachments)
{
mime.Save(#"c:\" + mime.SafeFileName);
}

deleting a file in c# after sending email attachment

I have the following code that basically attaches a file to an email message then after all attachments are attached and email is sent, i try to delete all files, however I get a file in use exception. I believe the error comes in this line
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
I tried using this code but I get an cannot sent email message
using Attachment data = new Attachment(file, MediaTypeNames.Application.Octet)){
//and the rest of the code in here.
}
foreach (KeyValuePair<string, string> kvp in reports) {
browser.GoTo(kvp.Value);
Thread.Sleep(1000);
System.IO.File.Move(#"C:\Reports\bidata.csv", #"C:\Reports\"+kvp.Key.ToString()+".csv");
string file = #"C:\Reports\" + kvp.Key.ToString() + ".csv";
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
mail.Attachments.Add(data);
}
smtpserver.Send(mail);
string[] files = Directory.GetFiles(#"C:\Reports");
foreach (string files1 in files)
{
File.Delete(files1);
}
In order to delete the files first you will have to dispose the attachment and mail objects and then delete the files
Dispose the smtpclient by putting it in a usings or calling dispose directly. That should free the file resource and allow you to nuke it.

Categories

Resources