I have tried almost every thing but can't get this to work. I will appreciate any help. thanks
Imports Microsoft.VisualBasic, System.Net.Mail
Shared Sub SendMailHTML(ByVal ToAdd, ByVal FromAdd, ByVal Message, ByVal Subject)
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.Credentials = New Net.NetworkCredential("xxxx#gmail.com", "xxxxxx")
SmtpServer.UseDefaultCredentials = False
SmtpServer.Port = 465
SmtpServer.EnableSsl = True
SmtpServer.Timeout = 5000
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
Try
mail.From = New MailAddress("xxxxx#gmail.com")
mail.To.Add("xxxxxx#gmail.com")
SmtpServer.Send(Mail)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
You can try changing the port to 587 for gmail.
And also add in the try block
mail.Subject= ...
mail.Body= ...
(add suitable strings here)
also try running it without
SmtpServer.UseDefaultCredentials = False
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
I can add my code in C# which works if you need as I do not know VB
Related
I am trying to send an email after a click of a button. But this is the exception i get
Is something wrong with my code ? I have already tried to go to my gmail settings and turned on the "Access for less secure apps" , still problem persists.
Imports System.Net.Mail
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page`
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Try
Dim mailMessage As New MailMessage("group2#gmail.com", "ndumisosizwe#gmail.com")
mailMessage.Subject = "Mail Body"
mailMessage.Body = "This is a test email"
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
Dim credentials As New NetworkCredential("group2#gmail.com", "myPasswordHere")
SmtpClient.Credentials = credentials
SmtpClient.EnableSsl = True
SmtpClient.Send(mailMessage)
MsgBox("Email sent successfully")
Catch ex As Exception
MsgBox("Email Failed ! " & ex.ToString)
End Try
End Sub
End Class
The exception is thrown on the line SmtpClient.Send(mailMessage)
The port you are using is for TLS according to this page.
The port for SSL (older version) is 465.
Perhaps EnableSsl does not support TLS. You can try with that port.
The code I'm troubleshooting exports a Crystal Report ReportDocument to Excel. Most of the time, the export works just fine. Unfortunately for some datasets, the ExportToHttpResponse method never returns and causes the app to hang. Eventually there is a Thread was being aborted exception along with a request timeout.
Here is the line that hangs:
reportDocument.ExportToHttpResponse(ExportFormatType.Excel,Response,True, fileName);
I also tried using ExportToStream from here which also hangs:
System.IO.Stream myStream;
byte[] byteArray;
myStream = boReportDocument.ExportToStream (ExportFormatType.PortableDocFormat);
I have tried different export formats, restarting IIS, etc. There seems to be a size limit or perhaps specific data scenarios that cause these methods to hang. Any workarounds or explanations for this behavior? Thanks!
Try this:
Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.Buffer = True
response.Charset = ""
'response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
response.ContentType = "application/vnd.ms-excel"
'response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & ".xls")
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
Dim dg As New DataGrid()
dg.DataSource = ds
dg.DataBind()
dg.RenderControl(htw)
response.Charset = "UTF-8"
response.ContentEncoding = System.Text.Encoding.UTF8
response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
response.Output.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub
and in your viewer add :
DT = New DataTable
DT = (Your Method)
ExportDataSetToExcel(DT, "ExportedReport")
also add :
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
ReportObject.Close()
ReportObject.Dispose()
End Sub
So report would not add restrictions on the number of loaded reports.
I had a working mail client in Visual Basic that sent email perfectly. Then one day it stopped working. When I used VB's try/catch on my method the exception it threw was Failure Sending Mail.
This doesn't help of course, because it's incredibly general. I have no clue why the mail isn't being sent. I searched the web, as well as StackOverflow, but none of the solutions provided worked for me.
I've tried Aol and Gmail and I'm sure that all passwords and usernames are correct. Also the port is correct too. What could be the problem?
Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
Try
Dim smtpServer As New SmtpClient("smtp.aol.com")
Dim Mail As New MailMessage()
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("Email", "Pass"
smtpServer.Port = 587
smtpServer.Host = "smtp.aol.com"
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
smtpServer.EnableSsl = True
Mail.From = New MailAddress("Email")
Mail.To.Add("Email")
smtpServer.Send(Mail)
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
My code is adding a user to my database. I am trying to have my site send an email with a passcode to verify the email account is legit.
First I am trying to get it to send a basic test email. Then I plan on adding the passcode in a link to my site validating it.
My problem is my code doesn't send the basic test email.
Imports System.Data.SqlClient
Imports System.Net.Mail
Partial Class Account_Register
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RegisterUser.ContinueDestinationPageUrl = Request.QueryString("ReturnUrl")
End Sub
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)
Dim MyMailMessage As New MailMessage()
' MyMailMessage.IsBodyHtml = True
MyMailMessage.From = New MailAddress("NAME#gmail.com")
MyMailMessage.To.Add("Name#yahoo.com")
MyMailMessage.Subject = "Email Confirmation"
MyMailMessage.Body = "TESTING"
'MyMailMessage.Body = "<html>" & RegisterUser.UserName & "Link: $" & "<br/> " & "</html>"
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 588
SMTPServer.Credentials = New System.Net.NetworkCredential("NAME#gmail.com", "Password")
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
'MessageBox.Show("Email Sent")
Catch ex As SmtpException
'MessageBox.Show(ex.Message)
End Try
Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
If String.IsNullOrEmpty(continueUrl) Then
continueUrl = "~/"
End If
Response.Redirect(continueUrl)
End Sub
End Class
Any help?
Change the port number Gmail SMTP port is : 465
Use this
SMTPServer.Port = 465
So I ended up having to use Port = 587
Thank you for the help!
I have an asp.net 4.0 webpage which has 10 chart controls on it. I have been to email the chart controls to the current logged in user when they open the page. The chart controls will be different for each user. I have been testing this by trying to send 1 chart control but the body of the email doesnt show the chart only the image outline. I have tried several things but cant get it to work. The code i have just now is -
web.config
<add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true;"/>
webpage
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMail()
End Sub
Private Sub SendMail()
Dim SB As New StringBuilder()
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)
'SB.Append("<td><img src=""cid:chart17""></td>")
Chart10.RenderControl(htmlTW)
Dim MyHTML As String = SB.ToString()
Dim from As String = "EMAIL ADDRESS"
Dim recip As String = "EMAIL ADDRESS"
'Dim recip As String = Membership.GetUser.Email.ToString
Dim subject As String = "Test Email"
'Create message object and populate w/ data from form
Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
message.From = New System.Net.Mail.MailAddress(from.Trim())
message.To.Add(recip.Trim())
message.Subject = subject.Trim()
message.IsBodyHtml = True
message.Body = MyHTML
'Setup SmtpClient to send email. Uses web.config settings.
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
'Error handling for sending message
Try
smtpClient.Send(message)
'Exception contains information on each failed receipient
Catch recExc As System.Net.Mail.SmtpFailedRecipientsException
For recipient = 0 To recExc.InnerExceptions.Length - 1
Dim statusCode As System.Net.Mail.SmtpStatusCode
'Each InnerException is an System.Net.Mail.SmtpFailed RecipientException
statusCode = recExc.InnerExceptions(recipient).StatusCode
If (statusCode = Net.Mail.SmtpStatusCode.MailboxBusy) Or (statusCode = Net.Mail.SmtpStatusCode.MailboxUnavailable) Then
'Log this to event log: recExc.InnerExceptions(recipient).FailedRecipient
System.Threading.Thread.Sleep(5000)
smtpClient.Send(message)
Else
'Log error to event log.
'recExc.InnerExceptions(recipient).StatusCode or use statusCode
End If
Next
'General SMTP execptions
Catch smtpExc As System.Net.Mail.SmtpException
'Log error to event log using StatusCode information in
'smtpExc.StatusCode
Catch ex As Exception
'Log error to event log.
End Try
End Sub
As you can see i have tried some examples on forums like "SB.Append" and "chart10.rendercontrol(htmlTW) but both do not work for me.
Any helpo would be greatful.
You are not attaching the image to the e-mail.
''I am not sure how to handle memory streams in vb but it should be something like so.
Dim ms as MemoryStream = new MemoryStream()
Chart10.SaveImage(ms, ChartImageFormat.Png)
Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(ms, "MyChart.png")
A.ContentId = "chart17"
A.ContentDisposition.Inline = True
message.Attachments.Add(A)