[英]Send an email on VB.net
Can anyone help me. 谁能帮我。 I've created a email program.
我创建了一个电子邮件程序。 I'm having an error when clicking on the send button.
单击发送按钮时出现错误。 Here's my code:
这是我的代码:
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
And I've got this error: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 而且我遇到了以下错误:System.ArgumentOutOfRangeException:指定的参数不在有效值范围内。 Parameter name:value at System.Net.Mail.SmtpClient.set_Port(Int32 value)
参数名称:System.Net.Mail.SmtpClient.set_Port处的值(Int32值)
***Edited forgot to modify it to the original code ***编辑忘记将其修改为原始代码
Your sub Private Sub frmEmail_Load(sender As Object, e As EventArgs) 您的子私人子frmEmail_Load(作为对象发送,作为EventArgs发送)
doesnt run when the form loads. 窗体加载时不运行。
Because of this port
is set to the default value of 0
由于此
port
设置为默认值0
You need to tell VB.Net to run when the form loads like this 您需要告诉VB.Net在表单加载时运行,如下所示
Private Sub frmEmail_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Also further on down the code, you might find that you get an error about authentication from the email server. 同样在代码下方,您可能会发现从电子邮件服务器收到有关身份验证的错误。 If you get this, just below
如果得到这个,就在下面
Dim smtpServer As New SmtpClient
add this 加上这个
smtpServer.UseDefaultCredentials = False
it might work. 它可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.