logo
down
shadow

save System.Net.mail.MailMessage as .msg file


save System.Net.mail.MailMessage as .msg file

By : vofannPln
Date : October 28 2020, 04:55 PM
This might help you Here Ryan suggests an easy and great way to do it whithout any effort.
code :


Share : facebook icon twitter icon
Sending a MailMessage with System.Net.Mail.SmtpClient: Why is the mail body attached as text file?

Sending a MailMessage with System.Net.Mail.SmtpClient: Why is the mail body attached as text file?


By : user1829275
Date : March 29 2020, 07:55 AM
To fix this issue The way an attachment is inserted in an SMTP message is the same as when there are alternate versions of the message. The message header of the main part has a MIME type meaning "multi-part" and a delimiter is defined. Then, several sub-messages are included, separated by this delimiter. Each sub-message is tagged with a MIME part defining the format of the body or of the attachment. For example, a Word file is tagged as something like "application/ms-word", while the plain text part is tagged as text/plain or HTML as text/html.
In the implementation of SmtpClient (I looked at it thanks to Reflector), if there is no attachment and no alternate version, an HTML sub-message is automatically created. Thus, an old client like Outlook 2000 thinks there is an attachment because I guess it does not support this notion of alternate views (and thus to it, the MIME type text/html is an attachment).
How to convert OpenPop.NET MailMessage to System.Net.Mail.MailMessage?

How to convert OpenPop.NET MailMessage to System.Net.Mail.MailMessage?


By : user2616037
Date : March 29 2020, 07:55 AM
hop of those help? If you are using OpenPop.NET >=version 2, you could just call the ToMailMessage method that accompanies the Message class.
Allow null for To, Cc, Bcc of mailmessage (system.net.mail.mailmessage)

Allow null for To, Cc, Bcc of mailmessage (system.net.mail.mailmessage)


By : Romain Piquot
Date : March 29 2020, 07:55 AM
I hope this helps . I am using System.Net.Mail.MailMessage to capture and pass it over the info related to email. , As Hans inferred in his comment, something like
code :
bool send = false;

if (!String.IsNullOrEmpty(mailMsg.To))
{
     mailMessage.CC.Add(mailMsg.To);
     send = true;
}
if (!String.IsNullOrEmpty(mailMsg.Cc))
{
     mailMessage.CC.Add(mailMsg.Cc);
     send = true;
}
if (!String.IsNullOrEmpty(mailMsg.Bcc))
{
     mailMessage.CC.Add(mailMsg.Bcc);
     send = true;
}
if (!send)
{
     // what to do if none are set...
}
How can I set the Attachment file encoding for a System.Net.Mail.MailMessage?

How can I set the Attachment file encoding for a System.Net.Mail.MailMessage?


By : Ernie
Date : March 29 2020, 07:55 AM
Does that help You can set the Charset property of the attachment's ContentType property to a valid value.
How to save System.Net.Mail MailMessage object into SQL Server database?

How to save System.Net.Mail MailMessage object into SQL Server database?


By : Stepan Nikoghosyan
Date : March 29 2020, 07:55 AM
this one helps. I think the update you posted is pretty good. Also check out this answer for How to save MailMessage object to disk as *.eml or *.msg file. You probably can create multiple clients, one to properly send and other to save the message to local file, which then needs to be saved to DB. I haven't tried this though.
If you are still looking into this, posted sample code...
code :
try
        {
            MailMessage message = new MailMessage();
            var logoPath = @"C:\MyLogo.jpg";
            message.From = new MailAddress("from@email.co.nz");
            message.To.Add("to@email.co.nz");
            message.IsBodyHtml = true;

            //Read the attachment into byte Array. This assumes single attachment only in the Mail Message.
            byte[] arr = File.ReadAllBytes(logoPath);

            using (var stream = new MemoryStream(arr))
            {
                stream.Position = 0;
                Attachment logo = new Attachment(stream, "Logo");
                string contentID = "Image";
                logo.ContentId = contentID;
                logo.ContentDisposition.Inline = true;
                logo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

                string wsHdr = "<html><body><p><a href=\"http://www.tecnq.com.au\" target=\"_blank\"><img alt=\"tecnqlogo\" src=\"cid:" + contentID + "\" style=\"border-style:none; height:34px; width:100px\" /></a></p>" + "</html></body>"; ;

                message.Body = wsHdr;
                message.Attachments.Add(logo);


                SmtpClient smtp = new SmtpClient();
                smtp.Host = "MyServer";
                smtp.Port = 25;
                smtp.Send(message);
                SaveMessage(message, arr);
            }             
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
  //ZERO ERRO CHECKING.
    private static void SaveMessage(MailMessage message, byte[] arr)
    {
        using (var conn = new SqlConnection("CONNECTION_STRING"))
        {
            using (var cmd = new SqlCommand("INSERT INTO MailSent (Body,Attachment) VALUES(@BODY,@ATTACHMENT)", conn))
            {
                conn.Open();
                var param = new SqlParameter("@BODY", SqlDbType.VarChar)
                {
                    Value = message.Body
                };
                var param2 = new SqlParameter("@ATTACHMENT", SqlDbType.Binary)
                {
                    Value = arr
                };
                cmd.Parameters.Add(param);
                cmd.Parameters.Add(param2);
                cmd.ExecuteNonQuery();
            }
        }            
    }
 try
        {
            MailMessage message = new MailMessage();
            byte[] arr2 = null;
            GetMailMessage(2, ref message, ref arr2);


            message.From = new MailAddress("from@email.co.nz");
            message.To.Add("to@email.co.nz");
            message.IsBodyHtml = true;

            using (var stream = new MemoryStream(arr2))
            {
                stream.Position = 0;
                Attachment logo = new Attachment(stream, "Logo");
                string contentID = "Image";
                logo.ContentId = contentID;
                logo.ContentDisposition.Inline = true;
                logo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                message.Attachments.Add(logo);

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "MyServer";
                smtp.Port = 25;
                smtp.Send(message);

            }

            // SaveMessage(message, arr);
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
        }
    //ZERO ERROR CHECKING.
    private static void GetMailMessage(int itemId, ref MailMessage msg2, ref byte[] arr2)
    {
        using (var conn = new SqlConnection("CONNECTION_STRING"))
        {
            using (var cmd = new SqlCommand("SELECT [Body],[Attachment] FROM MailSent WHERE ID = @itemId", conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@itemId", itemId);

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        msg2.Body = dr[0].ToString();
                        arr2 = dr[1] as byte[];
                    }
                }
            }
        }
    }
Related Posts Related Posts :
  • How to use Selenium Grid with C#?
  • What is the best way to download files via HTTP using .NET?
  • How to get files from a device using USB
  • Given a user's SID, how do I get their userPrincipalName?
  • NHibernate mapping in Asp.Net using MySql
  • Why do some cookies have a '.' before the domain?
  • C# SqlDataReader = null?
  • InvalidCastException for two Objects of the same type
  • "The parameters dictionary contains a null entry for parameter" - How to fix?
  • Font family name from font file
  • What is the best way to generate KML files in C#?
  • How can I receive mail using .NET?
  • How to send raw data over a network?
  • meaning of '+='
  • Object reference not set to an instance of an object #5
  • C# Create "wireframe"/3D "map"
  • How to change size of database
  • Serialization problem
  • Using unmanaged code from managed code
  • Are there any bindings between .NET and TK
  • error with linq join
  • VB.NET equivalent to C# var keyword
  • Accessing object properties from string representations
  • Inheritance issue
  • C# timer won't tick
  • How to retrieve items from a database c#
  • Sending mail using SmtpClient in .net
  • Tag problem c# listbox
  • How to know if the Form App open or not c#
  • C# XPath id() not working?
  • Load PDF from Memory ASP.Net
  • C# ListView with a ProgressBar
  • Getting the right WPF dispatcher in a thread
  • How to create Pivot table using C#?
  • how to download a file from remote server using asp.net
  • Binding files in C#?
  • Copy one object to another
  • How to post on Google Buzz?
  • Generic <T> how cast?
  • Set global hotkeys using C#
  • Change the key being pressed with C#
  • Uploading Large Files
  • How do I get the duration of a video file using C#?
  • how to create instance for a generic type in c#
  • Drag and drop rectangle in C#
  • RSA Encryption C#
  • Title=
  • What is meant by Web Services?
  • The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via
  • Check if server exists
  • time interval in c#
  • Extracting a sub-string in C#
  • C# - Programmatically Log-off and Log-on a user
  • c# array vs generic list
  • TCPClient in C# (Error)
  • How can I know if a file has been changed in .NET C#?
  • New to C# and trying to use a global variable
  • Convert RGB color to CMYK?
  • Tesseract.NET in C#
  • Is it possible to Update Sharepoint List Without "ID"?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk