Jump to content

Send email to OutLook via SMTP


Recommended Posts

你好, 我通过以下代码将电子邮件发送到Outlook邮箱,电子邮件正文中的中文将被混淆,并且电子邮件标题中的中文可以正常显示。

请帮忙解决这个问题,谢谢!

 

#include <file.au3>


$ s_SmtpServer =“”; smtp服务器要使用的地址
$ s_FromName =“”;发送电子邮件的人的名字
$ s_FromAddress =“”;邮件应来自的地址
$ s_ToAddress =“”;电子邮件的目标地址
$ s_Subject =“”;来自电子邮件
$ as_Body =“”;邮件中的消息正文
$ s_AttachFiles =“”;要附加的文件
$ s_CcAddress =“”;抄送地址
$ s_BccAddress =“”;密件抄送地址
$ s_Username =“”;发送邮件所用帐户的用户名
$ s_Password =“”;发送邮件所用帐户的密码
$ IPPort = 25;启用/禁用安全套接字层发送
$ ssl = 0


Func sendmail()
        全局$ oMyRet [2]
        全局$ oMyError = ObjEvent(“ AutoIt.Error”,“ MyErrFunc”)
        $ rc = _INetSmtpMailCom($ s_SmtpServer,$ s_FromName,$ s_FromAddress,$ s_ToAddress,$ s_Subject,$ as_Body,$ s_AttachFiles,$ s_CcAddress,$ s_BccAddress,$ s_Username,$ s_Password,$ IPPort,$ ssl)
        如果@error然后
                MsgBox0,“错误发送消息”,“错误代码:”&@错误&“描述:”&$ rc)
        万一
    EndFunc; ==>发送邮件
    
全局$ oMyError = ObjEvent(“ AutoIt.Error”,“ MyErrFunc”)
全局$ oMyRet [2]
 
Func _INetSmtpMailCom($ s_SmtpServer,$ s_FromName,$ s_FromAddress,$ s_ToAddress,$ s_Subject =“”,$ as_Body =“”,$ s_AttachFiles =“”,$ s_CcAddress =“”,$ s_BccAddress =“”,$ s_Username =“ ,$ s_Password =“”,$ IPPort = 25$ ssl = 0)
    $ objEmail = ObjCreate(“ CDO.Message”)
    $ objEmail.From ='“'$ s_FromName&'” <'$ s_FromAddress&'>'
    $ objEmail.To = $ s_ToAddress
    本地$ i_Error = 0
    本地$ i_Error_desciption =“”
    如果$ s_CcAddress <>“”那么$ objEmail.Cc = $ s_CcAddress
    如果$ s_BccAddress <>“”那么$ objEmail.Bcc = $ s_BccAddress
    $ objEmail.Subject = $ s_Subject
    如果StringInStr$ as_Body,“ <”)和StringInStr$ as_Body,“>”)然后
        $ objEmail.HTMLBody = $ as_Body
    其他
        $ objEmail.Textbody = $ as_Body和@CRLF
    万一
    如果$ s_AttachFiles <>“”然后
        本地$ S_Files2Attach = StringSplit$ s_AttachFiles,“;”)
        对于$ x = 1$ S_Files2Attach [0]
            $ S_Files2Attach [$ x] = _PathFull$ S_Files2Attach [$ x])
            如果FileExists$ S_Files2Attach [$ x])然后
                $ objEmail.AddAttachment$ S_Files2Attach [$ x])
            其他
                $ i_Error_desciption = $ i_Error_desciption&@lf'找不到要附加的文件:'$ S_Files2Attach [$ x]
                SetError1)
                返回0
            万一
        下一个
    万一
    $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/sendusing”)= 2
    $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/smtpserver”)= $ s_SmtpServer
    $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/smtpserverport”)= $ IPPort
;经过认证的SMTP
    如果$ s_Username <>“”然后
        $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”)= 1
        $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/sendusername”)= $ s_Username
        $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/sendpassword”)= $ s_Password
    万一
    如果$ Ssl然后
        $ objEmail.Configuration.Fields.Item(“ http://schemas.microsoft.com/cdo/configuration/smtpusessl”)=真
    万一
;更新设置
    $ objEmail.Configuration.Fields.Update
; 发送消息
    $ objEmail.Send
    如果@error然后
        SetError2)
        返回$ oMyRet [1]
    万一
EndFunc; ==> _ INetSmtpMailCom
 
Func MyErrFunc()
    $ HexNumber =十六进制($ oMyError.number8)
    $ oMyRet [0] = $ HexNumber
    $ oMyRet [1] = StringStripWS$ oMyError.description3)
    ConsoleWrite(“ ### COM错误!编号:”&$ HexNumber&“ ScriptLine:”&$ oMyError.scriptline&“ Description:”&$ oMyRet [1]@LF)
    SetError1; 该函数返回时要检查的东西
    返回
EndFunc; ==> MyErrFunc

 

Edited by meety
Link to post
Share on other sites
  • Developers
14 minutes ago, meety said:

the Chinese in the body of the email will be confused

Which means what exactly? 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
On 2020/11/8 at AM8点26分, Zedna said:

在CDO.Message中,您可以像这样显式设置“ codepage”(这是针对捷克语的)

$ objEmail 。身体部位。ContentTransferEncoding  =  “ 8bit” 
$ objEmail 。身体部位。CharSet  =  “ windows-1250

 

thanks for your help

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...