How to set content transfer encoding using MimeKit/MailKit? - c#

I try to send text in attachment. That works well only right now i have encoding problems in the builder.ToMessageBody() statement (see code below) Itry to send an email with text "my text = my subject and Version=1.0" as attachment , but when you open the email the text has been altered to "my text =3D my subject and Version=3D1.0". As you can see the = sign has been encoded to =3D Is there a way to change the encoding?
Thanks
var message = new MimeMessage();
var builder = new BodyBuilder();
builder.TextBody = "";
var xmltext ="my text = my subject and Version=1.0" ;
builder.Attachments.Add("12345.txt", Encoding.ASCII.GetBytes(xmltext), new ContentType("text", "plain"));
message.Body = builder.ToMessageBody();

The Add method actually returns the MimeEntity that it creates, allowing you to make changes to it.
To force the encoding to base64, for example, you can do this:
var attachment = (MimePart) builder.Attachments.Add("12345.txt", Encoding.ASCII.GetBytes(xmltext), new ContentType("text", "plain"));
attachment.ContentTransferEncoding = ContentENcoding.Base64;

Related

How do i edit the mapi property content disposition for inline image using c#?

I'm trying to add an inline image to the HTML body. How do I edit the content disposition ID. Currently it takes it as normal attachment instead of emebdded attachment?
string file = GetImageBytes();
Redemption.Attachment att = mail.Attachments.Add(file, 1, null, "logo");
att.Fields[0x3712001E] = "image.logo";
mail.Commit();
RDOMail msg = Globals.ThisAddIn.session.GetMessageFromID(mailItem.Item.EntryId);
mail.Item.HTMLBody = CreateHTMLBody(msg, sender, nvd_sii, recipient);
I want add the above attachment as inline attachment. Is there any way I could do it using redemptions?
Instead of setting the content disposition property, I set PR_ATTACH_FLAGS "http://schemas.microsoft.com/mapi/proptag/0x37140003" to 4(for embedded image). This solved the issue. The code below worked
string file = GetImageFile();
Redemption.Attachment att = mail.Attachments.Add(file);
att.Fields[0x3712001E] = "image.logo";
att.Fields[0x37140003] = 4;
mail.Commit();
System.IO.File.Delete(file);
RDOMail msg = Globals.ThisAddIn.session.GetMessageFromID(mailItem.Item.EntryId);
mail.Item.HTMLBody = CreateHTMLBody(msg, sender, nvd_sii, recipient);

setting the encoding of a System.Net.Mail.MailMessage body while getting text from xml using c#

I'm trying to set the encoding of a MailMessage body so it is able to display german "umlauts" (ä, ö, ü). I get the message text from an xml file. The encoding of the xml is iso-8859-1 and also is the MailMessage body.
MailMessage msg = new MailMessage();
msg.From = new MailAddress(sFrom);
msg.To.Add(sTo);
msg.Subject = sSubject;
msg.Body = sBody;
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.GetEncoding("iso-8859-1");
The first line of xml is this:
<?xml version="1.0" encoding="iso-8859-1"?>
I also tried UTF8 and Unicode encoding, but that doesn't help neither. The body is still unable to display umlauts.
Is there anything special I have to notice or am I just using the wrong encoding?
EDIT: It's a Windows Forms application and I'm using .NET framework 4.5.2
EDIT2: I'm using XmlSerializer to get the xml data.
XmlSerializer xSerializer = new XmlSerializer(typeof(App));
using (StreamReader srXml = new StreamReader(_sFileName))
{
_oAppConfig = (App)xSerializer.Deserialize(srXml);
}
Note that App is a class I created for my own.
Try specifying the Encoding while reading the file such as:
XmlSerializer xSerializer = new XmlSerializer(typeof(App));
using (StreamReader srXml = new StreamReader(_sFileName, Encoding.GetEncoding("iso-8859-1")) )
{
_oAppConfig = (App)xSerializer.Deserialize(srXml);
}

Fill PDF form and send filled form with email using iTextSharp

I have one PDF form which can be filled manually.
I want to fill it with data dynamically and send this PDF with email.
Which dll you advice to use and how to do it ?
So lets start,
First using NuGet add iTextSharp to your project.Link
Lets continue :
Every field in editable PDF have own name.After learning this field names.
private static void FillPDFForm()
{
// Original File
const string pdfTemplate="Uploads\CV.PDF";
// New file which will be created after fillin PDF
var newFile ="Uploads\FilledCV.PDF"
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
// So one of our fields in PDF is FullName I am filling it with my full name
pdfFormFields.SetField("FullName", "Vuqar Qasimov");
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = false;
pdfStamper.FormFlattening = false;
pdfStamper.Close();
break;
}
}
Bingo we already created file lets attach it to email and send it
public void SendEmailWithAttachment()
{
var objMailMessage = new MailMessage("EnterFromEmailAddress", "ReceiverEmailAddress")
{
Subject = "EnterEmailSubject",
IsBodyHtml = true,
Body = "EnterEmailBody"
};
//You can add more attachments
objMailMessage.Attachments.Add(new Attachment("EnterAttachmentLocationPath"));
var smtp = new SmtpClient("smtp.office365.com");
var login = new NetworkCredential("SenderEmail","SenderEmailPAssword");
smtp.Credentials = login;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(objMailMessage);
}
I used office 365 as email .if you using different email accounts you may have to change port or another credentials.
From my experience rather than design PDF files using iTextSharp or another packages better to design it with third part applications and fill it with iTextSharp.It will look more better and will take less time.

Getting Body content from MSMQ

I try to get the content of an item in a MSMQ - Queue.
When I look at that Entry using QueueExplorer the body content is like
[{"$type":"MyProject.MyClass.MyMethod, Messages","CustomerDecision":0,"OrderReferenceoId":"4fdb6be2-bfde-42b0-93fd-47058a326a24"}]
When I try to read the content using the following code, the body just contains weird crap, mostly \0\0 etc.:
message.Formatter = new XmlMessageFormatter();
var reader = new StreamReader(message.BodyStream);
var msgBody = reader.ReadToEnd();
(message is of type System.Messaging.Message)
It was a Coding Issue. The result LOOKED like random rubbish, but just was a unicode characterset. The following solved the problem:
message.Formatter = new BinaryMessageFormatter();
var reader = new StreamReader(message.BodyStream, Encoding.Unicode);
var msgBody = reader.ReadToEnd();

how to attach the pdf file that is present in program file throught mail

am generating a pdf file in my c# code... i want to attach that pdf file and same to my mail id using attachment... can anyone help me in sending pdf through attachment internally using c# code
thank you
You can attach your pdf file as :
MailMessage message = new MailMessage();
message.To = "mailgoesto#domain.com";
message.From = "mailcomesfrom#domain.com";
message.Subject = "mail with pdf";
message.Body = "your pdf attached";
message.Attachments.Add(new Attachment(#"c:\pdftoattach.pdf"));
SmtpMail.SmtpServer = "mail.domain.com";
SmtpMail.Send(message);

Categories