Bonjour,

Dans l'exemple VB-NET-OUTLOOK-SEND-EMAIL que vous pouvez trouver à cet endroit : https://code.msdn.microsoft.com/wind...thId=293681583

Voici un extrai du code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
Imports Microsoft.Office 
 
 
Public Class Form1 
    Dim sFilesource As String 
    Dim sName As String 
 
    Private Sub setEmailSend(sSubject As String, sBody As String, _ 
                             sTo As String, sCC As String, _ 
                             sFilename As String, sDisplayname As String) 
        Dim oApp As Interop.Outlook._Application 
        oApp = New Interop.Outlook.Application 
 
        Dim oMsg As Interop.Outlook._MailItem 
        oMsg = oApp.CreateItem(Interop.Outlook.OlItemType.olMailItem) 
 
        oMsg.Subject = sSubject 
        oMsg.Body = sBody 
 
        oMsg.To = sTo 
        oMsg.CC = sCC 
 
 
        Dim strS As String = sFilename 
        Dim strN As String = sDisplayname 
        If sFilename <> "" Then 
            Dim sBodyLen As Integer = Int(sBody.Length) 
            Dim oAttachs As Interop.Outlook.Attachments = oMsg.Attachments 
            Dim oAttach As Interop.Outlook.Attachment 
 
            oAttach = oAttachs.Add(strS, , sBodyLen, strN) 
 
        End If 
 
        oMsg.Send() 
        MessageBox.Show("Email Send", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
 
        txtBody.Text = "" 
        txtTo.Text = "" 
        txtCC.Text = "" 
        txtFile.Text = "" 
        txtSubject.Text = "" 
 
        KillOutLookProcess() 
 
    End Sub 
 
    Private Sub KillOutLookProcess() 
        Try 
            Dim Xcel() As Process = Process.GetProcessesByName("OUTLOOK") 
            For Each Process As Process In Xcel 
                Process.Kill() 
            Next 
        Catch ex As Exception 
        End Try 
    End Sub 
    Public Function AppPath() 
        Return System.AppDomain.CurrentDomain.BaseDirectory 
    End Function 
 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
        If txtTo.Text <> "" Then 
            setEmailSend(txtSubject.Text, txtBody.Text, txtTo.Text, txtCC.Text, sFilesource, sName) 
        Else 
            MessageBox.Show("No Recipient", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information) 
        End If 
    End Sub 
 
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
        With OpenFileDialog1 
            .Filter = "All Files(*.*)|*.*" 
            .FileName = "" 
            .ShowDialog() 
            sFilesource = .FileName 
 
            sName = .FileName 
 
            Dim cCnt As Integer, iSlash As Integer, x As Integer, iStart As Integer, iLenght As Integer, iSlash1 As Integer 
            For cCnt = 0 To sFilesource.Length - 1 
                If sFilesource(cCnt) = "\" Then 
                    iSlash = iSlash + 1 
                End If 
            Next 
 
            For x = 0 To sFilesource.Length - 1 
                If sFilesource(x) = "\" Then 
                    iSlash1 = iSlash1 + 1 
                    If iSlash = iSlash1 Then 
                        iStart = x 
                        iLenght = sFilesource.Length - x 
                        Exit For 
                    End If 
                End If 
            Next 
 
            sName = sFilesource.Substring(iStart + 1, iLenght - 1) 
 
            txtFile.Text = sName 
        End With 
    End Sub 
End Class
Dans ce code si il y a une pièce jointe, l'e-mail part sans soucis.
Alors que si je n'ai pas besoin de joindre quelque chose ça bug.

Que dois-je modifier dans la ligne ci-dessous pour améliorer l'envoi du mail ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
  If sFilename <> "" Then 
            Dim sBodyLen As Integer = Int(sBody.Length) 
            Dim oAttachs As Interop.Outlook.Attachments = oMsg.Attachments 
            Dim oAttach As Interop.Outlook.Attachment 
 
            oAttach = oAttachs.Add(strS, , sBodyLen, strN) 
 
        End If
En espérant que le soucis vienne bien de là !!

Merci pour le coup de pouce

Cordialement,

Bruno