Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use vbs to send mail with attachments

Shulou Source: shulou.com Published: 2022-06-03 07:42:29 10月03日 Update

This article mainly introduces how to use vbs to send attached e-mail, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Function Send_mail (You_Account,You_Password,Send_Email,Send_Email2,Send_Topic,Send_Body,Send_Attachment)

'code by NetPatch

'description of parameters for VBS to send email

You_Account: your email account

You_Password: your email password

'Send_Email: primary email address

'Send_Email2: alternate email address

Send_Topic: the subject of the message

'Send_Body: the content of the message

'Send_Attachment: email attachment

You_ID=Split (You_Account, @,-1, vbTextCompare)

'The account is separated from the server

MS_Space = "http://schemas.microsoft.com/cdo/configuration/"

This is a must, but you can rest assured that e-mail will not be sent through Microsoft

Set Email = CreateObject ("CDO.Message")

Email.From = You_Account

This must be the same as the account that sent the mail

Email.To = Send_Email 'primary email address

If Send_Email2 "" Then

Email.CC = Send_Email2 'alternate email address

End If

Email.Subject = Send_Topic 'email subject

Email.Textbody = Send_Body 'message content

If Send_Attachment "" Then

Email.AddAttachment Send_Attachment 'email attachment

End If

With Email.Configuration.Fields

.Item (MS_Space& "sendusing") = 2 'outgoing port

.Item (MS_Space& "smtpserver") = "smtp." & You_ID (1) 'SMTP server address

.Item (MS_Space& "smtpserverport") = 25 'SMTP server port

.Item (MS_Space& "smtpauthenticate") = 1 'cdobasec

.Item (MS_Space& "sendusername") = You_ID (0) 'your email account

.Item (MS_Space& "sendpassword") = You_Password 'your email password

.Update

End With

Email.Send

'send an email

Set Email=Nothing

'close the component

Send_Mail=True

If there is no error message, it means it was sent successfully, otherwise it failed.

If Err Then

Err.Clear

Send_Mail=False

End If

End Function

The following is an example of sending an email with an attachment using the function above

If Send_Mail ("test@163.com", "test", "test2@163.com", "", "message subject", "message content", "d:\ test.exe") = True Then

Wscript.Echo "sent successfully"

Else

Wscript.Echo "send failed"

End If

Thank you for reading this article carefully. I hope the article "how to send an attached email with vbs" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Email attachment address article account subject content server service success password port standby value example information interest function parameter at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi MariaDB OPPO Reno Linux