【发布时间】:2021-12-12 05:02:38
【问题描述】:
public ExchangeService CreateConnection(string url)
{
ServicePointManager.ServerCertificateValidationCallback = (object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
ExchangeService service = new ExchangeService();
service.Url = new Uri(url);
service.UseDefaultCredentials = true; // Uses users windows login credentials
return service;
}
public void SendEmail(string sEmailTo, string slocation, DateTime dtdate, string slogyard, string sScaling, string sLogMfct, string sOther, string sScaler, int icheckbox)
{
string scheckbox;
string sDomain = Globals.gDefaultEmailSuffix;
string sUserName = SystemInformation.UserName;
ExchangeService service = CreateConnection("https://myexchangeserver.com/EWS/Exchange.asmx");
string sHTMLpre = $"{EmailBody(Globals.gHTML)}";
EmailMessage mailMessage = new EmailMessage(service); // Use with exchange service
ReplaceWithBR(slogyard);
ReplaceWithBR(sScaling);
ReplaceWithBR(sLogMfct);
ReplaceWithBR(sOther);
if (icheckbox > 0)
scheckbox = "Yes";
else
scheckbox = "No";
sHTMLpre = sHTMLpre.Replace("[@Location]", slocation);
sHTMLpre = sHTMLpre.Replace("[@Date]", dtdate.ToString());
sHTMLpre = sHTMLpre.Replace("[@LogYard]", slogyard);
sHTMLpre = sHTMLpre.Replace("[@Scaling]", sScaling);
sHTMLpre = sHTMLpre.Replace("[@LogManufacturing]", sLogMfct);
sHTMLpre = sHTMLpre.Replace("[@Scaler]", sScaler);
sHTMLpre = sHTMLpre.Replace("[@Other]", sOther);
sHTMLpre = sHTMLpre.Replace("[@RemotesExist]", scheckbox);
mailMessage.ToRecipients.Add(new EmailAddress($"{sEmailTo}@{sDomain}")); // Use with Exchange service
mailMessage.From = new EmailAddress($"{sUserName}@{sDomain}"); // Use with Exchange service
mailMessage.Subject = $"Scaling Yard Report for {slocation} on {dtdate}"; // Use with Exchange
mailMessage.Body = sHTMLpre; // Use with Exchange
try
{
mailMessage.SendAndSaveCopy()); //Use with Exchange Service
if (Globals.bLastEmail == true)
{
MessageBox.Show("Email Sent Successfully!");
Globals.MonthlyLog("Email Sent", $"Recipients:{Globals.gEmailList}");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Globals.MonthlyLog("Email Fail", $"{ex.Message}");
return;
}
}
这是运行良好的 VB.Net 代码:
Public Function CreateConnection(ByVal url As String) As ExchangeService
ServicePointManager.ServerCertificateValidationCallback = Function(ByVal obj As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal errors As SslPolicyErrors) True
Dim service As New ExchangeService()
service.Url = New Uri(url)
service.UseDefaultCredentials = True ' Uses users windows login credentials
Return service
End Function
Public Sub SendEmail(sEmailTo As String, slocation As String, dtdate As String, slogyard As String, sScaling As String, sLogMfct As String, sOther As String, sScaler As String, scheckbox As String)
Dim sDomain As String = gDefaultEmailSuffix
Dim sUserName As String = SystemInformation.UserName
Dim service As ExchangeService = CreateConnection("https://myexchangeserver.com/EWS/Exchange.asmx")
Dim sHTMLpre As String = $"{EmailBody(gHTML)}"
Dim mailMessage = New EmailMessage(service) ' Use with exchange service
'Dim mailMessage As New MailMessage() ' Use with SMTP client service
slogyard = slogyard.Replace(ControlChars.Lf, "<br>")
sScaling = sScaling.Replace(ControlChars.Lf, "<br>")
sLogMfct = sLogMfct.Replace(ControlChars.Lf, "<br>")
sOther = sOther.Replace(ControlChars.Lf, "<br>")
If scheckbox = True Then
scheckbox = "Yes"
Else
scheckbox = "No"
End If
sHTMLpre = sHTMLpre.Replace("[@Location]", slocation)
sHTMLpre = sHTMLpre.Replace("[@Date]", dtdate)
sHTMLpre = sHTMLpre.Replace("[@LogYard]", slogyard)
sHTMLpre = sHTMLpre.Replace("[@Scaling]", sScaling)
sHTMLpre = sHTMLpre.Replace("[@LogManufacturing]", sLogMfct)
sHTMLpre = sHTMLpre.Replace("[@Scaler]", sScaler)
sHTMLpre = sHTMLpre.Replace("[@Other]", sOther)
sHTMLpre = sHTMLpre.Replace("[@RemotesExist]", scheckbox)
mailMessage.ToRecipients.Add(New EmailAddress($"{sEmailTo}@{sDomain}")) ' Use with Exchange service
mailMessage.From = New EmailAddress($"{sUserName}@{sDomain}") ' Use with Exchange service
mailMessage.Subject = $"Scaling Yard Report for {slocation} on {dtdate}" ' Use with Exchange
mailMessage.Body = sHTMLpre ' Use with Exchange
'mailMessage.IsBodyHtml = True ' Use with SMTP CLient
'mailMessage.To.Add($"{sEmailTo}@{sDomain}") ' Use with SMTP CLient
'mailMessage.From = New MailAddress($"{sUserName}@{sDomain}") ' Use with SMTP
'mailMessage.Subject = $"Scaling Yard Report for {slocation} on {dtdate}" ' Use with SMTP
'mailMessage.Body = sHTMLpre ' Use with SMTP
'Dim SmtpCli As New SmtpClient() ' Leaving this just in case the EWS breaks
'With SmtpCli ' SMTP Client Code
' .UseDefaultCredentials = False
' .Port = 25
' .DeliveryMethod = SmtpDeliveryMethod.Network
' .Host = gSMTPServer
' .Credentials = New NetworkCredential(gSMTPUsername, gSMTPPassword)
'End With
Try
'SmtpCli.Send(mailMessage) ' Use with SMPT client
mailMessage.SendAndSaveCopy() ' Use with Exchange Service
If bLastEmail = True Then
MsgBox("Email Sent Successfully!")
Call MonthlyLog("Email Sent", $"Recipients:{gEmailList}")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Call MonthlyLog("Email Fail", $"{ex.Message}")
Exit Sub
End Try
End Sub
它在以下行失败:mailMessage.SendAndSaveCopy() - 在 C# 但不是 VB.Net
收到错误:“请求失败。底层连接已关闭:发送时发生意外错误”
正如我所说,它适用于 vb.net 代码,因此它不是像限制那样的交换服务器设置。有什么想法吗?
【问题讨论】:
-
我建议你尝试从VB到C#的代码转换,并将结果与你的结果进行比较......
-
您确定您的 VB 版本可以像您在此处提供的那样工作吗?我看到有一些使用
SmtpClient注释掉的代码。是否有可能是有效的VB代码。如果不是这样,您运行 VB 的环境与代码的 C# 版本之间有什么区别吗?我看到它正在使用 Windows 身份验证。不同的机器可能有不同的凭据? -
@JackA。 100% 有效。 smtpclient 的东西是我留下的旧代码,以防 EWS 失败(因为我最近切换到 EWS),我可以把它切换回来。我在同一台 PC 上同时打开和运行这两个程序,注释掉了 smtpclient,它在 vb 项目中运行良好,在 C# 项目中出现错误。带有 EWS 代码的 vb 代码在过去 4 个月内一直运行良好。我们决定将一些项目翻译成 C#,这是我第一次尝试。
-
@NoChance 我已经这样做了,这就是我到达这里的原因。这是我第一个从 vb.net 到 C# 的项目转换。
-
@JackA。它在 4.5 的 VB.net 代码中工作。我的 VB 项目没有使用任何 DevExpress 工具或表单,就像我在 c# 转换中所做的那样。感谢您的帮助。
标签: c# vb.net email exchangewebservices