This project is read-only.

How to set SmtpMessage ContentType to text/plain

Aug 11, 2014 at 3:38 PM
Trying to use this code to set contenttype to text/plain but getting "object reference not set to an instance of an object"
mail.ContentType = new HigLabo.Net.Smtp.ContentType("text/plain");
anyone know how to set the ContentType to text/plain ??
Aug 12, 2014 at 7:12 AM
Hi.

I could not reproduct your bug.
My code is here and no exception.
 SmtpMessage mg = new SmtpMessage();
 mg.ContentType = new HigLabo.Net.Smtp.ContentType("text/plain");
Aug 12, 2014 at 2:16 PM
Here is the whole stack trace
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=HigLabo.Mail
  StackTrace:
       at HigLabo.Net.Smtp.MimeWriter.WriteBodyData(Stream stream, SmtpMessage message)
       at HigLabo.Net.Smtp.MimeWriter.WriteBody(Stream stream, SmtpMessage message)
       at HigLabo.Net.Smtp.MimeWriter.Write(Stream stream, SmtpMessage message)
       at HigLabo.Net.Smtp.MimeWriter.Write(SmtpMessage message)
       at HigLabo.Net.Smtp.SendMailCommand..ctor(SmtpMessage message)
       at HigLabo.Net.Smtp.SmtpClient.SendMail(SmtpMessage message)
       at Sendemail.Program.Main(String[] args) in C:\Users\HP\Documents\Visual Studio 2010\Projects\Sendemail\Sendemail\Program.cs:line 36
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Aug 12, 2014 at 2:28 PM
Can i set it this way or this will cause duplicate headers ?
mg.Headers.Add(new SmtpMailHeader("Content-Type", "text/plain"));
Aug 13, 2014 at 4:45 PM
Any other solution or workaround i can use ?

I'd really appreciate your help!
Aug 15, 2014 at 2:48 PM
fixed the issue myself!

seems that the CharsetEncoding was throwing a null so i had to set it like this
SmtpMessage mg = new SmtpMessage();
HigLabo.Net.Smtp.ContentType ct =new  HigLabo.Net.Smtp.ContentType("text/plain");
ct.CharsetEncoding = Encoding.UTF8;
mg.ContentType = ct;
seems to have fixed the problem!
Aug 15, 2014 at 10:24 PM
thought that fixed it but still getting weird characters , i think this is due to a bad encoding , but when i change "text/plain" to "multipart/mixed" , it works just fine!
Aug 16, 2014 at 10:33 PM
anyone can help with this issue i'm having ?
Aug 18, 2014 at 12:49 AM
Hi aymen99.

Thank you for your report.
I'll modify internal code to improve more intuitive exception thrown.

Now, NullReferenceException issue was solved by setting CharsetEncoding.
I want to help you but I don't know what you want to do in next.

Can tell me more detail about your issue and what you want to do?
If you can, please let me know below 3 things.
1.Reproduct way (with code is better)
2.Expceted behavior (What you want to do)
3.Observed behavior (Actually observed)

It helps me to solve your problem.