本文共 8648 字,大约阅读时间需要 28 分钟。
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; }}
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 mailoFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"] .Value="myaccount@test.com"; //email accountoFields["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; }}
220 Welcome to coremail System(With Anti-Spam) 2.1EHLO 263.NET250-192.168.30.29250-PIPELINING250-SIZE 10240000250-ETRN250-AUTH LOGIN250 8BITMIMEAUTH LOGIN334 VXNlcm5hbWU6bXlhY2NvdW50334 UGFzc3dvcmQ6bXlwYXNzd29yZA==235 Authentication successfulMAIL FROM:myaccount@263.NET250 OkRCPT TO:myaccount@263.NET250 OkData354 End data with. This is a testing email.haha..250 Ok: queued as AC5291D6406C4QUIT221 Bye
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();}private bool AuthLogin(ref NetworkStream nwstream){if (username != null && username.Length > 0 && password != null && password.Length > 0){ WriteToStream(ref nwstream, "AUTH LOGIN\r\n"); if (AuthImplemented(ReadFromStream(ref nwstream))) { WriteToStream(ref nwstream, Convert.ToBase64String( Encoding.ASCII.GetBytes(this.username.ToCharArray())) + "\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.SERVER_CHALLENGE); WriteToStream(ref nwstream, Convert.ToBase64String(Encoding.ASCII.GetBytes( this.password.ToCharArray())) + "\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.AUTH_SUCCESSFUL); return true; }}return false;}