正在阅读:三种使用SMTP协议发送邮件的方法三种使用SMTP协议发送邮件的方法

2005-06-03 10:18 出处: 作者:hedonister 责任编辑:moningfeng

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(); }

键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品