vba 邮件body html,Excel VBA中的Outlook电子邮件和签名 - .Body vs .HTMLbody

尝试将数据插入正确的html标记:

.HTMLBody = "This is some text!"

对于空格,您必须添加此标记"
",例如:

.HTMLBody = " hello
"

.HTMLBody = .HTMLBody & " how
" & " are
" & " you?"

结果:

你好

如何

你?

EDIT2 强>

为了插入图像(签名为图像),您可以使用以下代码:

1步。将此代码复制到类模块中并将该模块命名为“MailOptions”

Private Message As CDO.Message

Private Attachment, Expression, Matches, FilenameMatch, i

Public Sub PrepareMessageWithEmbeddedImages(ByVal FromAddress, ByVal ToAddress, ByVal Subject, ByVal HtmlContent)

Set Expression = CreateObject("VBScript.RegExp")

Expression.Pattern = "\"

Expression.IgnoreCase = True

Expression.Global = False 'one match at a time

Set Message = New CDO.Message

Message.From = FromAddress

Message.To = ToAddress

Message.Subject = Subject

'Find matches in email body, incrementally increasing the auto-assigned attachment identifiers

i = 1

While Expression.Test(HtmlContent)

FilenameMatch = Expression.Execute(HtmlContent).Item(0).SubMatches(0)

Set Attachment = Message.AddAttachment(FilenameMatch)

Attachment.Fields.Item("urn:schemas:mailheader:Content-ID") = "" ' set an ID we can refer to in HTML

Attachment.Fields.Item("urn:schemas:mailheader:Content-Disposition") = "inline" ' "hide" the attachment

Attachment.Fields.Update

HtmlContent = Expression.Replace(HtmlContent, "cid:attachedimage" & i) ' update the HTML to refer to the actual attachment

i = i + 1

Wend

Message.HTMLBody = HtmlContent

End Sub

Public Sub SendMessageBySMTP(ByVal SmtpServer, ByVal SmtpUsername, ByVal SmtpPassword, ByVal UseSSL)

Dim Configuration

Set Configuration = CreateObject("CDO.Configuration")

Configuration.Load -1 ' CDO Source Defaults

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SmtpServer

'Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SmtpPort

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30

If SmtpUsername <> "" Then

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SmtpUsername

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SmtpPassword

End If

Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = UseSSL

Configuration.Fields.Update

Set Message.Configuration = Configuration

Message.Send

End Sub

第2步。在标准模块中,您将详细说明.html内容并从类中实例化对象:

public sub send_mail()

Dim signature As String

dim mail_sender as new MailOptions 'here you are instantiating an object from the class module created previously

dim content as string

signature = "C:\Users\your_user\Documents\your_signature.png"

content = "This is some text!"

content = content & """ />"

mail_sender.PrepareMessageWithEmbeddedImages _

FromAddress:="chrism_mail@blablabla.com", _

ToAddress:="addressee_mail@blablabla.com", _

Subject:="your_subject", _

HtmlContent:=content

'your_Smtp_Server, for example: RelayServer.Contoso.com

correos.SendMessageBySMTP "your_Smtp_Server", "your_network_user_account", "your_network_user_account_password", False

end sub

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值