0

我正在努力从 excel 表中向每个学生发送包含(学生姓名和他的分数)的电子邮件,如下所示

在此处输入图像描述

在此处输入图像描述

一切正常,但是当学生姓名为阿拉伯字符时。名称显示为 (???? ),如下所示

在此处输入图像描述

我将本地系统的设置更改为阿拉伯语,但仍然遇到同样的问题。

有什么建议吗?

4

2 回答 2

0

您需要设置htmlBody和使用 utf-8 字符集。

使用以下函数将文本字符串简单地转换为 html 字符串。

Function StringToHTML(sStr As String) As String
    sStr = Replace(sStr, Chr(10), "<br/>")
    sStr = Replace(sStr, Chr(13), "<br/>")
    sStr = Replace(sStr, Chr(11), "<br/>")
    StringToHTML = "<!doctype html><html lang=""en""><body><p>"
    StringToHTML = StringToHTML & sStr
    StringToHTML = StringToHTML & "</p></body></html>"
End Function

参考this,你需要objEmail.TextBody = mailBody用以下两行替换该行

objEmail.htmlBody = StringToHTML(mailBody)
objEmail.HtmlBodyPart.Charset = "utf-8"

如果您遇到更多问题(例如电子邮件主题包含阿拉伯字符但无法正确显示),请尝试添加这两行

objEmail.TextBodyPart.Charset = "utf-8"
objEmail.BodyPart.Charset = "utf-8"

编辑(以下评论)

你的完整代码应该是这样的

Sub SendMail()
    Dim objEmail
    Dim mailBody as String

    Const cdoSendUsingPort = 2  ' Send the message using SMTP
    Const cdoBasicAuth = 1      ' Clear-text authentication
    Const cdoTimeout = 100      ' Timeout for SMTP in seconds

     mailServer = "smtp.gmail.com"
     SMTPport = 465     '25 'SMTPport = 465
     mailusername = "email@some.com"
     mailpassword = "password"
     ''''''''
     
     Dim n As Integer
     n = Application.WorksheetFunction.CountA(Range("c:c"))
     For i = 2 To n
     
     mailto = Range("c" & i).Value
     mailSubject = Range("e" & i).Value
     mailBody = "Hi " & Range("b" & i) & "," & vbCrLf & vbCrLf & _
               "Below you can find your marks:" & vbCrLf & vbCrLf & _
               "Math: - " & Range("F" & i) & vbCrLf & _
               "Network: - " & Range("G" & i) & vbCrLf & _
               "Physics: - " & Range("H" & i) & vbCrLf & _
               "Antenna: - " & Range("I" & i)

    Set objEmail = CreateObject("CDO.Message")
    Set objConf = objEmail.Configuration
    Set objFlds = objConf.Fields

    With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword
    .Update
    End With

    objEmail.To = mailto
    objEmail.From = mailusername
    objEmail.Subject = mailSubject
    objEmail.htmlBody = StringToHTML(mailBody)
    objEmail.HtmlBodyPart.Charset = "utf-8"
    objEmail.Send

    Set objFlds = Nothing
    Set objConf = Nothing
    Set objEmail = Nothing
    Next i
End Sub

Function StringToHTML(sStr As String) As String
    sStr = Replace(sStr, Chr(10), "<br/>")
    sStr = Replace(sStr, Chr(13), "<br/>")
    sStr = Replace(sStr, Chr(11), "<br/>")
    StringToHTML = "<!doctype html><html lang=""en""><body><p>"
    StringToHTML = StringToHTML & sStr
    StringToHTML = StringToHTML & "</p></body></html>"
End Function
于 2020-08-15T16:59:58.500 回答
0

@Super Symmetry,真的很感谢你的合作,我还是会出错

在此处输入图像描述

我当前的代码

 Sub SendMail()
    Dim objEmail

    Const cdoSendUsingPort = 2  ' Send the message using SMTP
    Const cdoBasicAuth = 1      ' Clear-text authentication
    Const cdoTimeout = 100      ' Timeout for SMTP in seconds

     mailServer = "smtp.gmail.com"
     SMTPport = 465     '25 'SMTPport = 465
     mailusername = "laggnaimtehan@gmail.com"
     mailpassword = "*****"
     ''''''''
     Dim n As Integer
     n = Application.WorksheetFunction.CountA(Range("c:c"))
     For i = 2 To n
     
     mailto = Range("c" & i).Value
     mailSubject = Range("e" & i).Value
     mailBody = "Hi " & Range("b" & i) & "," & vbCrLf & vbCrLf & _
               "Below you can find your marks:" & vbCrLf & vbCrLf & _
               "Math: - " & Range("F" & i) & vbCrLf & _
               "Network: - " & Range("G" & i) & vbCrLf & _
               "Physics: - " & Range("H" & i) & vbCrLf & _
               "Antenna: - " & Range("I" & i)

    Set objEmail = CreateObject("CDO.Message")
    Set objConf = objEmail.Configuration
    Set objFlds = objConf.Fields

    With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword
    .Update
    End With

    objEmail.To = mailto
    objEmail.From = mailusername
    objEmail.Subject = mailSubject
    objEmail.HTMLBodyPart.Charset = "utf-8"
    objEmail.HTMLBody = StringToHTML(mailBody)
    objEmail.TextBodyPart.Charset = "utf-8"
    objEmail.BodyPart.Charset = "utf-8"
    objEmail.Send

    Set objFlds = Nothing
    Set objConf = Nothing
    Set objEmail = Nothing
    Next i
  End Sub

   Function StringToHTML(sStr As String) As String
     sStr = Replace(sStr, Chr(10), "<br/>")
     sStr = Replace(sStr, Chr(13), "<br/>")
     sStr = Replace(sStr, Chr(11), "<br/>")
     StringToHTML = "<!doctype html><html lang=""en""><body><p>"
     StringToHTML = StringToHTML & sStr
     StringToHTML = StringToHTML & "</p></body></html>"
    End Function
 

提前致谢 ....

于 2020-08-15T18:52:38.233 回答