Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please i need help adding Attachments to my code please help everything works fin apart from the Attachments thanks for any help

VB
With openDLG
'this line of code contains my openfile dainlog adding my the files in my listviwe to an arry
           .Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt"
           .FilterIndex = 1
           ' The OpenFileDialog control only has an Open button, not an OK button.
           ' However, there is no DialogResult.Open enum so use DialogResult.OK.
           If .ShowDialog() = Windows.Forms.DialogResult.OK Then
               If IsNothing(arlAttachments) Then
                   arlAttachments = New ArrayList()

                   ' Clear the "(No Attachments)" default text in the ListView
                   Attachments.Items.Clear()
               End If
               arlAttachments.Add(New Attachment(.FileName))
               ' You only want to show the file name. The OpenFileDialog.FileName
               ' property contains the full path. So Split the path and reverse it
               ' to grab the first string in the array, which is just the FileName.
               Dim strFileName() As String = .FileName.Split(New Char() {CChar("\")})
               System.Array.Reverse(strFileName)
               'Attachments.Items.Add(strFileName(0) + "(" + (New FileInfo(openDLG.FileName).Length / 1000).ToString("f2") + " KB)")
               objItem = Attachments.Items.Add((strFileName(0)))
               objItem.SubItems.Add((New FileInfo(openDLG.FileName).Length / 1000).ToString("f2") + " " + "KB")
           End If
       End With



VB
Public Async Function sed() As Tasks.Task
      Dim mybody As String = mbody.Text
      Dim myto As String = txtto.Text
      Dim mysubject As String = txtsubject.Text
      Dim mysender As String = txtname.Text
      Dim myfrom As String = txtfrom.Text
      Dim mycc As String = txtCc.Text
      Dim mybcc As String = txtbcc.Text
      If Not IsNothing(arlAttachments) Then
          Dim mailAttachment As BinaryReader
          For Each mailAttachment In arlAttachments
              ' e_mail.Attachments.Add(mailAttachment)

          Next
      End If

      Dim postDatas = "&mybody=" & mybody & "&myto=" & myto & "&mysubject=" & mysubject & "&mysender=" & mysender & "&myfrom=" & myfrom & "&myattach=" & arlAttachments.ToString

' Create a request using a URL that can receive a post.
      Dim request As WebRequest = WebRequest.Create("http://wolfsoftonline.com/send.php")
      ' Set the Method property of the request to POST.
      request.Method = "POST"
      ' Create POST data and convert it to a byte array.
      Dim postData As String = postDatas
      Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
      ' Set the ContentType property of the WebRequest.
      request.ContentType = "application/x-www-form-urlencoded"
      ' Set the ContentLength property of the WebRequest.
      request.ContentLength = byteArray.Length
      ' Get the request stream.
      Dim dataStream As Stream = Await request.GetRequestStreamAsync
      ' Write the data to the request stream.
      dataStream.Write(byteArray, 0, byteArray.Length)
      ' Close the Stream object.
      dataStream.Close()
      ' Get the response.
      Dim response As WebResponse = Await request.GetResponseAsync
      ' Display the status.
      If (CType(response, HttpWebResponse).StatusDescription) = "OK" Then
          MsgBox("")
      End If
      ' Get the stream containing content returned by the server.
      dataStream = response.GetResponseStream()
      ' Open the stream using a StreamReader for easy access.
      Dim reader As New StreamReader(dataStream)
      ' Read the content.
      Dim responseFromServer As String = Await reader.ReadToEndAsync
      ' Display the content.
      MsgBox(responseFromServer)
      ' Clean up the streams.
      reader.Close()
      dataStream.Close()
      response.Close()
  End Function


here is my php code
PHP
<?php
    $myb =mysql_real_escape_string($_POST['mybody']);
    $myt =mysql_real_escape_string($_POST['myto']);
    $mysb =mysql_real_escape_string($_POST['mysubject']);
    $mys =mysql_real_escape_string($_POST['mysender']);
    $myf =mysql_real_escape_string ($_POST['myfrom']);

$htmlbody = $myb;



$to = $myt; //Recipient Email Address

$subject = $mysb; //Email Subject

$headers = "From:".$myf."\r\nReply-To: ".$myf."";

$random_hash = md5(date('r', time()));

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents('nopic.jpg'))); // Set your file path here

//define the body of the message.

$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"nopic.jpg\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );

echo $mail ? "Mail sent" : "Mail failed";


?>
Posted
Updated 26-Dec-14 21:20pm
v2
Comments
DamithSL 27-Dec-14 4:19am     CRLF
your php site and vb.net site running on same server or not? why you need php site for sending email? you can do it from vb.net
Wolfsoftonline 27-Dec-14 4:22am    
vb.net can be DE-compiled and am coding a native app nt a webapp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900