none
I need Public Function Help

    Question

  • Can someone help me understand what's wrong with this Public Function?

        Public Function EmailSender()
            Try
                Dim smtpserver As New SmtpClient()
                Dim mail As New MailMessage()
                smtpserver.Credentials = New Net.NetworkCredential("myname@gmail.com", "password")
                smtpserver.Port = 465
                smtpserver.Host = "smtp.gmail.com"
                mail = New MailMessage()
                mail.From = New MailAddress("myname@gmail.com")
                mail.To.Add("to mail id")
                mail.Subject = ("Subject Name")
                mail.Body = ("Body text")
                smtpserver.Send(mail)
                MsgBox("Mail Sent")
            Catch ex As Exception
                Close()
            End Try
        End Function

    Saturday, April 29, 2017 9:33 PM

All replies

  • Can someone help me understand what's wrong with this Public Function?

    You need to describe what the problem is.  Is there an error, or does the code not do what you expect it to do?  If there is an error then you need to show the exact error message and the line where it occurs.  If the code is not doing what you want then you need to describe what you expected to happen and what actually happened.

    Your Try/Catch handling doesn't actually do anything with the error, so it should be removed.  Then, if there is an error, you will be able to see the details.

    Saturday, April 29, 2017 9:47 PM
  • A common reason for gmail emails not working is not setting up for them in your Google settings.

    https://support.google.com/accounts/answer/6010255?hl=en

    On a side note, you should have provided the error if any and lastly catch the exception in the Try Catch rather than just do nothing but issue Close().


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 29, 2017 10:44 PM
    Moderator
  •  You will need to change the Port number in your code to 587.  You will also need to set the EnableSsl property to True.

                SmtpServer.Port = 587
                SmtpServer.EnableSsl = True
    
     

      Then sign into your Gmail account and in the "Sign-in & security" section you need to scroll to the bottom of the page and make sure you set the "Allow less secure apps" setting to On as shown below.


    If you say it can`t be done then i`ll try it

    Sunday, April 30, 2017 1:10 AM
  • Hi TimeFluid,

    I agree with Acamar. You should explain what error is, or where it occur.

    I suspect there exist some errors in your code.I've tried to rewrite:
    Public Function EmailSender()
      Try
        Dim mail As New MailMessage()
        mail.From = New MailAddress("myname@gmail.com")
        mail.To.Add("to mail id")
        mail.Subject = "Subject Name"
        mail.Body = "Body text"
        
        Dim smtpserver As New SmtpClient()
        smtpserver.Credentials = New Net.NetworkCredential("myname@gmail.com", "password")
        smtpserver.Port = 465
        smtpserver.Host = "smtp.gmail.com"
        smtpserver.Send(mail)
        MsgBox("Mail Sent")
      Catch ex As Exception
        Close()
      End Try
    End Function
    _____________
    Ashidacchi
    Sunday, April 30, 2017 3:42 AM
  • What is wrong with the function is that it does not return a value.  This method is written as if it should be a subroutine.  You should remove the MsgBox() call and have the function return True if the mail is sent, otherwise false.  Be sure to modify the function signature to return a Boolean value.

    Now, as for why it doesn't work, you should follow the suggestions the others have provided about sending email via gmail.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, April 30, 2017 1:02 PM
    Moderator
  • As Reed Kimble stated, a function is meant to return a value. When I paste your code in a sample program, I get that red squiggly line under "End Function" and a build attempt gives this:

    warning BC42105: Function 'EmailSender' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

    Monday, May 01, 2017 2:56 AM
  • Hi TimeFluid,

    Based on your description, I think that you may need return a value if you use function in the code.

    A function is more or less the same thing as a Sub - a segment of code you create yourself, and that can be used whenever you want it. The difference is that a Function returns a value, while a Sub doesn't.

    Hope it is helpful to you.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 02, 2017 8:43 AM
    Moderator