I am trying to send mail with attachment through hosted website on IIS7.5.
I am able to send mail without attachment but not with attachment.
I have developed web site in Asp.Net MVC4.
I am able to send mail through IIS7.5 which installed on my local machine.
MemoryStream objMemoryStream = new MemoryStream();
StringReader sr = new StringReader(RazorViewToString.RenderRazorViewToString(this, "~/Views/Booking/BookingFormPdf.cshtml", objBookingForm));
Document pdfDoc = new Document(PageSize.A3, 20f, 20f, 20f, 20f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, objMemoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] Attachment = objMemoryStream.ToArray();
string Subject = objBookingForm.modBookingDetail.CompanyName + " Confirmation of Advertising Booking " + BookingId + ".";
string Body = "Dear " + objBookingForm.modBookingDetail.CustomerContact + "," + Environment.NewLine + Environment.NewLine + "We are pleased to confirm your advertising booking with " + objBookingForm.modBookingDetail.CompanyName + Environment.NewLine + Environment.NewLine + "Please find attached a pdf copy for your records." + Environment.NewLine + Environment.NewLine + "Regards" + Environment.NewLine + Environment.NewLine + objBookingForm.modBookingDetail.BookedBy + Environment.NewLine + objBookingForm.modBookingDetail.CompanyName;
SendEmail(objBookingForm.modBookingDetail.CustomerEmailId, Subject, Body, Attachment);
Related
i have this code in my vb.net application:
myHttpWebRequest = WebRequest.Create(url)
myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
If myHttpWebResponse.StatusCode = HttpStatusCode.OK Then
receivestream = myHttpWebResponse.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(receivestream, encode)
response = readStream.ReadToEnd()
myHttpWebResponse.Close()
receivestream.Close()
readStream.Close()
TextBox1.Text = TextBox1.Text + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + vbCrLf + "URL: " + url + vbCrLf + vbCrLf
'scroll to bottom
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
Me.Refresh()
Else
TextBox1.Text = TextBox1.Text + "Could not contact server for: " + url + vbCrLf + vbCrLf
'scroll to bottom
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
Me.Refresh()
End If
sometimes i get 500 internal server errors or 404 not found
also if the internet connection goes down that this program is running on i get an error too
the above code is inside a loop. how can i just skip this loop if there is an error
Use a try catch outside your loop, and log your errors. You can exit a for loop with a simple Exit keyword. Also you can refactor your code into a single method and just exit that as well.
Trying to convert and old Pachube feed to Xively using Visual Studio 2013. Receive a 400 error.
Here is a snippet of my code base.It still works with my old pachube feed but after changing to xlivey api no longer works.
Dim PachubeStream As String = timestamp + "," + PT1Meter + "," + PT2Meter + "," + LevelOffset + "," + CurrentLevel + "," + PT1Raw + "," + PT2Raw + "," + PT1Offset + "," + PT2Offset + "," + RateofRise + "," + PeakRateofRise
'prepare to Upload the latest changed data to pachube for on-line data storage and plotting
Dim myWebClient As New WebClient()
uploaddatapachubekey = TextBox7.Text
myWebClient.Headers.Add("X-ApiKey", uploaddatapachubekey)
' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(PachubeStream)
'added 11/11/2010
'configure uploaddata pachube port url
uploaddatapachubeport = "https://api.xively.com/v2/feeds/" + pachubeport + ".csv"
'UploadData input string using Http 1.0 PUT Method if not in test mode
'added 11/11/2010
If TestMode = False Then myWebClient.UploadData(uploaddatapachubeport, "put", byteArray)
Solved my own problem. Issue was the new format of all data submission is
"Channel Name",Timestamp UTC Format,Channel Data VBCRLF
i.e.
Dim PachubeStream As String = "PT1Meter"+","+timestamp+","+PT1Meter
there is a lot of topic about sending email with attachment but i couldn't find what i was looking for
my form contain 8 textbox and 1 PictureBox i can send all textbox via email but ican't send PictureBox i try many code
this is my form.
and here is my code
Dim MyMessage As New MailMessage
Try
MyMessage.From = New MailAddress("x#gmail.com")
MyMessage.To.Add("x#gmail.com")
MyMessage.Subject = "Data"
MyMessage.Body = Label1.Text + " = " + IDTextBox.Text + Environment.NewLine + Label2.Text + " = " + ناوی_سیانی_کارمەندTextBox.Text + Environment.NewLine + Label3.Text + " = " + ساڵی_لە__دایک_بوونTextBox.Text + Environment.NewLine + Label4.Text + " = " + شوێنی_دانیشتنTextBox.Text + Environment.NewLine + Label5.Text + " = " + گروپی_خوێنTextBox.Text + Environment.NewLine + Label6.Text + " = " + ناو_نیشانی_کار_لە_کۆمپانیاTextBox.Text + Environment.NewLine + Label7.Text + " = " + ژمارەی_مۆبایلTextBox.Text + Environment.NewLine + Label8.Text + " = " + شوێنی_کارTextBox.Text
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("x#gmail.com", "x")
SMTP.Send(MyMessage)
MsgBox("your message Has been sent successfully")
Catch ex As Exception
End Try
i will be greatfull if you could help me :)
Have you tried this.You can use the LinkedResource and AlternatiView for the same.
string path = Filelocation; // as you are using picture box,give the location from where the image is loading into picture box.
LinkedResource imageLink = new LinkedResource(path);
imageLink.ContentId = "myImage";
AlternateView altView = AlternateView.CreateAlternateViewFromString(
"<html><body><img src=cid:myImage/>" +
"<br></body></html>" + strMailContent,
null, MediaTypeNames.Text.Html);
altView.LinkedResources.Add(imageLink);
//now append it to the body of the mail
msg.AlternateViews.Add(altView);
msg.IsBodyHtml = true;
or
You can send it with HtmlBody here is the
reference : http://vba-useful.blogspot.fr/2014/01/send-html-email-with-embedded-images.html
You need to add the directory of the picture file or any file you have and add it as attachment to your mail like this...
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment(FilePath)
MyMessage.Attachments.Add(attachment)
I was sucessful in sending email using smtpserver. I am writing a VB.Net application.
My current code is:
SmtpServer.Credentials = New Net.NetworkCredential(mEMailUser, mEMailPassword)
SmtpServer.Port = 587
SmtpServer.Host = mHostName
mail = New MailMessage()
mail.From = New MailAddress(mFromEMail)
mail.To.Add(mfrmSendAnEmail.txtTo.Text)
mail.Subject = mfrmSendAnEmail.txtSubject.Text
_Attachment = mDirectory & "\" & gcloGlobals.DocumentName & ".pdf"
Dim oAttch As Attachment = New Attachment(_Attachment)
mail.Attachments.Add(oAttch)
mail.Body = mfrmSendAnEmail.txtBody.Text
SmtpServer.Send(mail)
I want the Recipient to get a read request notification so that when they open the email they will have the option of sending the sender a notification that it was read.
I thought this might work:
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
but it didn't.
Here's the code I use:
message.Headers.Add("Disposition-Notification-To", "\"" + fromAddress.DisplayName + "\" <" + fromAddress.Address + ">");
In your code you don't set a display name for the from address, so you should use the simpler form instead of the line given above:
message.Headers.Add("Disposition-Notification-To", mFromEMail);
I am currently developing an application using VB.NET in which I am using the REST WebServices. I have been able to do the basics with REST, however, I have not been able to add an attachment (more specifically upload a file, using REST which gets attached). I have done extensive research online, but so far I have not been able to find any working examples in VB.NET. To actually upload the data I use System.Net.WebClient. The following VB.NET code does the important work:
Dim Client As New System.Net.WebClient
Dim postBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(postString)
Client.UploadData(URL, "POST", postBytes)
A simplified version of my URL is as follows:
"../REST/1.0/ticket/" + ticketNumber + "/comment?user=" + userName + "&pass=" + password
Finally, an example of the content that I post is:
postString = "content=Text: RT Test" + vbLf + "Action: Comment" + vbLf + "Attachment: examplefile.jpg" + vbLf + "attachment_1="
As you can see, the postString is converted to bytes and then uploaded to the server. However, I do not know where or how I should be posting the raw attachment itself. The documentation for the service we are specifically using states to use a variable "attachment_1," which I added to the postString variable, but I am not sure what the next step should be. Should the file be converted into bytes and appended to the postBytes variable? I attempted something like this but I received an error saying that no attachment was found for examplefile.jpg.
Thanks for your help!
We could not use Client.UploadData(...) and had to convert the entire post to bytes, starting with the POST fields before the attachment, then the attachment itself, and finally the remainder of the POST fields.
Public Sub AddAttachmentToRT(ByVal url As String, ByVal fileName As String, ByVal filePath As String)
Dim dataBoundary As String = "--xYzZY"
Dim request As HttpWebRequest
Dim fileType As String = "image/jpeg" 'Will want to extract this to make it more generic from the uploaded file.
'Create a POST web request to the REST interface using the passed URL
request = CType(WebRequest.Create(url), HttpWebRequest)
request.ContentType = "multipart/form-data; boundary=xYzZY"
request.Method = "POST"
request.KeepAlive = True
'Write the request to the requestStream
Using requestStream As IO.Stream = request.GetRequestStream()
'Create a variable "attachment_1" in the POST, specify the file name and file type
Dim preAttachment As String = dataBoundary + vbCrLf _
+ "Content-Disposition: form-data; name=""attachment_1""; filename=""" + fileName + """" + vbCrLf _
+ "Content-Type: " + fileType + vbCrLf _
+ vbCrLf
'Convert this preAttachment string to bytes
Dim preAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(preAttachment)
'Write this preAttachment string to the stream
requestStream.Write(preAttachmentBytes, 0, preAttachmentBytes.Length)
'Write the file as bytes to the stream by passing its exact location
Using fileStream As New IO.FileStream(Server.MapPath(filePath + fileName), IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(4096) As Byte
Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
Do While (bytesRead > 0)
requestStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
Loop
End Using
'Create a variable named content in the POST, specify the attachment name and comment text
Dim postAttachment As String = vbCrLf _
+ dataBoundary + vbCrLf _
+ "Content-Disposition: form-data; name=""content""" + vbCrLf _
+ vbCrLf _
+ "Action: comment" + vbLf _
+ "Attachment: " + fileName + vbCrLf _
+ "Text: Some description" + vbCrLf _
+ vbCrLf _
+ "--xYzZY--"
'Convert postAttachment string to bytes
Dim postAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postAttachment)
'Write the postAttachment string to the stream
requestStream.Write(postAttachmentBytes, 0, postAttachmentBytes.Length)
End Using
Dim response As Net.WebResponse = Nothing
'Get the response from our REST request to RT
'Required to capture response, without this Try-Catch attaching will fail
Try
response = request.GetResponse()
Using responseStream As IO.Stream = response.GetResponseStream()
Using responseReader As New IO.StreamReader(responseStream)
Dim responseText = responseReader.ReadToEnd()
End Using
End Using
Catch exception As Net.WebException
response = exception.Response
If (response IsNot Nothing) Then
Using reader As New IO.StreamReader(response.GetResponseStream())
Dim responseText = reader.ReadToEnd()
End Using
response.Close()
End If
Finally
request = Nothing
End Try
End Sub