Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Send email via Yahoo mail

I want to be able to send an attachment, which is a report, as a PDF using Yahoo mail.  I need to include the sender's email address (hard coded), the To email address (hard coded), the BCC email address (hard coded), the Subject line (hard coded but with the addition of the date on the report AND the location which is on the report), and the body copy (as html copy), and don't know where to begin.
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Forgot to mention...  via a command button click
Avatar of David Johnson, CD
if you are a yahoo premium member you can send using smtp provider i.e. using powershell or vb script.  a normal yahoo account is not sufficient.
Avatar of Bill Prew
Bill Prew

I was able to do this from my personal email account.  I have read that yahoo limits the amount of email that you can send this way to low volumes, but if the author only needs to do this occasionally I can post some code...


»bp
I'd like to see the code if you could provide it.
Here's a working test, just add in the yahoo account id and password where needed...

Option Explicit

Sub Test()
    Dim Attachment As String
    Dim MsgBody As String
    
    Attachment = "C:\Temp\test.txt"
    MsgBody = "<HTML><BODY><P>Hello world.<P></BODY></HTML>"
    SendEmail "from_email@yahoo.com", "to_email@anyplace.net", "", "Test email subject", MsgBody, Attachment
End Sub


Sub SendEmail(FromEmail As String, ToEmail As String, BccEmail As String, Subject As String, Body As String, Attachment As String)
    Const SmtpUser = "yahoo_account_id"     '<===== MAKE CHANGES HERE
    Const SmtpPassword = "yahoo_account_password"     '<===== MAKE CHANGES HERE
    Const SmtpServer = "smtp.mail.yahoo.com"
    Const SmtpPort = 465

    ' CDO Constants needed to send email
    Const CdoSendUsingPickup = 1   'Send message using the local SMTP service pickup directory.
    Const CdoSendUsingPort = 2     'Send the message using the network (SMTP over the network).
    Const CdoAnonymous = 0         'Do not authenticate
    Const CdoBasic = 1             'basic (clear-text) authentication
    Const CdoNTLM = 2              'NTLM
    Const CdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
    Const CdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
    Const CdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
    Const CdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
    Const CdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
    Const CdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
    Const CdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
    Const CdoSmtpUseSsl = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
    
    ' Local variables
    Dim ObjConfig As Object
    Dim ObjMessage As Object

    ' Get a handle to the config object and it's fields
    Set ObjConfig = CreateObject("CDO.Configuration")

    ' Set config fields we care about
    With ObjConfig.Fields
        .Item(CdoSendUsingMethod) = CdoSendUsingPort
        .Item(CdoSMTPServer) = SmtpServer
        .Item(CdoSMTPServerPort) = SmtpPort
        .Item(CdoSMTPConnectionTimeout) = 60
        .Item(CdoSMTPAuthenticate) = CdoBasic
        .Item(CdoSendUserName) = SmtpUser
        .Item(CdoSendPassword) = SmtpPassword
        .Item(CdoSmtpUseSsl) = True
        .Update
    End With

    ' Create a new message
    Set ObjMessage = CreateObject("CDO.Message")
    Set ObjMessage.Configuration = ObjConfig

    ' Populate message fields and send it
    With ObjMessage
        .To = ToEmail
        .From = FromEmail
        .BCC = BccEmail
        .Subject = Subject
        .AutoGenerateTextBody = True
        .HtmlBody = Body
        If Attachment <> "" Then
            .AddAttachment Attachment
        End If
        .Send
    End With
End Sub

Open in new window


»bp
# powershell script
$From = "YourEmail@yahoo.com"
$To = "AnotherEmail@YourDomain.com"
$Cc = "YourBoss@YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "465"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment

Open in new window

Neither of these solutions work anymore with Yahoo Mail.
Even if you go to the security page and turn "Allow access for less secure apps" on, an smtp connection is rejected. For Gmail, that is usually the trick.

And Yahoo Premium Mail account doesn't exist anymore.

You may have to obtain an account at Office 365 (Exchange Online) or Gmail. Look at the main Yahoo site - it is so much a consumer "thing" powered by heavy advertising. It is not intended for a pro/business service like distributing reports.
Gustav, I'm sure you are correct but since Bill said he tried it I thought I'd try it out.

Bill:  I don't know how to use your code or where to put it.
Okay, it sounded like you were an Access developer, is that not true?  Do you have no experience with VBA in Access?


»bp
It may - or most likely did - work, but it refuses to work with my account, and similar code I have working for both Microsoft and Gmail accounts.

Bill's code example is as close to a copy-paste you can possibly get. He even marked where to adjust to use your account and password.
If you don't have any experience in VBA with Access, then perhaps something like this would be worth a read and some experimenting...

How to Create Visual Basic for Applications (VBA) in Excel with Examples


»bp
Bill, I do have experience but could not make it work.
Did you get errors?  Did you adjust the account info?

You should have been able to post that code into a module and run the Test() procedure as a test.


»bp
Ok.  I think I'm getting close.  I'm sending to a gmail account using Access VBA code.  It works except I can't figure out how to add the attachment.  Again, any help much appreciated.  As you can see I have any code related to the report attachment commented out because it fails.

Private Sub cmdSend_Click()
On Error GoTo Err_cmdSend_Click

    Dim strFilter As String
    Dim strFileName As String
    Dim strReportName As String
    Dim strMailto As String
    Dim strMailFrom As String
    Dim strSubject As String
    Dim strCC As String
    Dim strBCC As String
    Dim msg As Object
    
    
'    strReportName = "Daily Schedule"
'    strFileName = Environ("Temp") & "\" & strReportName & " # " & "Report" & " - " & Me.txtDate & " - " & Me.txtLocation & ".PDF"
   
    
    Set msg = CreateObject("CDO.Message")
    msg.From = "name@gmail.com"
    msg.To = "name@gmail.com"
    msg.Subject = "Report" & " - " & Me.txtDate & " - " & Me.txtLocation
    msg.TextBody = "Body copy goes here."
'    msg.Cc = "name@somewhere.com"
    msg.bcc = "name@somewhere.com"
'    msg.AddAttachment = strFileName

    msg.configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    msg.configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    msg.configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    msg.configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "stevel5231@gmail.com"
    msg.configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "gtrplr13"
    msg.configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    msg.configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

    msg.configuration.fields.Update
    msg.Send

'    DoCmd.Close acReport, strReportName, acSaveNo

    MsgBox "Report sent."
    
'    Kill strFileName

Exit_cmdSend_Click:
    Exit Sub

Err_cmdSend_Click:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_cmdSend_Click
    
End Sub

Open in new window

Your date value probably introduces invalid filename characters.
Try - and do check the result (Debug):

strFileName = Environ("Temp") & "\" & strReportName & " # " & "Report" & " - " & Format(Me.txtDate, "yyyy-mm-dd") & " - " & Me.txtLocation & ".PDF"
Debug.Print strFileName

Open in new window

Attachment paths have to be the full path to the file I believe, but it seems like you have that covered.  As Gustav mentions, debug and display the filename and check for it's validity and bad characters, etc...


»bp
The debug.print shows exactly what I want.  But when the code gets to:  (after I un-comment it)

msg.AddAttachment = strFileName

It errors out on me.
What's the error?  Does that file exist?  What is the full path?


»bp
Then the filename either is invalid or doesn't exist.
Try attaching a simple name you positively know exists, like "c:\test\somefile.pdf"
Bill:  Here's the full path:
strFileName = Environ("Temp") & "\" & strReportName & " - " & "Report" & " - " & Format(Me.txtDate, "yyyy-mm-dd") & " - " & Me.txtLocation & ".PDF"

Gustav:  I tried using a report I know exists on my desktop using:
msg.addattachment "C:\Users\Steve\Desktop\Daily Schedule.pdf"
And it worked fine.  Its like the file isn't being created.
Well, that's how the path is created, I'd be more interested in what it displayed at during a debug at runtime.

Naturally if the file doesn't exist then attaching it will throw an error...


»bp
Where do you create that file?


»bp
I guess I don't know.  Evidently I'm not.
I didn't see it being created anyplace in that code you posted...


»bp
Stupid question.  Do I need to add code to create the report pdf file?
And if so, what would the code look like?
I suspect you may need a "DoCmd.OutputTo" command someplace, assuming you wanted a report in that file.


»bp
Do study the output from:

Debug.Print strFileName

Open in new window

and correct the path or other errors.
A starting point would be (but I haven't worked a lot in Access lately so may be rusty)...

DoCmd.OutputTo objecttype:=acOutputReport, objectName:=strReportName, outputformat:=acFormatPDF, outputFile:=strFileName

Open in new window


»bp
Bill.  I added that and then I get a message indicating the output to actions was cancelled.
Check if the output path/ filename is legit...those pesky special characters are always here to cause issues..
Here is what I get...

C:\Users\Steve\AppData\Local\Temp\Report - Show/No Show Report - 10/24/2018 - Location.PDF
I keep getting...  The OutputTo action was canceled
Loose the slashes
As I wrote earlier above, you have invalid characters for filenames - forward slashes:

Show/No Show Report - 10/24/2018

Open in new window

Replace these with, say, a dash: -
Assuming you mean here... Show/No Show Report

I removed the "/" and it still failed.
replace all the slashes
Also in the date - see above how to use Format for this.
Nothing works.  Here is my entire code:

Private Sub cmdSend_Click()
On Error GoTo Err_cmdSend_Click

    Dim objOutlook As Object
    Dim objOutlookEmail As Object
    Dim rstEMail As DAO.Recordset
    Dim strEmail As String
    Dim strEmailBody As String
    Dim strEmailCC As String
    Dim strEmailBCC As String
    Dim strEmailSubject As String
    Dim strEmailTo As String
    Dim strFileName As String
    Dim strFilter As String
    Dim strReportName As String
    
    Set objOutlook = GetOutlookObject()
    Set objOutlookEmail = objOutlook.CreateItem(olMailItem)
    
    strEmailBody = ""
    strEmailBCC = "somebody@somewhere.com"
    strEmailSubject = "Report" & " - " & Me.txtAppointmentDate & " - " & Me.txtLocation
    strEmailTo = "somebody@somewhere.com"
    strReportName = "Schedule"

    strFileName = Environ("Temp") & "\" & strReportName & " - " & "Report" & " - " & Format(Me.txtAppointmentDate, "yyyy-mm-dd") & " - " & Me.txtLocation & ".PDF"

    Debug.Print strFileName
 
    
'    DoCmd.OpenReport strReportName, acViewPreview, , strFilter, acHidden
    
    DoCmd.OpenReport strReportName, acViewPreview, , , acHidden
    
    
    DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strFileName
    DoCmd.Close acReport, strReportName, acSaveNo
    
    With objOutlookEmail
        .To = strEmailTo
        .bcc = strEmailBCC
        .Subject = strEmailSubject
        .Attachments.Add strFileName
        .HTMLBody = strEmailBody
        .Display
    End With

    Set objOutlookEmail = Nothing
    Set objOutlook = Nothing

    Kill strFileName

Exit_cmdSend_Click:
    Exit Sub

Err_cmdSend_Click:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_cmdSend_Click
    
End Sub

Open in new window

And...

Public Function GetOutlookObject()
On Error GoTo Err_GetOutlookObject

    Dim objOutlook As Object
    Set objOutlook = GetObject(, "Outlook.Application")

    Set GetOutlookObject = objOutlook
    Set objOutlook = Nothing

Exit_GetOutlookObject:
    Exit Function

Err_GetOutlookObject:
    Select Case Err
        Case 429
            Set objOutlook = CreateObject("Outlook.Application")
            Resume Next
        Case Else
            MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
            Resume Exit_GetOutlookObject
    End Select

End Function

Open in new window

So what does the filename look like now?
Now you are using Outlook. This is very different from CDO - and very confusing to us.
C:\Users\Steve\AppData\Local\Schedule - Report - 2018-10-24 - Location.PDF
Sorry, I switched gears.  Gave up on the Yahoo and GMail method.
I don't use Outlook for sending mails this way, sorry.
Nothing works.
Need more to go on than this.  What error(s) are you getting, what have you tried in attempting to debug this, what did your test environment and data look like, etc...


»bp
I have decided to award point to each of you and drop the topic.  Thanks to everyone that tried to help.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.