我有以下代码,我正在尝试从我的Windows应用程序发送电子邮件,但它不起作用...任何帮助?请注意,我正在使用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
答案 0 :(得分:0)
根据我的经验,通过.net(和之前的vb6)发送电子邮件很奇怪。有时它应该工作而不是,有时是因为.net错误,有时与smtp服务器不兼容。下面是一些适用于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