locked
VB - Problem sending email RRS feed

  • Question

  • I'm making a small application where the user fills in an email address and some other info in a form, and hits OK-button, and the data is sent in an email. I use Gmail's SMTP server. It works fine, but there is one small problem. The user also fills in the from-address (user's email address). But when I receive the email, the sender is 'myemail@gmail.com', and not what the user typed in ('SenderAddress').
    Am I doing something wrong, or is Gmail overriding this data, and use my login username as sender?

    Below is the code I use.

     

    Try
        Dim MyMailMessage As New MailMessage()
        MyMailMessage.From = New MailAddress(SenderAddress)
        MyMailMessage.To.Add(EmailAddress.Text)
        MyMailMessage.Subject = Subjekt
        MyMailMessage.Body = MessageBody
        Dim SMPT As New SmtpClient("smtp.gmail.com")
        SMPT.Port = 587
        SMPT.EnableSsl = True
        SMPT.Credentials = New System.Net.NetworkCredential("myemail@gmail.com", "mypassword")
        SMPT.Send(MyMailMessage)
    Catch ex As Exception
    End Try

     

    Thanks for any suggestions!

    Sunday, September 11, 2011 2:14 PM

Answers

  • Your code looks pretty typical for the most part.

    I would venture to guess that google isn't going to let you spoof the sender address...  note that the recipients may have this email get caught by their spam filters if the google server did transmit the message unmolested.

    It is almost always best to just let the user enter the SMTP server & credentials to use.  That way if they access the Internet through different ISPs, they can just change the SMTP server address to whatever is valid for their connection.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    • Marked as answer by Blipblop Sunday, September 11, 2011 6:57 PM
    Sunday, September 11, 2011 3:36 PM
    Moderator
  • the mistake lies in this line :     SMPT.Credentials = New System.Net.NetworkCredential("myemail@gmail.com", "mypassword")
    that's the sender.

    ' so if you want the user to be able to enter their own details
    
    dim myEmail as string
    dim myPassword as string
    myEmail = txtUser.text
    myPassword = txtPass.text
    
    SMPT.Credentials = New System.Net.NetworkCredential(myEmail, myPassword)
    
    ' this way the user can use his own account to send mails instead of your preset
    

    Regards,

    Dylan Meeus


    0x2B |~ 0x2B Blog : www.it-ca.net/blogdylan
    • Proposed as answer by Michel K Sunday, September 11, 2011 6:08 PM
    • Marked as answer by Blipblop Sunday, September 11, 2011 6:56 PM
    Sunday, September 11, 2011 6:05 PM

All replies

  • Your code looks pretty typical for the most part.

    I would venture to guess that google isn't going to let you spoof the sender address...  note that the recipients may have this email get caught by their spam filters if the google server did transmit the message unmolested.

    It is almost always best to just let the user enter the SMTP server & credentials to use.  That way if they access the Internet through different ISPs, they can just change the SMTP server address to whatever is valid for their connection.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    • Marked as answer by Blipblop Sunday, September 11, 2011 6:57 PM
    Sunday, September 11, 2011 3:36 PM
    Moderator
  • the mistake lies in this line :     SMPT.Credentials = New System.Net.NetworkCredential("myemail@gmail.com", "mypassword")
    that's the sender.

    ' so if you want the user to be able to enter their own details
    
    dim myEmail as string
    dim myPassword as string
    myEmail = txtUser.text
    myPassword = txtPass.text
    
    SMPT.Credentials = New System.Net.NetworkCredential(myEmail, myPassword)
    
    ' this way the user can use his own account to send mails instead of your preset
    

    Regards,

    Dylan Meeus


    0x2B |~ 0x2B Blog : www.it-ca.net/blogdylan
    • Proposed as answer by Michel K Sunday, September 11, 2011 6:08 PM
    • Marked as answer by Blipblop Sunday, September 11, 2011 6:56 PM
    Sunday, September 11, 2011 6:05 PM
  • I guess you are right about that.

    Thanks!

    Sunday, September 11, 2011 6:56 PM