Windows Server 2003 + VB6 + SMTP; html content-type not working - email

Server: Windows 2003 r3
Program is supposed to automatically send out emails when they appear in a folder.
The issue that I am having is I can't find any support for the object that is being created.
I am trying to send a email using the text/html content-type and the emails being sent keep being received with the text/plain and the html is just normally displaying as text
The object being created is:
oMail As SMTP
I've tried
oMail.IsBodyHtml = True
And
oMail.MessageFormat = 1
All the tutorials online i've seen use
Dim oSmtp As EASendMailObjLib.Mail
Below is the entire function
Public Function sEnd(oMail As SMTP, MailToSend() As OutMail, ByVal i As Integer) As Boolean
On Error GoTo SendEmail_Err
Dim result
' Reset Smtp err code
iSmtpErr = 0
' Go thru the list of Mail to send
With MailToSend(i)
If .Status = EMO_TO_SEND Then
DoEvents
''''''''''''''''''''''''''''''''''''''
' Load Winsock
oMail.WinsockLoaded = True
' Specify Domain, Host and Mail Port
oMail.MailServer = sOutboundDomain
oMail.MailPort = iOutboundMailPort
DoEvents
'oMail.Action = a_ConnectToServer
oMail.Action = a_ResetHeaders
' oMail.IsBodyHtml = True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Specify Mail contents: Sender, Recipient, subject, body, ..
oMail.From = .SenderEmailAddr
oMail.To = .RecipientEmailAddr
oMail.SUBJECT = .SUBJECT
oMail.Date = Format(Now, "ddd, dd mmm yyyy hh:mm:ss") & Space(1) & sTimeZone
oMail.MessageText = .Body
' oMail.MessageFormat = 1
oMail.OtherHeaders = X_MAILER & Space(1) & sXMailer
' Send Message
oMail.Action = a_SendMessage
.Status = EMO_SENT
.TimeSent = Now
DoEvents
' Quit
oMail.Action = a_DisconnectFromServer
End If
End With
sEnd = True
Exit Function
SendEmail_Err:
On Error Resume Next
If iSmtpErr >= 25005 And iSmtpErr <= 26005 Or _
iSmtpErr = 20302 Or iSmtpErr = 20163 Or _
iSmtpErr = 20162 Then
' Changed to handle invalid email address error - July 20, 1999
' Or iSmtpErr = 0 Then
' Winsock/connection error
MailToSend(i).Status = EMO_NO_CONNECTION
RaiseAlert USER_CONNECTION_ERR, ""
Else
MailToSend(i).Status = EMO_FAILED_TO_SEND
End If
' Log error
Call LogError(CLng(iSmtpErr), MailToSend(i).FileName & ":" & Error, "Send")
' Put in to handle invalid email address error - July 20, 1999
oMail.Action = a_DisconnectFromServer
oMail.Action = a_Idle
sEnd = True
Exit Function
End Function
Thanks a lot for any input!

Related

hMailserver - Allow sending mail FROM:alias address and FROM:distribution address

Is it possible to make a script that allows sending email FROM:alias address including FROM:distribution address. I found a script that is only for FROM:alias address, but I didn't find a script for FROM:distribution address. The script is this:
Sub OnAcceptMessage(oClient, oMessage)
On Error Resume Next
If oClient.Username <> "" Then
If LCase(oClient.Username) <> LCase(oMessage.FromAddress) Then
Dim obBaseApp
Set obBaseApp = CreateObject("hMailServer.Application")
Call obBaseApp.Authenticate("Administrator","password") 'PUT HERE YOUR PASSWORD
StrClientDomain = Mid(oClient.Username,InStr(oClient.Username,"#") + 1)
StrFromDomain = Mid(oMessage.FromAddress,InStr(oMessage.FromAddress,"#") + 1)
Dim obDomain
Set obDomain = obBaseApp.Domains.ItemByName(StrClientDomain)
Dim obAliases
Dim obAlias
AliasFound = False
If LCase(StrClientDomain) <> LCase(StrFromDomain) Then
Set obAliases = obDomain.DomainAliases
For iAliases = 0 To (obAliases.Count - 1)
Set obAlias = obAliases.Item(iAliases)
if LCase(obAlias.AliasName) = LCase(StrFromDomain) Then
AliasFound = True
Exit For
End If
Next
If AliasFound Then
StrFromAddress = Left(oMessage.FromAddress, Len(oMessage.FromAddress) - Len(StrFromDomain)) + StrClientDomain
End If
Else
StrFromAddress = oMessage.FromAddress
AliasFound = True
End If
I found these variables for Distribution list in this code:
Sub OnAcceptMessage(oClient, oMessage)
Dim IsDistributionList : IsDistributionList = False
Dim Ogg, i, j, Recip, Dom, DomObj, DistListObj
For j = 0 to oMessage.Recipients.Count -1
Recip = oMessage.Recipients(j).OriginalAddress
Dom = (Split(Recip, "#"))(1)
Set DomObj = oApp.Domains.ItemByName(Dom)
If DomObj.DistributionLists.Count > 0 Then
For i = 0 To DomObj.DistributionLists.Count - 1
Set DistListObj = DomObj.DistributionLists.Item(i)
If Recip = DistListObj.Address Then
IsDistributionList = True
End If
Next
End If
Next
If IsDistributionList Then
Ogg = "[" & DistListObj.Address & "] "
Ogg = Ogg & oMessage.subject
oMessage.subject = Ogg
oMessage.Save
End If
End Sub

Send up arrow `↑` character to iPhone with SMS using VBA and a CDO mail object

I need to send an up arrow ↑ to an iPhone with SMS using VBA and a CDO mail object.
My attempts are as follows:
Unicode:
subj = ChrW(8593) & " Up " & ChrW(8593)
HTML:
subj = "↑ Up ↑"
Both of the above methods result in the iPhone receiving either a ? Up ? for the Unicode or ↑ Up ↑ as a string literal.
Does anyone know the correct syntax for an up arrow ↑?
Solution:
I was looking for some 'special character' syntax but the problem was not in the construction of the message. I needed to add .BodyPart.Charset = "utf-8" to the CDO.Message object and use the Unicode Chrw(8593) where I needed it.
Sub sendMSSG(sbj As String, mssg As String)
Dim cdoMail As New CDO.Message
On Error GoTo err_Report
With cdoMail
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort '2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my.smtpserverserver.net"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic '1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = sFROM
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sFROMPWD
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 6
.Update
End With
.BodyPart.Charset = "utf-8" '<~~ THIS WAS REQUIRED
.Subject = sbj
.From = sFROM
.To = sPHONEMSSGNO
.Bcc = vbNullString
.Cc = vbNullString
.TextBody = mssg
.Send
End With
Exit Sub
err_Report:
Debug.Print Err.Number & ": " & Err.Description
End Sub
Apparently, .BodyPart.Charset covers both the subject and the message body as I was able to use unicode in both.
Anyone planning to use this code for their own purposes needs to add the Microsoft CDO for Windows 2000 library to their project via Tools, References.

Issue with vbscript emailing, unable to generate text body

So I'm trying to send an e-mail with VBscripting and otherwise it works fine, but nothing I try to write to the txt or html body, appear on the message. Here is the function I'm using:
Function EMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment, mySMTPServer, mySMTPPort )
' This function sends an e-mail message using CDOSYS
'
' Arguments:
' myFrom = Sender's e-mail address ("John Doe <jdoe#mydomain.org>" or "jdoe#mydomain.org")
' myTo = Receiver's e-mail address ("John Doe <jdoe#mydomain.org>" or "jdoe#mydomain.org")
' mySubject = Message subject (optional)
' myTextBody = Actual message (text only, optional)
' myHTMLBody = Actual message (HTML, optional)
' myAttachment = Attachment as fully qualified file name, either string or array of strings (optional)
' mySMTPServer = SMTP server (IP address or host name)
' mySMTPPort = SMTP server port (optional, default 25)
'
' Returns:
' status message
'
' Standard housekeeping
Dim i, objEmail
' Use custom error handling
On Error Resume Next
' Create an e-mail message object
Set objEmail = CreateObject( "CDO.Message" )
' Fill in the field values
With objEmail
.From = myFrom
.To = myTo
' Other options you might want to add:
' .Cc = ...
' .Bcc = ...
.Subject = mySubject
.TextBody = myTextBody
.HTMLBody = myHTMLBody
If IsArray( myAttachment ) Then
For i = 0 To UBound( myAttachment )
.AddAttachment Replace( myAttachment( i ), "\", "\\" ),"",""
Next
ElseIf myAttachment <> "" Then
.AddAttachment Replace( myAttachment, "\", "\\" ),"",""
End If
If mySMTPPort = "" Then
mySMTPPort = 25
End If
With .Configuration.Fields
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = mySMTPServer
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = mySMTPPort
.Update
End With
End With
' Return status message
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
objEmail.send
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = "ok"
End If
End If
' Release the e-mail message object
Set objEmail = Nothing
' Restore default error handling
On Error Goto 0
End Function
And here is how I'm calling it:
MsgBox Email ( "address#sender.com", _
address#receiver.com, _
"This subject appears on the email correctly", _
"But this txt body does not", _
"", _
"", _
"mail.smtp-server.com", _
25 )
Like I said, the e-mail goes through alright, attachments work, subject works but for whatever reason the body never appears in the message! What is the problem?
For text use TextBody, for HTML use HTMLBody don't set both, currently you set HTMLBody to "" which will override TextBody.
You could use a flag argument;
Function EMail( myFrom, myTo, mySubject, myBody, isHTML, myAttachment...
Then
if isHTML then
.HTMLBody = myBody
else
.TextBody = myBody
end if

Intermittent "The transport failed to connect to the server" CDO error

I am writing an application that sends emails to an admin when there is a problem with the data. The account it's sending through is a Network Solutions SMTP server.
The code works most of the time, but about 1 out of 10 sends fail with the error -2147220973 "The transport failed to connect to the server".
Any suggestions on how to handle this?
Set imsg = CreateObject("cdo.message")
Set iconf = CreateObject("cdo.configuration")
Set Flds = iconf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.OurCompany.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 2525
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "me#OurCompany.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
.Update
End With
With imsg
Set .Configuration = iconf
.To = "me#MyEmail.com" 'CMemail
.From = "resupport#OurCompanycom"
.Subject = ACT
.HTMLBody = "Notification for " & CTName & " of " & CTCname & " " & ACT & ". You must manually Notify them about new docs for " & pname & ". " _
& "<br>Tell " & CTName & " to '" & Nz(DLookup("Notify", "TBLINVOICESETTINGS"), "") & " " & PRName & "_" & pname & ".pdf'"
.Send
End With
Set imsg = Nothing
Set iconf = Nothing
Should the smtpserverport be 25, is it being blocked by firewall?
This piece of code executes correctly :
Sub SMail(pTO As String, pSB As String, pBO As String, pAT As String)
On Error GoTo ErrH: Dim mm As CDO.Message: Set mm = New CDO.Message
mm.Configuration.Fields(cdoSMTPUseSSL) = "True"
mm.Configuration.Fields(cdoSendUsingMethod) = 2
mm.Configuration.Fields(cdoSMTPAuthenticate) = 1
mm.Configuration.Fields(cdoSMTPServer) = "smtp.gmail.com"
mm.Configuration.Fields(cdoSendUserName) = "MyID"
mm.Configuration.Fields(cdoSendPassword) = "MyPW"
'mm.Configuration.Fields(cdoSMTPConnectionTimeout) = 20
'mm.Configuration.Fields(cdoSMTPServerPort) = 25
mm.Configuration.Fields.Update
mm.From = "MyID"
mm.To = pTO
mm.Subject = pSB
mm.TextBody = pBO
mm.AddAttachment (pAT)
mm.send
ErrH: If Err Then MsgBox (Err.Number & " : " & Err.Description)
Set mm = Nothing
End Sub

VBScript SMTP Auto Email

I have a script to auto email a list of address' stored in Excel, but it is only sending to the first address and not looping to the rest, I cannot seem to fix it:
Set objMessage = CreateObject("CDO.Message")
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
email = sh.Range("A" & row)
LastRow = sh.UsedRange.Rows.Count
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f
Set f = fso.OpenTextFile("Y:\Billing_Common\autoemail\Script\Email.txt", ForReading)
BodyText = f.ReadAll
For r = row to LastRow
If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then
objMessage.Subject = "Billing: Meter Read"
objMessage.From = "billing#energia.ie"
row = row + 1
objMessage.To = email
objMessage.TextBody = BodyText
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SERVER ADDRESS HERE"
'Server port
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
End if
Next
f.Close
Set f = Nothing
Set fso = Nothing
wb.Close
End If
Next
Any help would be much appreciated guys!
Thanks!
row = 2
email = sh.Range("A" & row)
...
For r = row to LastRow
...
objMessage.To = email
...
Next
You set email to the value of the cell "A2" and never change it. If you want to send a mail to multiple recipients, you should make that
objMessage.To = sh.Range("A" & r).Value
or (better) build a recipient list (assuming that your used range starts with headers in the first table row):
ReDim recipients(LastRow - row)
For r = row To LastRow
recipients(r - row) = sh.Range("A" & r).Value
Next
objMessage.To = Join(recipients, ";")
and send the message just once. The MTA will handle the rest.
Side note: as Vishnu Prasad Kallummel pointed out in the comments your code doesn't close the Excel instance it started. Unlike other objects created in VBScript, Office applications won't automatically terminate with the script, so you have to handle it yourself:
...
wb.Close
app.Quit