我们可以使用Outlook文件类型(.oft)模板并使用smtp邮件服务器发送电子邮件吗?

时间:2019-07-11 07:19:49

标签: c# outlook smtp

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

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

2 个答案:

答案 0 :(得分:0)

使用MsgReader库(安装包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

答案 1 :(得分:-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