Office 365发送电子邮件(VBA)的解决方案

时间:2016-11-23 08:35:04

标签: excel vba smtp office365

我已经搜索了几个月,但仍然在寻求解决方案。

使用CDO for Office365发送电子邮件无效。

enter image description here

获取传输错误无法连接或身份验证失败。

1 个答案:

答案 0 :(得分:1)

想要共享解决方案,我构建解决方案以通过Office365 SMTP发送电子邮件。

1)我们需要为Excel构建自定义DLL

2)将DLL打包为安装程序,然后安装到计算机中(如果您想共享宏)

3)通过Excel VBA使用DLL

让我们开始吧:

1)为Excel创建自定义DLL(源代码)

重要的一点是让一切工作都是域

client.Credentials = new System.Net.NetworkCredential(Email, Password, "outlook.com");

如果域名错误或为空,则无效。

using System.Net.Mail;

namespace Eric_Library
{
    public class SMTP
    {
        public string oSMTP(string Email, string Password, string subject, string htmlBody, 
                            string[] Attachments,
                            string To, string Cc, string Bcc)
        {
            Email = Email.Trim();

        try
        {
            if (!Email.EndsWith("@outlook.com", StringComparison.CurrentCultureIgnoreCase))
                throw new Exception("Your domain is not matching");

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.office365.com");
            client.TargetName = "STARTTLS/smtp.office365.com";
            client.UseDefaultCredentials = false;

            //Domain name can be "company.com" or "outlook.com" or etc
            client.Credentials = new System.Net.NetworkCredential(Email, Password, "outlook.com");
            client.EnableSsl = true;
            client.Port = 587;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new MailAddress(Email);
            msg.CC.Add(Email);
            msg.Subject = subject;
            msg.Body = htmlBody;
            msg.IsBodyHtml = true;

            if (string.IsNullOrEmpty(To))
                throw new Exception("To cannot be blank");
            else
            {
                To.Replace(";", ",");
                msg.To.Add(To);
            }

            if (!string.IsNullOrEmpty(Cc))
            {
                Cc.Replace(";", ",");
                msg.CC.Add(Cc);
            }

            if (!string.IsNullOrEmpty(Bcc))
            {
                Bcc.Replace(";", ",");
                msg.Bcc.Add(Bcc);
            }

            if (Attachments.Count() > 0)
            {
                foreach (var item in Attachments)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        System.Net.Mail.Attachment attachment;
                        attachment = new System.Net.Mail.Attachment(item);
                        msg.Attachments.Add(attachment);
                    }
                }
            }

            client.Send(msg);
            return "Message Sent : " + DateTime.Now.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

}

***请记得检查注册COM互操作,否则您将无法在VBA中将其添加为参考 enter image description here

2)将DLL打包为安装程序(我的项目名称为Office365 SMTP Library) 创建安装程序非常简单,请记住将这2个文件写入安装程序,然后构建它。

enter image description here

3)通过Excel VBA使用DLL 转到Program目录,然后选择tlb文件将其添加为参考。

enter image description here

- >如果您将宏共享给其他用户,请确保他们也安装了DLL

- >他们不需要再次添加引用,Excel会自动查找。

现在你可以使用DLL了

Private Sub test_oMail()

Dim oMsg As Office365_SMTP_Library.SMTP
Set oMsg = New Office365_SMTP_Library.SMTP

Dim nArr_Attach() As String
ReDim nArr_Attach(1)

nArr_Attach(0) = "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"
nArr_Attach(1) = "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"

Debug.Print oMsg.oSmtp("email", "password", _
                "Testing Subject", "<p>First Paragraph</p><p>Second Paragraph</p>", _
                nArr_Attach, "TO", "CC", "BCC")

End Sub

- &GT;将附件作为数组传递,以便您可以拥有所需的数量 - &GT;但请记住,每个电子邮件的Office365最大限制为30MB

由于