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


private void btnCreateFile_Click(object sender, EventArgs e)
        {
            string attachFile = Path.Combine(Application.StartupPath, "Attachment/Hotel2.png");
            List<string> attachments = new List<string>();
            attachments.Add(attachFile);
            string subject = "测试邮件";
            string body = "<html>这是一份测试邮件,来自<font color=red><b>LumiSoft.Net</b></font></html>";
            string bodyEmbedy = "<html>这是一份测试邮件<img src=https://www.jb51.net/article/\"cid:test.jpg\">,来自<font color=red><b>LumiSoft.Net</b></font></html>";
            Dictionary<string, string> embedList = new Dictionary<string, string>();
            embedList.Add("test.jpg", "C:\\test.jpg");

//存储为Eml文件
            string path = Path.Combine(Application.StartupPath, "Eml");
            DirectoryUtil.AssertDirExist(path);
            string emlFile = string.Format("{0}/{1}.eml", path, DateTime.Now.ToFileTime());

Mime m = Create_Html_Attachment_Image(to, from, from, subject, bodyEmbedy, attachments, embedList);
            m.ToFile(emlFile);

MessageUtil.ShowTips("OK");
        }


发送EML文件操作如下所示。

复制代码 代码如下:


private void btnSendFile_Click(object sender, EventArgs e)
        {
            using (SMTP_Client client = new SMTP_Client())
            {
                int smtpPort = smtpUseSsl ? WellKnownPorts.SMTP_SSL : WellKnownPorts.SMTP;

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);
                client.RcptTo(to);

string path = Path.Combine(Application.StartupPath, "Eml");
                string emlFile = Directory.GetFiles(path)[0];
                var msg = Mail_Message.ParseFromFile(emlFile);

MemoryStream stream = new MemoryStream();
                msg.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
                stream.Position = 0;
                client.SendMessage(stream);
                client.Disconnect();
            }
            MessageUtil.ShowTips("OK");
        }


您可能感兴趣的文章:

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

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