Hi,
I dont know if someone had encountered this problem.
I used the code below to send email with a google account. I can send the email with attachment but a minor problem for recipient using gmail. I notice that the paper clip image or icon on the right side of the the gmail inbox list is missing (that indicates that there is an attachment on the email) although the email itself has an attachment. Theres no problem with yahoomail and hotmail.
Did i miss any in the code?
Thanks a lot.
Yuan
Private Function SendEmail(ByVal mRecipient As String, ByVal mSubject As String, ByVal mContent As String) As Boolean
Dim MyMailMessage As New MailMessage()
Dim mPlainText As String = ""
MyMailMessage.From = New MailAddress(getAccount("emailaccount"), getAccount("sender"))
MyMailMessage.To.Add(New MailAddress(mRecipient))
MyMailMessage.Subject = mSubject
'MyMailMessage.Body = mContent
MyMailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8")
'first we create the Plain Text part
mPlainText = stripHTML(mContent)
If mPlainText = Nothing Then
mPlainText = "The email sent is in html format."
End If
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(mPlainText, Nothing, "text/plain")
'then we create the Html part
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(mContent, Nothing, "text/html")
MyMailMessage.AlternateViews.Add(plainView)
MyMailMessage.AlternateViews.Add(htmlView)
MyMailMessage.IsBodyHtml = True
If Me.txtAttachment.Text <> "" Then
'For counter = 0 To ListBox1.Items.Count - 1
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(Me.txtAttachment.Text.Trim)
MyMailMessage.Attachments.Add(Attach)
'Next
End If
Dim SMTPServer As New SmtpClient(getAccount("smtpserver"))
If getAccount("port") <> "" Then
SMTPServer.Port = CInt(getAccount("port"))
End If
SMTPServer.Credentials = New System.Net.NetworkCredential(getAccount("emailaccount"), getAccount("password"))
SMTPServer.EnableSsl = CBool(getAccount("isSSL"))
Try
SMTPServer.Send(MyMailMessage)
'MessageBox.Show("Email Sent")
Return True
Catch ex As SmtpException
MsgBox("" & ex.Message & " ", MsgBoxStyle.OkOnly, "Error on sending email.")
Return False
End Try
End Function
I dont know if someone had encountered this problem.
I used the code below to send email with a google account. I can send the email with attachment but a minor problem for recipient using gmail. I notice that the paper clip image or icon on the right side of the the gmail inbox list is missing (that indicates that there is an attachment on the email) although the email itself has an attachment. Theres no problem with yahoomail and hotmail.
Did i miss any in the code?
Thanks a lot.
Yuan
Private Function SendEmail(ByVal mRecipient As String, ByVal mSubject As String, ByVal mContent As String) As Boolean
Dim MyMailMessage As New MailMessage()
Dim mPlainText As String = ""
MyMailMessage.From = New MailAddress(getAccount("emailaccount"), getAccount("sender"))
MyMailMessage.To.Add(New MailAddress(mRecipient))
MyMailMessage.Subject = mSubject
'MyMailMessage.Body = mContent
MyMailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8")
'first we create the Plain Text part
mPlainText = stripHTML(mContent)
If mPlainText = Nothing Then
mPlainText = "The email sent is in html format."
End If
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(mPlainText, Nothing, "text/plain")
'then we create the Html part
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(mContent, Nothing, "text/html")
MyMailMessage.AlternateViews.Add(plainView)
MyMailMessage.AlternateViews.Add(htmlView)
MyMailMessage.IsBodyHtml = True
If Me.txtAttachment.Text <> "" Then
'For counter = 0 To ListBox1.Items.Count - 1
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(Me.txtAttachment.Text.Trim)
MyMailMessage.Attachments.Add(Attach)
'Next
End If
Dim SMTPServer As New SmtpClient(getAccount("smtpserver"))
If getAccount("port") <> "" Then
SMTPServer.Port = CInt(getAccount("port"))
End If
SMTPServer.Credentials = New System.Net.NetworkCredential(getAccount("emailaccount"), getAccount("password"))
SMTPServer.EnableSsl = CBool(getAccount("isSSL"))
Try
SMTPServer.Send(MyMailMessage)
'MessageBox.Show("Email Sent")
Return True
Catch ex As SmtpException
MsgBox("" & ex.Message & " ", MsgBoxStyle.OkOnly, "Error on sending email.")
Return False
End Try
End Function