简体   繁体   English

使用vb.net从Windows应用发送电子邮件

[英]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 ? 我有以下代码,我正在尝试从Windows应用程序发送电子邮件,但无法正常工作……有什么帮助吗? note that i'm using vb.net and i'm not getting any errors.. i'm just not receiving any emails ! 请注意,我正在使用vb.net,并且没有收到任何错误。.我只是没有收到任何电子邮件!

 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. 以我的经验,通过.net(以及之前的vb6)发送电子邮件很奇怪。 Sometimes it should work and just doesn't, sometimes because of .net bugs and sometimes incompatibility with the smtp server. 有时它应该工作而不能,有时是因为.net错误,有时是与smtp服务器不兼容。 Here is some code that works on VB 2008, and it should work with 2010. 这是一些适用于VB 2008的代码,它应该适用于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. 如果这不起作用,请尝试对邮件服务器使用localhost而不是127.0.0.1。
  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). 如果这不起作用,请尝试使用具有您自己的用户名和密码的外部smtp服务器(仅用于测试)。
  4. profit. 利润。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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