Use vbs to send emails with attachments

Source: Internet
Author: User

Function send_mail (you_account, you_password, send_email, send_email2, send_topic, send_body, send_attachment)
'Code by netpatch
'Vbs mail sending parameter descriptions
'You _ account': Your email account
'You _ password: Your Email Password
'Send _ Email: main email address
'Send _ email2: alternate email address
'Send _ TOPIC: Email Subject
'Send _ body: Mail content
'Send _ attachment: email attachment

You_id = Split (you_account, "@",-1, vbtextcompare)
'Account and server Separation
Ms_space = "http://schemas.microsoft.com/cdo/configuration"
'This is a must, but you can rest assured that you will not send emails through Microsoft.
Set email = Createobject ("CDO. Message ")
Email. From = you_account
'This must be the same as the email sending account.
Email. To = send_email 'main email address

If send_email2 <> "then
Email. Cc = send_email2 'backup email address
End if

Email. Subject = send_topic 'subject
Email. textbody = send_body 'email content

If send_attachment <> "" then
Email. addattachment send_attachment 'email attachment
End if

With email. configuration. Fields
. Item (ms_space & "sendusing") = 2' mail 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
Email. Send
'Send email
Set email = nothing
'Close the component

Send_mail = true
'If no error message exists, the message is sent successfully. Otherwise, the message fails to be sent.
If err then
Err. Clear
Send_mail = false
End if
End Function

below is an example of sending an email with an attachment using the above function
If send_mail ("test@163.com", "test", "test2@163.com ","", "Email Subject", "email content", "d: \ test.exe") = true then
wscript. echo "sent successfully"
else
wscript. echo "failed to send"
end if

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.