深入Lumisoft.NET组件与.NET API实现邮件发送功能的对(4)


public bool Send()
        {
            bool sended = false;
            using (SMTP_Client client = new SMTP_Client())
            {
                client.Connect(smtpServer, smtpPort, smtpUseSsl);
                client.EhloHelo(smtpServer);
                var authhh = new AUTH_SASL_Client_Plain(username, password);
                client.Auth(authhh);
                //client.Authenticate(username, password);
                //string text = client.GreetingText;
                client.MailFrom(from, -1);
                foreach (string address in toList.Keys)
                {
                    client.RcptTo(address);
                }

//采用Mail_Message类型的Stream
                Mail_Message m = Create_PlainText_Html_Attachment_Image(toList, ccList, from, fromDisplay, subject, body, attachments);
                using (MemoryStream stream = new MemoryStream())
                {
                    m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
                    stream.Position = 0;
                    client.SendMessage(stream);

sended = true;
                }
                if (m != null)
                {
                    m.Dispose();
                }

client.Disconnect();
            }
            return sended;
        }


构造用于SMTP发送的数据,可以使用Mail_Message 对象,也可以使用Mime对象,虽然读都可以实现发送功能,不过Mime对象是舍弃的对象了。

构造Mail_Message对象后,创建用于发送的格式要转换为Stream对象。转换为发送的Stream操作如下所示。

复制代码 代码如下:


using (MemoryStream stream = new MemoryStream())
{
        m.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
        stream.Position = 0;
        client.SendMessage(stream);

sended = true;
 }


构造Mail_Message格式的邮件操作如下所示。

复制代码 代码如下:


private Mail_Message Create_PlainText_Html_Attachment_Image(Dictionary<string,string> tomails, Dictionary<string, string> ccmails, string mailFrom, string mailFromDisplay,
            string subject, string body, Dictionary<string, string> attachments, string notifyEmail = "", string plaintTextTips = "")
        {
            Mail_Message msg = new Mail_Message();
            msg.MimeVersion = "1.0";
            msg.MessageID = MIME_Utils.CreateMessageID();
            msg.Date = DateTime.Now;
            msg.Subject = subject;
            msg.From = new Mail_t_MailboxList();
            msg.From.Add(new Mail_t_Mailbox(mailFromDisplay, mailFrom));
            msg.To = new Mail_t_AddressList();
            foreach (string address in tomails.Keys)
            {
                string displayName = tomails[address];
                msg.To.Add(new Mail_t_Mailbox(displayName, address));
            }
            msg.Cc = new Mail_t_AddressList();
            foreach (string address in ccmails.Keys)
            {
                string displayName = ccmails[address];
                msg.Cc.Add(new Mail_t_Mailbox(displayName, address));
            }           

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgxzd.html