任何人都可以帮助我。我已经创建了一个电子邮件程序。单击发送按钮时出错。这是我的代码:
Imports System.Net.Mail
Public Class frmEmail
Dim emailaddress As String
Dim password As String
Dim port As Integer
Dim host As String
Private Sub frmEmail_Load(sender As Object, e As EventArgs)
If emailaddress.ToLower.Contains("@gmail") Then
port = 587
host = "smtp.gmail.com"
ElseIf emailaddress.ToLower.Contains("@yahoo") Then
port = 465
host = "smtp.mail.yahoo.com"
End If
End Sub
Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
Try
Dim smtpServer As New SmtpClient
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential(emailaddress, password)
smtpServer.Port = port
smtpServer.Host = host
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress(emailaddress)
mail.To.Add(txtTo.Text)
mail.Subject = txtSubject.Text
mail.Body = txtMessage.Text
smtpServer.Send(mail)
MsgBox("The mail is send!", MsgBoxStyle.Information)
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
End Class
我遇到了这个错误:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:value 在System.Net.Mail.SmtpClient.set_Port(Int32值)
***编辑忘记将其修改为原始代码
答案 0 :(得分:-1)
你的潜艇 Private Sub frmEmail_Load(sender as Object,e As EventArgs)
在表单加载时不会运行。
由于此port
设置为默认值0
你需要告诉VB.Net在表单加载时运行
Private Sub frmEmail_Load(sender As Object, e As EventArgs) Handles MyBase.Load
此外,在代码中,您可能会发现从电子邮件服务器收到有关身份验证的错误。如果你得到这个,就在
下面Dim smtpServer As New SmtpClient
添加此
smtpServer.UseDefaultCredentials = False
它可能有用。