0

選択したファイルを VB.net フォームに添付して送信ボタンを押すと、添付ファイル付きの電子メールが送信されます。現時点では、フォームでダイアログ ボックスを開いてファイルを参照できますが、マシン上の場所からファイルを選択するとエラーが発生します。誰でも助けてもらえますか?ありがとう。これは、添付ボタンに使用したコードです。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim openDLG As New OpenFileDialog

    'openDLG.AddExtension = True
    openDLG.ReadOnlyChecked = True
    openDLG.Multiselect = True
    openDLG.Title = "Select the file(s) you want added to the message..."
    openDLG.Filter = "All Files (*.*)|*.*"

    If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then

        For Each item As String In openDLG.FileNames

            'Create a new System.NET.Mail.Attachment class instance for each file.
            attachToMsg = New System.Net.Mail.Attachment(item)

            'Then add the attachment to your message. You have to do this everytime you run the code
            'above.

            EmailMessage.Attachments.Add(attachToMsg)

        Next

        MsgBox("I have finished adding all of the selected files! You can do more if you want!")

    End If
End Sub

Button3 コード:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Using message As New MailMessage()
        'set to the from, to and subject fields
        message.From = (New MailAddress(TextBox3.Text.ToString()))
        message.[To].Add(New MailAddress("NO.ONE@elsewhere.com"))
        message.Subject = "New commission query"

        'code the message body
        Dim MsgBody As String
        MsgBody = TextBox1.Text.ToString() & vbCr & _
                  TextBox2.Text.ToString() & vbCr & _
                  TextBox3.Text.ToString() & vbCr & _
                  ComboBox1.Text.ToString() & vbCr & _
                  ComboBox2.Text.ToString() & vbCr & _
                  ComboBox3.Text.ToString() & vbCr & _
                  ComboBox4.Text.ToString() & vbCr
        message.Body = MsgBody
        Dim client As New SmtpClient()
        client.Host = "mailhost"
        client.Send(message)
    End Using
        'display submitted box
        MessageBox.Show("Your request has been submitted!", "Congratulations!")
        'close form
        Me.Close()
  End Sub
4

2 に答える 2

0

一時ファイル(フォルダ)に入れます。リソースからファイルを抽出し、一時ファイルに入れて開く例を次に示します。使用される一時フォルダーを返します。

 Public Overloads Function EXTRACT_FILE_FROM_RESOURCE(ByVal sFile As String, Optional ByVal andStart As Boolean = True) As String

        Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        Dim root As String = assembly.GetName().Name
        Dim stream As System.IO.Stream = assembly.GetManifestResourceStream(root & "." & sFile)
        Dim buffer(Convert.ToInt32(stream.Length) - 1) As Byte
        stream.Read(buffer, 0, buffer.Length)
        stream.Close()
        Dim sFolderUsed As String = GetTempDirectory()
        Dim sFilePath As String

        sFilePath = IO.Path.Combine(sFolderUsed , sFile )


        Using f As New IO.FileStream(sFilePath , IO.FileMode.Create, IO.FileAccess.Write)
            f.Write(buffer, 0, buffer.Length)
            f.Close()
            File.SetAttributes(sFilePath , File.GetAttributes(sFilePath ) And Not FileAttributes.ReadOnly)
        End Using

        If andStart Then
            StartProcess(sFilePath )
        End If
        Return sFolderUsed 


    End Function


    Public Function GetTempDirectory() As String
        Dim pathx As String
        Do
            pathx = IO.Path.Combine(IO.Path.GetTempPath(), IO.Path.GetRandomFileName())
        Loop Until Not Directory.Exists(pathx)
        Directory.CreateDirectory(pathx)
        Return pathx

    End Function
于 2013-02-12T11:28:09.487 に答える
0

Button3_Click コードの「メッセージ」オブジェクトと Button1_Click コードの「EmailMessage」は、同じ「メッセージ」オブジェクトである必要があります。

スティーブが上で述べたように...

EmailMessage 変数をどこで定義して初期化しましたか? デバッガーを使用して、添付ファイルを追加しようとしたときにその変数が Nothing であるかどうかを確認します – Steve 5 時間前

EmailMessage オブジェクトを DEFINED した WHERE のすべてのケースですか?

おそらく、EmailMessage オブジェクトを FORM レベル スコープで宣言し、次に Button3 コードを変更して、添付ファイル プロシージャ (Button1_Click コード) の代わりに同じ EmailMessage にアクセスする必要があります。また、変更を試みる前に、EmailMessage メッセージ オブジェクトを「インスタンス化」する必要があります。そのプロパティ。

だから、あなたは....の線に沿って何かを持っているでしょう.

'Declarations section (form scope)

Dim TheEmailMessage As New MailMessage()


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim openDLG As New OpenFileDialog

  'openDLG.AddExtension = True
  openDLG.ReadOnlyChecked = True
  openDLG.Multiselect = True
  openDLG.Title = "Select the file(s) you want added to the message..."
  openDLG.Filter = "All Files (*.*)|*.*"
  If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
    For Each item As String In openDLG.FileNames
      'Create a new System.NET.Mail.Attachment class instance for each file.
      attachToMsg = New System.Net.Mail.Attachment(item)
      'Then add the attachment to your message. 
      'You have to do this everytime you run the code above
      TheEmailMessage.Attachments.Add(attachToMsg)
    Next
    MsgBox("I have finished adding all of the selected files! You can do more if you want!")
  End If
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  Using TheEmailMessage

    'set to the from, to and subject fields
    TheEmailMessage.From = (New MailAddress(TextBox3.Text.ToString()))
    TheEmailMessage.[To].Add(New MailAddress("NO.ONE@elsewhere.com"))
    TheEmailMessage.Subject = "New commission query"

    'code the message body
    Dim MsgBody As String
    MsgBody = TextBox1.Text.ToString() & vbCr & _
      TextBox2.Text.ToString() & vbCr & _
      TextBox3.Text.ToString() & vbCr & _
      ComboBox1.Text.ToString() & vbCr & _
      ComboBox2.Text.ToString() & vbCr & _
      ComboBox3.Text.ToString() & vbCr & _
      ComboBox4.Text.ToString() & vbCr
    TheEmailMessage.Body = MsgBody
    Dim client As New SmtpClient()
    client.Host = "mailhost"
    client.Send(message)
  End Using
  'display submitted box
  MessageBox.Show("Your request has been submitted!", "Congratulations!")
  'close form
  Me.Close()
End Sub
于 2013-02-11T20:00:05.687 に答える