简体   繁体   English

vb.net发送邮件smtp和Outlook错误(单独)

[英]vb.net send mail smtp and outlook error (individually)

 'Variable which will send the mail
Dim obj As System.Net.Mail.SmtpClient

'Variable to store the attachments 
Dim Attachment As System.Net.Mail.Attachment

'Variable to create the message to send
Dim Mailmsg As New Mail.MailMessage()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try

        Dim ol As New Outlook.Application()
        Dim ns As Outlook.NameSpace
        Dim fdMail As Outlook.MAPIFolder

        ns = ol.GetNamespace("MAPI")
        ns.Logon(, , True, True)

        'creating a new MailItem object
        Dim newMail As Outlook.MailItem

        'gets defaultfolder for my Outlook Outbox
        fdMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)



        'assign values to the newMail MailItem
        newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
        newMail.Subject = "tesst"
        newMail.Body = "test"
        newMail.To = TextBox1.Text
        Dim sSource As String = Application.StartupPath + "\kk.sys"
        ' TODO: Replace with attachment name
        Dim sDisplayName As String = "kaar.jpg"

        Dim sBodyLen As String = newMail.Body.Length


        newMail.SaveSentMessageFolder = fdMail

        newMail.Send()

    Catch ex As Exception


        Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err1.txt")

            writer.WriteLine(ex.ToString)



        End Using

    End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        Dim address As New MailAddress(TextBox1.Text, "Nigraan")
        Dim oAttch As Mail.Attachment = New Mail.Attachment(Application.StartupPath + "\kk.sys")
        SmtpServer.Credentials = New  _
Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
        SmtpServer.Port = "587"
        SmtpServer.Host = "smtp.gmail.com"
        mail = New MailMessage()
        mail.From = New MailAddress(TextBox2.Text)
        mail.To.Add(New MailAddress(TextBox1.Text))
        mail.Subject = TextBox3.Text
        mail.Body = "test"


        mail.Attachments.Add(oAttch)

        SmtpServer.Send(mail)

    Catch ex As Exception

        Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err2.txt")

            writer.WriteLine(ex.ToString)



        End Using
    End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Try
        System.Diagnostics.Process.Start("mailto:" & TextBox1.Text & "?subject=" & "re:Subject" & "&body=" & "EmailBody")
    Catch ex As Exception

        Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err3.txt")

            writer.WriteLine(ex.ToString)



        End Using
    End Try
End Sub`

errors are: 错误是:

err1: 错误1:

System.Runtime.InteropServices.COMException (0x80004005): There must be at least one name or distribution list in the To, Cc, or Bcc box. System.Runtime.InteropServices.COMException(0x80004005):“收件人”,“抄送”或“密件抄送”框中必须至少有一个名称或分发列表。 at Microsoft.Office.Interop.Outlook._MailItem.Send() at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) WindowsApplication1.Form1.Button1_Click上Microsoft.Office.Interop.Outlook._MailItem.Send()上的(Object sender,EventArgs e)

err2: 错误2:

System.ArgumentException: The parameter 'address' cannot be an empty string. System.ArgumentException:参数'address'不能为空字符串。 Parameter name: address at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) at WindowsApplication1.Form1.Button2_Click(Object sender, EventArgs e) 参数名称:位于System.Net.Mail.MailAddress..ctor的地址(字符串地址,字符串displayName,Encoding displayNameEncoding),位于WindowsApplication1.Form1.Button2_Click(对象发送者,EventArgs e)

When i send using a machine with visual studio both mail gets sent, when not these errors show. 当我使用带有Visual Studio的机器发送邮件时,两个邮件都会发送出去,但不会显示这些错误。

i have double checked .net framework 我已经仔细检查了.net框架

thank you.. 谢谢..

Try creating a variable string and set that before sending the mail 尝试创建变量字符串并在发送邮件之前进行设置

Dim ToEmail as string
ToEmail = Textbox1.text

Then set your to address first. 然后将您的地址设置为第一。

'assign values to the newMail MailItem
    newMail.To = ToEmail
    newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
    newMail.Subject = "tesst"
    newMail.Body = "test"

i got everything working, 我一切正常

got smtp to work by giving ssl encryption to true 通过将ssl加密设置为true来使smtp正常工作

got outlook to work by creating a contact and giving the contact's email id in the 'to' field 通过创建联系人并在“收件人”字段中提供联系人的电子邮件ID来使Outlook工作

just don't save the contact if u don't want the contact to be added in outlook :D 如果您不希望在Outlook中添加联系人,请不要保存该联系人:D

yeyyy!! yeyyy!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM