gpt4 book ai didi

excel - Office 365 发送邮件(VBA)解决方案

转载 作者:行者123 更新时间:2023-12-04 03:16:14 24 4
gpt4 key购买 nike

我已经搜索了几个月,但仍然得到解决方案。

使用 Office365 的 CDO 发送电子邮件不起作用。

enter image description here

出现传输失败连接或身份验证失败等错误。

最佳答案

想分享解决方案,我构建解决通过 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)
创建安装程序真的很容易,记住将这两个文件放入安装程序中,然后构建它。

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

--> 把附件作为数组传入,这样就可以有多少了
--> 但请记住,Office365 每封电子邮件的最大限制为 30MB

谢谢

关于excel - Office 365 发送邮件(VBA)解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com