DotNetOpenMail

        开源项目地址:http://dotnetopenmail.sourceforge.net/
What is DotNetOpenMail?

DotNetOpenMail allows you to send email from applications which use Microsoft's .Net development framework, including asp.net, C# and WinForms.

It is a freely available open-source component written in C# that makes it easy to create HTML and plain text email with file attachments without needing the System.Web.Mail library. A programmer can use it to create multipart/alternative, multipart/related and multipart/mixed MIME messages in various character sets and various mime encodings such as quoted-printable, 7bit, 8bit and base64 without needing to know too much about the details.

DotNetOpenMail能够使你在微软.net框架开发的asp.net, WinForm应用程序发送Email。
     它是C#编写的开源组件,它不需要使用System.Web.Mail类库就可以容易的创建带附件HTML和Plain-text的Email。程序员不需要知道很多相关的细节就可以使用不同的字符集或不同的MINE编码来创建multipart/alternative,multipart/related和multipart/mixed的MIME消息。

Examples

 

A simple text/html email:

            EmailMessage emailMessage = new EmailMessage();
            emailMessage.FromAddress = new EmailAddress("cswann@XXXXXX.com",
            "Charles Swann");
            emailMessage.AddToAddress(new EmailAddress("ocrecy@XXXXXX.com",
            "Odette de Crecy"));
            emailMessage.Subject = "Missed you";
            emailMessage.TextPart = new TextAttachment("Just checking where "+
            "you were last night.\r\nSend me a note!\r\n\r\n-Charles");
            emailMessage.HtmlPart = new HtmlAttachment("<html><body>"+
            "<p>Just checking up on where you were last night.</p>\r\n"+
            "<p>Send me a note!</p>\r\n\r\n"+
            "<p>-Charles</p></body></html>");
            emailMessage.Send(new SmtpServer("localhost"));
            
 

An email with an inline graphic:

            EmailMessage emailMessage = new EmailMessage();
            emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
            emailMessage.AddToAddress(new EmailAddress("marcel@XXXXXX.com"));
            emailMessage.Subject = "A photo of hawthorns";
            emailMessage.TextPart = new TextAttachment("This photo requires a better "+
            " email reader.");
            emailMessage.HtmlPart = new HtmlAttachment("<html><body>"+
            "<p>Note to self: look at this photo again in 30 years.</p>"+
            "<p><img src=\"cid:hawthorns\" alt=\"Hawthorn bush\"/></p>"+
            "<p>-Marcel</p>");
            FileInfo relatedFileInfo = new FileInfo(@"somehawthorns.jpg");
            FileAttachment relatedFileAttachment = new FileAttachment(relatedFileInfo,
            "hawthorns");
            relatedFileAttachment.ContentType = "image/jpeg";
            emailMessage.AddRelatedAttachment(relatedFileAttachment);
            emailMessage.Send(new SmtpServer("localhost"));
            
 

An email with a PDF attachment:

            EmailMessage emailMessage = new EmailMessage();
            emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
            emailMessage.AddToAddress(new EmailAddress("gilberte@XXXXXX.com"));
            emailMessage.Subject = "Something has come up";
            emailMessage.TextPart = new TextAttachment("I regret that something has "+
            "come up unexpectedly,\r\n"+
            "and I must postpone our meeting.\r\n\r\n"+
            "Please read the 20 pages of my thoughts on this in the attached\r\n"+
            "PDF file.\r\n\r\n-Marcel");
            emailMessage.HtmlPart = new HtmlAttachment("<p>I regret that something "+
            "has come up unexpectedly,\r\n"+
            "and I must postpone our meeting.</p>\r\n"+
            "<p>Please read the 20 pages of my thoughts on this in the attached\r\n"+
            "PDF file.</p>\r\n<p>-Marcel</p></body><html>");
            FileAttachment fileAttachment = new FileAttachment(new FileInfo("mythoughts.pdf"));
            fileAttachment.ContentType = "application/pdf";
            emailMessage.AddMixedAttachment(fileAttachment);
            emailMessage.Send(new SmtpServer("localhost"));
            
 

An email with a text file created in memory:

            EmailMessage emailMessage = new EmailMessage();
            emailMessage.FromAddress = new EmailAddress("licensing@XXXXXX.com");
            emailMessage.AddToAddress(new EmailAddress("gleek@XXXXXX.com"));
            emailMessage.Subject = "Here is your license";
            emailMessage.TextPart = new TextAttachment("Here is your"+
            "license file.\r\n");
            emailMessage.HtmlPart = new HtmlAttachment("<p>Here is your"+
            "license file.</p>\r\n</body><html>");
            MemoryStream stream = new MemoryStream();
            StreamWriter sw = new StreamWriter(stream);
            sw.WriteLine("this is some test data 1");
            sw.WriteLine("this is some test data 2");
            sw.WriteLine("this is some test data 3");
            sw.WriteLine("this is some test data 4");
            sw.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            FileAttachment fileAttachment = new FileAttachment(new StreamReader(stream));
            fileAttachment.FileName = "License.txt";
            fileAttachment.CharSet = System.Text.Encoding.ASCII;
            fileAttachment.ContentType = "text/plain";
            emailMessage.AddMixedAttachment(fileAttachment);
            emailMessage.Send(new SmtpServer("localhost"));
            
 

Using SMTP AUTH (LOGIN):

            EmailMessage emailMessage = new EmailMessage();
            emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
            emailMessage.AddToAddress(new EmailAddress("albertine@XXXXXX.com"));
            emailMessage.Subject = "Something has come up";
            emailMessage.HtmlPart = new HtmlAttachment("<html><p>body<p>The greatness "+
            "of true art is that it rediscovers, holds, and communicates "+
            "to us that reality from which we live further and further away,
            "as the conventional knowledge we substitute for it becomes heavier "+
            "and more impenetrable.\r\n</p>"+
            "<p>-Marcel</p></body><html>");
            SmtpServer smtpServer=new SmtpServer("localhost");
            smtpServer.SmtpAuthToken=new SmtpAuthToken("marcel", "madeleine");
            emailMessage.Send(smtpServer);