【问题标题】:Can we use outlook file type(.oft) template and send email using smtp mail server?我们可以使用 Outlook 文件类型(.oft)模板并使用 smtp 邮件服务器发送电子邮件吗?
【发布时间】:2019-11-20 20:17:39
【问题描述】:

参考:https://docs.microsoft.com/en-us/office/vba/api/Outlook.Application.CreateItemFromTemplate

是否可以使用上述参考但使用其他 smtp 服务器发送电子邮件?

【问题讨论】:

  • @HimBromBeere 我不是在这里寻找任何具体的答案。我已经阅读了几篇文章和代码项目,它说这是不可能的,因此我只是想确定,如果有可能,那么我从出口寻找正确的方向。

标签: c# outlook smtp


【解决方案1】:

使用 MsgReader 库(Install-Package MSGReader -Version 3.7.3)

这是适合我的解决方案。

using (var msg = new MsgReader.Outlook.Storage.Message(@"C:\\test.oft"))
            {
                var from = msg.Sender;
                var sentOn = msg.SentOn;
                var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
                var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
                var subject = msg.Subject;
                var htmlBody = msg.BodyHtml;
                var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("^^service account email^^", "^^service account password^^"),
                    EnableSsl = true
                };
                var mailMessage = new MailMessage()
                {
                    From = new MailAddress("^^from email^^"),
                    Subject = subject,
                    Body = htmlBody,
                    IsBodyHtml = true,
                    Priority = MailPriority.Normal
                };
                mailMessage.To.Add("^^to email^^");
                client.Send(mailMessage);
            }

注意:解决方案不需要安装任何 Outlook 应用程序,也不需要对话框/弹出窗口。

有用的链接:

http://forums.codeguru.com/showthread.php?538563-Read-Outlook-Email-Template-(-OFT)-File-and-Save-Message-as-MSG-Format-in-C
https://stackoverflow.com/questions/26633082/read-outlook-msg-file
https://www.codeproject.com/Articles/19571/MsgReader-DLL

【讨论】:

    【解决方案2】:

    如果您在 Outlook 中配置了多个帐户(包括一个 smtp 邮件服务器),您可以使用SendUsingAccount 属性,该属性允许设置一个Account 对象,该对象代表将在其下发送MailItem 的帐户。

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
       If oAccount.AccountType = olPop3 Then  
         Dim oMail As Outlook.MailItem  
         Set oMail = Application.CreateItem(olMailItem)  
         oMail.Subject = "Sent using POP3 Account"  
         oMail.Recipients.Add ("someone@example.com")  
         oMail.Recipients.ResolveAll  
         Set oMail.SendUsingAccount = oAccount  
         oMail.Send  
       End If 
     Next  
    End Sub
    
    

    您可能会发现以下文章对您有所帮助:

    How To: Create a new Outlook message based on a template

    How To: Fill TO,CC and BCC fields in Outlook programmatically

    How To: Create and send an Outlook message programmatically

    【讨论】:

    • 虽然这个答案可能会或可能不会帮助 OP(这个问题太不清楚了,无法估计),但它肯定是用错误的语言编写的。
    • 感谢@Eugene,但我们的 smpt 服务器不是 Outlook 的一部分。
    • 那为什么还要使用Outlook的CreateItemFromTemplate呢?
    • 我们的要求是针对不同的客户,可能会有不同的电子邮件模板,并且创建 Outlook 电子邮件模板也很容易如果我们的电子邮件模板包含任何图像,我不需要创建相同的 CDN 链接图片。希望我能解释一下。
    • 您只需在 Outlook 中配置一个 SMTP 帐户,您的任务就会轻松解决。
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2015-08-10
    • 1970-01-01
    • 2012-06-10
    • 2016-09-07
    • 1970-01-01
    • 2022-08-20
    • 2016-02-09
    相关资源
    最近更新 更多