简体   繁体   中英

Send email from windows app using vb.net

i have the following code,i'm trying to send an email from my windows application but it's not working... any help ? note that i'm using vb.net and i'm not getting any errors.. i'm just not receiving any emails !

 Private Sub senemail()
    'create the mail message
    Dim mail As New MailMessage()

    'set the addresses
    mail.From = New MailAddress("jocelyne_elkhoury@inmobiles.net")
    mail.To.Add("jocelyne_el_khoury@hotmail.co.uk")

    'set the content
    mail.Subject = "This is an email"
    mail.Body = "this is a sample body"

    'send the message
    Dim smtp As New SmtpClient("127.0.0.1")
    smtp.Send(mail)
End Sub

In my experience, sending email via .net (and vb6 before it) is weird. Sometimes it should work and just doesn't, sometimes because of .net bugs and sometimes incompatibility with the smtp server. Here is some code that works on VB 2008, and it should work with 2010.

Using msg As New MailMessage(New MailAddress(fromAddress, fromName), New MailAddress(toAddress))
  Try
    Dim mailer As New SmtpClient
    msg.BodyEncoding = System.Text.Encoding.Default
    msg.Subject = subject
    msg.Body = body
    msg.IsBodyHtml = False

    mailer.Host = mailserver
    mailer.Credentials = New System.Net.NetworkCredential(username, password) ' may or may not be necessary, depending on the server
    mailer.Send(msg)
  Catch ex As Exception
    Return ex.Message
  End Try
End Using ' mailmsg
  1. If this doesn't work, try using localhost instead of 127.0.0.1 for the mailserver.
  2. If that doesn't work, try using your system name (the one that shows up on the network) for the mailserver.
  3. If that doesn't work, try using an external smtp server with your own username and password (just for testing).
  4. profit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM