doubt sending emails from Visual. net 2010

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ireferpesa
    New Member
    • May 2010
    • 1

    #1

    doubt sending emails from Visual. net 2010

    I have a management program with which a file sent by mail, the party of running the outlook is great, but when teams do not have outlook express and have only broken and that I have to check if there is me outlook mapi and only use, but that does not go.
    Code:
    Private Sub Enviar_mail()
      'comprobar si esta outlook o outlook express
      Dim fallo As Boolean = False
    
      Dim Objeto As Object
      ' Crea una referencia al objeto
      Try
        Objeto = CreateObject("Outlook.Application")
      Catch ex As OleDbException
        fallo = True
      End Try
    
      ' No dío error
      If fallo = True Then
        Dim fila As DataRowView
        Dim STRCod As String
    
        For Each fila In Me.List_Proyecto.SelectedItems
          STRCod = fila.Item(2)
          AxMAPIMessages1.AttachmentName = STRCod
          AxMAPIMessages1.AttachmentPathName = ruta & "Archivos\" & Me.ComboCliente.Text & "\" & STRCod
        Next
    
        'lo mandamos con outlook express
        AxMAPISession1.SignOn()
        With AxMAPIMessages1
          WithMAPIMessages()
          .MsgIndex = -1
          .RecipDisplayName = ""
          .MsgSubject = ""
          .MsgNoteText = ""
          .SessionID = AxMAPISession1.SessionID 'MAPISession.SessionID
          .ResolveName()
          .AttachmentIndex = 0
          .AttachmentPosition = 0
          .AttachmentType = 0
          '.Send()
        End With
        AxMAPISession1.SignOff()
    
      Else
        'Crear una aplicación Outlook.
        Dim oApp As Microsoft.Office.Interop.Outlook.Application
        oApp = New Microsoft.Office.Interop.Outlook.Application()
        
        ' Crear un nuevo elemento de correo.
        Dim oMsg As Microsoft.Office.Interop.Outlook._MailItem
        oMsg = oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
        oMsg.Body = "" & vbCr & vbCr
        
        Dim fila As DataRowView
        Dim STRCod As String
    
        Dim sBodyLen As String = oMsg.Body.Length
        Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments = oMsg.Attachments
        Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment
    
        For Each fila In Me.List_Proyecto.SelectedItems
          STRCod = fila.Item(2)
          oAttach = oAttachs.Add(ruta & "Archivos\" & Me.ComboCliente.Text & "\" & STRCod, , sBodyLen + 1, )
    
          '' Abrir mensaje nuevo
          oMsg.Display()
    
        End If
    
    End Sub
    Last edited by Frinavale; May 27 '10, 08:05 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags and indentation for legibility.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I wouldn't send email through Outlook at all. As you have already found out, not everyone has it.

    Just send an email directly through your application.

    The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      On line 58 in the above posted code you have a For Each loop but you do not have an ending "next" to close the loop.

      Other than that, I'm not sure how to check if outlook exists on the machine your application is installed on.

      I recommend that you avoid using Outlook and use the .NET tools for sending emails. Check out this Quick reference on how to send email using .NET for more information.

      -Frinny

      Comment

      Working...