I have set this up to auto email through the Outlook client, is it possible to change this code to work directly through an SMTP server? And could anyone possibly help me do it?
Any help would be much appreciated, thanks!
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files
If LCase(fso.GetExtensionName(f)) = "xls" Then
Set wb = app.Workbooks.Open(f.Path)
set sh = wb.Sheets("Auto Email Script")
row = 2
name = "Customer"
email = sh.Range("A" & row)
subject = "Billing"
the = "the"
LastRow = sh.UsedRange.Rows.Count
For r = row to LastRow
If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then
SendMessage email, name, subject, TRUE, _
NULL, "Y:\Billing_Common\autoemail\Script\energia-logo.gif", 143,393
row = row + 1
email = sh.Range("A" & row)
End if
Next
wb.Close
End If
Next
Sub SendMessage(EmailAddress, DisplayName, Subject, DisplayMsg, AttachmentPath, ImagePath, ImageHeight, ImageWidth)
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
template = FindTemplate()
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(EmailAddress)
objOutlookRecip.resolve
objOutlookRecip.Type = 1
' Set the Subject, Body, and Importance of the message.
.Subject = Subject
.bodyformat = 3
.Importance = 2 'High importance
body = Replace(template, "{First}", name)
body = Replace(body, "{the}", the)
if not isNull(ImagePath) then
if not ImagePath = "" then
.Attachments.add ImagePath
image = split(ImagePath,"\")(ubound(split(ImagePath,"\")))
body = Replace(body, "{image}", "<img src='cid:" & image & _
"'" & " height=" & ImageHeight &" width=" & ImageWidth & ">")
end if
else
body = Replace(body, "{image}", "")
end if
if not isNull(AttachMentPath) then
.Attachments.add AttachmentPath
end if
.HTMLBody = body
.Save
.Send
End With
Set objOutlook = Nothing
End Sub
Function FindTemplate()
Set OL = GetObject("", "Outlook.Application")
set Drafts = OL.GetNamespace("MAPI").GetDefaultFolder(16)
Set oItems = Drafts.Items
For Each Draft In oItems
If Draft.subject = "Template" Then
FindTemplate = Draft.HTMLBody
Exit Function
End If
Next
End Function
If you want to send mail directly to an SMTP server, there's no need to go through Outlook in the first place. Just use CDO. Something like this:
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set msg = CreateObject("CDO.Message")
msg.Subject = "Test"
msg.From = "sender#example.com"
msg.To = "recipient#example.org"
msg.TextBody = "This is some sample message text."
With msg.Configuration.Fields
.Item(schema & "sendusing") = 2
.Item(schema & "smtpserver") = "smtp.intern.example.com"
.Item(schema & "smtpserverport") = 25
.Update
End With
msg.Send
Related
I am trying to send mail in a particular time with task scheduler on windows.
The output I have in body is not what I expect and does not give a newline.
Is there any way to control the format of the output?
<%#LANGUAGE="VBSCRIPT" CODEPAGE="CP65001"%>
Const MSG_RECIPIENT_LIST = "foor#bar.com"
Const MSG_SUBJECT = "力"
Const MSG_BODY = "これをやろう" + vbNewLine + "我々は一緒に大きなものを達成する"
Dim olkApp, olkSes, olkMsg
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkMsg = olkApp.CreateItem(0)
With olkMsg
.To = MSG_RECIPIENT_LIST
.Subject = MSG_SUBJECT
.HTMLBody = MSG_BODY
.Send
End With
olkSes.Logoff
Set olkMsg = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
im using vbscript CDO for mail sending but i want to attachment as a body of the email can you please suggest me.
strSMTPFrom = "kampati.vinay#testing.in"
strSMTPTo = "kampati.vinay#testing.in"
strSMTPRelay = "testing.in"
strTextBody = "MDaemon Q status"
strSubject = "MDaemon Q status"
strAttachment = "C:\MDaemon\output.txt"
Set oMessage = CreateObject("CDO.Message")
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update
oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.TextBody = strTextBody
oMessage.AddAttachment strAttachment
oMessage.Send
you can use create email body as HTML and
like
.............
body
.......
<div><img src="linkOfImage" height="50px" width="50px"/></div>
......
...............
for this you have to set one bit in your email header.
Example:
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)
' The mailman object is used for sending and receiving email.
set mailman = CreateObject("Chilkat_9_5_0.MailMan")
' Any string argument automatically begins the 30-day trial.
success = mailman.UnlockComponent("30-day trial")
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
WScript.Quit
End If
' Set the SMTP server.
mailman.SmtpHost = "smtp.comcast.net"
' Create a new email object
set email = CreateObject("Chilkat_9_5_0.Email")
' Add an embedded image to the HTML email.
fileOnDisk = "images/dude2.gif"
filePathInHtml = "dudeAbc.gif"
' Embed the GIF image in the email.
success = email.AddRelatedFile2(fileOnDisk,filePathInHtml)
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
WScript.Quit
End If
' The src attribute for the image tag is set to the filePathInHtml:
htmlBody = "<html><body>Embedded Image:<br><img src=""dudeAbc.gif""></body></html>"
' Set the basic email stuff: HTML body, subject, "from", "to";
email.SetHtmlBody htmlBody
email.Subject = "VBScript HTML email with an embedded image."
success = email.AddTo("Admin","admin#chilkatsoft.com")
email.From = "Chilkat Support <support#chilkatsoft.com>"
success = mailman.SendEmail(email)
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
Else
outFile.WriteLine("Mail Sent!")
End If
outFile.Close
set cdoConfig = CreateObject("CDO.Configuration")
with cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "localhost"
.Item(cdoSMTPAuthenticate) = 1
.Item(cdoSendUsername) = "contact#example.com"
.Item(cdoSendPassword) = "password"
.Update
end with
set cdoMessage = CreateObject("CDO.Message")
with cdomessage
set .Configuration = cdoConfig
.From = "contact#example.com"
.To = email
.Subject = subject
.HTMLBody = message
.AddAttachment "c:/i/report.pdf"
.Send
end with
set cdomessage = nothing
set cdoconfig = nothing
Everything sends find, but the recipient gets the message as "Untitled Attachment 000X.pdf"
How do I give the attachment a name?
I have something like this. But never tried.
'With cdomessage
.AddAttachment "c:/i/report.pdf"
.Attachments(1).Fields.Item("urn:schemas:mailheader:content-disposition") ="attachment;filename=" & NEWNAME
.Attachments(1).Fields.Update
'End With
My project is to send fields before Visual Studio to Outlook Body in fields entered in the body after the reception of messages the user can modify the data sent in text fields on Outlook and sends the update data it will be saved in database that's possible to update data from outlook to Database ?
I have build a solution like that, I use it daily to read and file mail as part of my daily workflow. But as others have hinted you need to be specific about what you need help with.
If (TypeOf olItem Is Outlook.MailItem) Then
Dim olMailItem As Outlook.MailItem = TryCast(olItem, Outlook.MailItem)
If olMailItem.Permission = Outlook.OlPermission.olUnrestricted Then
strBody = olMailItem.HTMLBody 'file:///C:/AttSave/header.png
Else
strBody = "Rights Protected fix if I'm not in debugger"
End If
For Each olAttach In olMailItem.Attachments
If olAttach.Type <> Outlook.OlAttachmentType.olOLE Then
olAttach.SaveAsFile("c:\AttSave\" & olAttach.FileName)
'strBody = strBody.Replace("cid:header", "file:///C:/AttSave/header.png")
strCID = olAttach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E")
If strCID <> "" Then
strBody = strBody.Replace("cid:" & strCID, "file:///C:/AttSave/" & System.Web.HttpUtility.UrlEncode(olAttach.FileName))
End If
lst_Attach.Items.Add(olAttach.FileName)
Else
MsgBox("open this one via outlook")
End If
Next
Me.webBody.DocumentText = strBody
Me.txtSubject.Text = olMailItem.Subject
If olMailItem.Importance = Outlook.OlImportance.olImportanceHigh Then
Me.txtSubject.ForeColor = Color.Red
Else
Me.txtSubject.ForeColor = Color.White
End If
'Dim palSender As Microsoft.Office.Interop.Outlook.AddressEntry
'palSender = mailItem.Sender
Me.txtSentDate.Text = olMailItem.SentOn
'Me.txtTo.Text = olMailItem.To
olSenderA = olMailItem.Sender
If IsNothing(olSenderA) = False Then
'Dim olConItem As Outlook.ContactItem
'olConItem = olSenderA.GetContact()
'If Not IsNothing(olConItem) Then
' If olConItem.HasPicture = True Then
' Stop
' End If
'End If
Pa = olSenderA.PropertyAccessor
'Debug.Print(olRe.Name & "stp= " & Pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E"))
Try
Me.txtFrom.Text = Me.txtFrom.Text & olSenderA.Name & " (" & Pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E") & ")"
Catch ex As System.Runtime.InteropServices.COMException
'Stop
Me.txtFrom.Text = olMailItem.SenderName & " (" & olMailItem.SenderEmailAddress & ")"
Catch ex As Exception
Stop
End Try
I want to write a VBS macro to auto generate an email in outlook and attach a word document. I currently have a macro that does this for excel, but I can't get it to work for Word. I can't figure out for the life of me what my "FName= " should be. Any suggestions or help would be greatly appreciated. Here is what I have:
Sub AutoEmail()
On Error GoTo Cancel
Dim Resp As Integer
Resp = MsgBox(prompt:=vbCr & "Yes = Review Email" & vbCr & "No = Immediately Send" & vbCr & "Cancel = Cancel" & vbCr, _
Title:="Review email before sending?", _
Buttons:=3 + 32)
Select Case Resp
'Yes was clicked, user wants to review email first
Case Is = 6
Dim myOutlook As Object
Dim myMailItem As Object
Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItem(olMailItem)
FName = ActiveWord & "\" & ActiveWord.Name
With otlNewMail
.To = ""
.CC = ""
.Subject = ""
.Body = "Good Morning," & vbCr & vbCr & "" & Format(Date, "MM/DD") & "."
.Attachments.Add FName
.Display
End With
Set otlNewMail = Nothing
Set otlApp = Nothing
Set otlAttach = Nothing
Set otlMess = Nothing
Set otlNSpace = Nothing
'If no is clicked
Case Is = 7
Dim myOutlok As Object
Dim myMailItm As Object
Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItem(olMailItem)
FName = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
With otlNewMail
.To = ""
.CC = ""
.Subject = ""
.Body = "Good Morning," & vbCr & vbCr & " " & Format(Date, "MM/DD") & "."
.Attachments.Add FName
.Send
'.Display
'Application.Wait (Now + TimeValue("0:00:01"))
'Application.SendKeys "%s"
End With
'otlApp.Quit
Set otlNewMail = Nothing
Set otlApp = Nothing
Set otlAttach = Nothing
Set otlMess = Nothing
Set otlNSpace = Nothing
'If Cancel is clicked
Case Is = 2
Cancel:
MsgBox prompt:="No Email has been sent.", _
Title:="EMAIL CANCELLED", _
Buttons:=64
End Select
End Sub
May it is a bit late, but I want to solve it for future use.
You want to have the active document as your file name (FName).
FName = Application.ActiveDocument.Path + "\" + Application.ActiveDocument.Name
' .Path returns only the Path where the file is saved without the file name like "C:\Test"
' .Name returns only the Name of the file, including the current type like "example.doc"
' Backslash is needed because of the missing backslash from .Path
otlNewMail.Attachements.Add FName
May you also want to save your current document before sending it via outlook, otherwise you will send the document without the changes made.
Function SaveDoc()
ActiveDocument.Save
End Function
I hope that this will help others, because the code from the question helped me a lot while scripting a similar script.