使用outlook代替gmail发送邮件vb.net 2010

时间:2012-12-05 15:38:24

标签: email vb.net-2010

我有一个班级发送带有多个附件的电子邮件,它使用gmail,但我如何使用Outlook发送电子邮件?

   Imports System.Net.Mail
    Imports System.Net.Mime


    Public Sub SendThis(ByVal SubjectText As String, _
                             ByVal BodyText As String, _
                             ByVal FromAddress As String, _
                             ByVal ToAddress As String, _
                             Optional ByVal FileName As Collection = Nothing _
                             )
            Try
                Dim email As New Net.Mail.MailMessage(FromAddress, ToAddress)
                email.Subject = SubjectText
                email.Body = BodyText
                If Not FileName Is Nothing Then
                    For Each Name As String In FileName
                        Dim attach As New Net.Mail.Attachment(Name) 'Includes Path
                        email.Attachments.Add(attach)
                    Next
                    For Each At As Attachment In email.Attachments
                        At.TransferEncoding() = Net.Mime.TransferEncoding.Base64
                    Next
                End If
                Dim TheSmtp As New SmtpClient(YourSmtpServerName, 587)
                TheSmtp.Credentials = New Net.NetworkCredential("abc@gmail.com","MYPASS")
                TheSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
                TheSmtp.Send(email)
                email.Attachments.Clear()
                TheSmtp = Nothing
                email = Nothing    
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "HFB", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        End Sub

我将这个函数称为:

      Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
            Dim BodyText As String
            Dim SubjectText As String
            Dim FromAddress As String
            Dim ToAddress As String
            Dim Filename As New Collection
            If Me.LstBxAttach.Items.Count > 0 Then
                For Each TheItem As String In LstBxAttach.Items
                    Filename.Add(TheItem)
                Next
            End If
            SubjectText = Me.TbSubject.Text
            BodyText = Me.TbBody.Text
            SendThis(SubjectText, _
                          BodyText, _
                          "from@example.com", _
                          "to@example.com", _
                          Filename _
                          )
            SubjectText = ""
            BodyText = ""
            FromAddress = ""
            ToAddress = ""
            MessageBox.Show("Sent!", "HFB", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用Outlook object modelApplication对象可让您连接到Outlook;要发送电子邮件,您需要创建一个MailItem对象,填充相关属性,并调用其Send方法。