【问题标题】:VB.net - Error When Sending email using smtpclientVB.net - 使用 smtpclient 发送电子邮件时出错
【发布时间】:2014-09-15 11:46:16
【问题描述】:
Dim MailBody1 As String = vbCrLf & "A new carrier tape (Part Number: " & code & ") has been added into the system. Please verify and buy-off the specs in the MRP system. " & vbCrLf & vbCrLf & "MRP System: " & tsURL & vbCrLf & "Module: Manufacturing --> First Article --> QA"
Dim MailClient1 = New SmtpClient("localhost")
MailClient1.send(MailFrom1, MailTo1, MailSubject1, MailBody1)
'Catch ex As Exception
'End Try

指定的字符串不是电子邮件所需的格式 地址。

这是它显示的错误。在此之前它可以完美运行。有人帮忙吗?

这里有完整的功能代码

Public Sub send_email(ByVal code)
    Dim tsURL As String = "http://orion/one/"
    Dim n As Integer
    Dim email_add As String

    conn1("SELECT * FROM hr_employee WHERE emp_position IN ('QA ENGINEER', 'SR. QA ENGINEER') AND status = 'employed'", "email_add")
    If ds.Tables("email_add").Rows.Count > 0 Then
        For n = 0 To ds.Tables("email_add").Rows.Count - 1
            dr = ds.Tables("email_add").Rows(n)
            If n <> ds.Tables("email_add").Rows.Count - 1 Then
                email_add = email_add + dr("email")
                email_add = email_add + ", "
            Else
                email_add = email_add + dr("email")
            End If
        Next
    End If

    'Try
    Dim MailFrom1 As String = "ts_admin@astigp.com"
    'Dim MailTo1 As String = "tancheekeong@astigp.com, keesoonwei@astigp.com, karentan@astigp.com"
    Dim MailTo1 As String = email_add
    Dim MailSubject1 As String = "New Carrier Tape : " & code
    Dim MailBody1 As String = vbCrLf & "A new carrier tape (Part Number: " & code & ") has been added into the system. Please verify and buy-off the specs in the MRP system. " & vbCrLf & vbCrLf & "MRP System: " & tsURL & vbCrLf & "Module: Manufacturing --> First Article --> QA"
    Dim MailClient1 = New SmtpClient("localhost")
    MailClient1.send(MailFrom1, MailTo1, MailSubject1, MailBody1)
    'Catch ex As Exception
    'End Try

【问题讨论】:

  • 错误信息很清楚。检查MailFrom1MailTo1的值,它们可能包含无效的邮件地址。
  • 数值正确。
  • 正如我刚才在数据库中检查的那样,email_add 的 nvarchar 是为什么我弄错了?如您所见,我已将其转换为字符串

标签: .net vb.net smtp smtpclient


【解决方案1】:

您的 MailTo1 中似乎包含无效的电子邮件地址。 也许尝试删除逗号后的空格。

例如email_add = email_add + ","

【讨论】:

    【解决方案2】:

    这是使用 SmtpClient 发送邮件的最简单方法

       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
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多