(in C#) using System.Web.Mail; public void sendMail() { try { System.Web.Mail.MailMessage myMail=new MailMessage(); myMail.From = "myaccount@test.com"; myMail.To = "myaccount@test.com"; myMail.Subject = "MailTest"; myMail.Priority = MailPriority.Low; myMail.BodyFormat = MailFormat.Text; myMail.Body = "Test"; SmtpMail.SmtpServer="smarthost"; //your smtp server here SmtpMail.Send(myMail); } catch(Exception e) { throw e; } }
(in C#) public void CDOsendMail() { try { CDO.Message oMsg = new CDO.Message(); oMsg.From = "myaccount@test.com"; oMsg.To = "myaccount@test.com"; oMsg.Subject = "MailTest"; oMsg.HTMLBody = "Test"; CDO.IConfiguration iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2; oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "myaccount@test.com"; //sender mail oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "myaccount@test.com"; //email account oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username"; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password"; oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1; //value=0 代表Anonymous验证方式(不需要验证) //value=1 代表Basic验证方式(使用basic (clear-text) authentication. //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.) //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express) oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com"; oFields.Update(); oMsg.BodyPart.Charset="gb2312"; oMsg.HTMLBodyPart.Charset="gb2312"; oMsg.Send(); oMsg = null; } catch (Exception e) { throw e; } }
注意:由于Exchange2000的CDO组件cdoex.dll会更新原有的Windows2000的CDO组件cdosys.dll,所以如果您希望继续使用cdosys.dll,您必须先通过regsrv32.exe卸载掉cdoex.dll。public void SendMail(MailMessage msg) { NetworkStream nwstream = GetConnection(); WriteToStream(ref nwstream, "EHLO " + smtpHost + "/r/n"); string welcomeMsg = ReadFromStream(ref nwstream); // implement HELO command if EHLO is unrecognized. if (IsUnknownCommand(welcomeMsg)) { WriteToStream(ref nwstream, "HELO " + smtpHost + "/r/n"); } CheckForError(welcomeMsg, ReplyConstants.OK); // Authentication is used if the u/p are supplied AuthLogin(ref nwstream); WriteToStream(ref nwstream, "MAIL FROM: <" + msg.From.Address + ">/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); SendRecipientList(ref nwstream, msg.To); SendRecipientList(ref nwstream, msg.CC); SendRecipientList(ref nwstream, msg.BCC); WriteToStream(ref nwstream, "DATA/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.START_INPUT); if (msg.ReplyTo.Name != null && msg.ReplyTo.Name.Length != 0) { WriteToStream(ref nwstream, "Reply-To: /"" + msg.ReplyTo.Name + "/" <" + msg.ReplyTo.Address + ">/r/n"); } else { WriteToStream(ref nwstream, "Reply-To: <" + msg.ReplyTo.Address + ">/r/n"); } if (msg.From.Name != null && msg.From.Name.Length != 0) { WriteToStream(ref nwstream, "From: /"" + msg.From.Name + "/" <" + msg.From.Address + ">/r/n"); } else { WriteToStream(ref nwstream, "From: <" + msg.From.Address + ">/r/n"); } WriteToStream(ref nwstream, "To: " + CreateAddressList(msg.To) + "/r/n"); if (msg.CC.Count != 0) { WriteToStream(ref nwstream, "CC: " + CreateAddressList(msg.CC) + "/r/n"); } WriteToStream(ref nwstream, "Subject: " + msg.Subject + "/r/n"); if (msg.Priority != null) { WriteToStream(ref nwstream, "X-Priority: " + msg.Priority + "/r/n"); } if (msg.Headers.Count > 0) { SendHeaders(ref nwstream, msg); } if (msg.Attachments.Count > 0 ¦ ¦ msg.HtmlBody != null) { SendMessageBody(ref nwstream, msg); } else { WriteToStream(ref nwstream, msg.Body + "/r/n"); } WriteToStream(ref nwstream, "/r/n./r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); WriteToStream(ref nwstream, "QUIT/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.QUIT); CloseConnection(); }