JMail.net
在ASP.net用JMail发送邮件的方法,它是用VS.net为我们提供的工具来把COM组件和.net的组件进行了转化,但在实际的应用中发现,它的内存占用很厉害。好了,Dimac为我们发布了JMail的.net版本.
一、下载
进入网站下载地址:http://www.dimac.net/default2.asp?M=Products/MenuDOTNET.asp&P=Products/JMaildotnet/start.htm
由于下载还需要填写邮件和其他信息,为了让大家更加方便下载点击这里可以直接下载:http://www.dimac.net/FreeDownloads/Freedownload.asp?Id=3F824152-C635-4953-ABFF-0CB11E310C0A
二、 SMTP发送邮件
源代码C# SMTP范例
using System;
using Dimac.JMail;
namespace JMailTest
{
class MainClass
{
[STAThread]
static void Main( string[] args )
{
// create JMail message
Message message = new Message();
// set sender
message.From.Email = "[email protected]";
// add a recipient
message.To.Add( "[email protected]" );
// set the subject & body
message.Subject = "Hello, world!";
message.BodyText = "Hello, world! My hovercraft is full of eels.";
// add an attachment
message.Attachments.Add( "C://me_dancing.wmv" );
// send the message
try
{
Smtp.Send( message, "mail.mydomain.com" );
Console.WriteLine( "The message has been sent." );
}
catch ( Exception ex )
{
Console.WriteLine( "Failed to send message: {0}", ex.Message );
}
}
}
}
三、POP3接收邮件范例using System; using Dimac.JMail; namespace JMailTest { class MainClass { [STAThread] static void Main( string[] args ) { // connect to pop3 server using( Pop3 pop3 = new Pop3( "mail.mydomain.com", 110, "me", "secret" ) ) { // iterate message foreach( Message message in pop3.Inbox ) { // print interesting headers Console.WriteLine( "{0}/t{1}/t{2}", message.Date, message.From, message.Subject ); } } } } }
四、利用 MailMerge实现群发邮件
using System;
using System.Data;
using System.Data.Odbc;
using Dimac.JMail;
namespace JMailTest
{
class MainClass
{
[STAThread]
static void Main( string[] args )
{
// create mailmerge instance
MailMerge mm = new MailMerge();
// setup template
mm.Template.From = "[email protected]";
mm.Template.To.Add( "<%# EmailAddress %>", "<%# Name %>" );
mm.Template.Subject = "Hello <%# Name %>";
mm.Template.BodyText = "Hello there <%# Name %>. This is your e-mail address: <%# EmailAddress %>!";
// set output directory
mm.OutputDirectory = "C://MailServer//Pickup";
// open sql connection
using( OdbcConnection connection = new OdbcConnection( "DSN=MyDSN" ) )
using( OdbcCommand command = new OdbcCommand( "SELECT EmailAddress, Name FROM Contacts", connection ) )
{
connection.Open();
// get data reader
using( IDataReader reader = command.ExecuteReader() )
{
// set data source
mm.DataSource = reader;
// databind!
MailMergeResult result = mm.DataBind( false );
Console.WriteLine( "Success: {0}, Failure: {1}", result.SuccessCount, result.FailureCount );
}
}
}
}
}
五、总结
Jmail.net完全基于.net框架开发的,应该会与asp.net更加紧密的结合在一起。更重要是JMailServer也同时发布了,这样一来可以自己架设邮件服务器了。可惜还不能下载。有兴趣的可以在dimac的网站上找找看。