无法从vb.net中编写的Webservice发送电子邮件

时间:2015-07-28 14:10:50

标签: vb.net email

我想从我的Web服务发送一封电子邮件,用vb.net编写,但我得到了一个Timeout。 有什么问题?

                    Dim toaddress As MailAddress = New MailAddress("xxx")
                    Dim fromaddress As MailAddress = New MailAddress("yyy")
                    ' The structure for MailMessage(from, to)
                    Dim message As MailMessage = New MailMessage(fromaddress, toaddress)

                    message.Subject = "I have sent you a message from a program!"
                    message.Body = "Hello World!"

                    Dim messanger As SmtpClient = New SmtpClient("smtpxxx", 995)

                    messanger.Credentials = New NetworkCredential("user", "password")
                    messanger.EnableSsl = True

                    messanger.Send(message)

1 个答案:

答案 0 :(得分:0)

你做了很多错事,你提供的一些信息不够清楚。主机名和端口名必须正确。它会选择您正在使用的邮件服务,从您的代码中未正确提供,因此我将提供一个使用gmail发送邮件的代码段。请完成此操作并根据SmtpHost进行更改。

     Try
        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("email", "password")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(txtemail.Text)
        e_mail.To.Add(txtemail.Text)
        e_mail.Subject = "Email Sending"
        e_mail.IsBodyHtml = False
        e_mail.Body = txtMessage.Text
        Smtp_Server.Send(e_mail)
        MsgBox("Mail Sent")
    Catch error_t As Exception
        MsgBox(error_t.ToString)
    End Try

此链接可帮助您找到smtp.Server and Port numbers,您可以使用此thread检查smtp.servers的限制