SMTP outgoing server does not send text inside the email by vb.net - vb.net

I developed apart of an application that sends emails to partners and clients with a saved email subject and text; so i used the vb.net settings to declare variables to be initiated with the program. and then asked the user to save his email subject and body to be used again later.
the problem is when i attemped to send the email, the email reaches the otherside yet with no content at all ( mail subject and body are null).
some points may be taken into consideration:
i used background worker (thread dirrent than the UI thread)
the outgoing mail is SMTP (Gmail server).
I passes the Body and the subject as strings to the sub sendmail().
Is there any better method to store mail's body and subject instead of string variables ?
I am sure that the SMTP took all my strings
Sub sendmail(receiver As String, mailsubject As String, mailbody As String, mailattachement As String)
Dim email As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
Try
With smtp
.EnableSsl = My.Settings.mailssl
.Port = My.Settings.mailport
.Host = My.Settings.mailhost
.Credentials = New Net.NetworkCredential(My.Settings.mailaddress, My.Settings.mailpassword)
End With
With email
.From = New System.Net.Mail.MailAddress(My.Settings.mailaddress)
.Subject = severalclientmailsubject
.Body = severalclientmailbody
.To.Add(receiver)
End With
smtp.Send(email)
Catch ex As Exception
'Generic Exception raised.
changestatus2("General Error.")
End Try
End Sub
what could can cause this thing ?!?

Related

VB.Net Email not putting copy in sent folder

I'm going mad here!
Sending emails in VB.Net is working fine, even the read/delivery receipts are working. What it's not doing, is putting a copy of the email it sends in the sent folder. Is there something else I need to do as I've searched high a low for a solution but can't see anything.
EMail.From = New MailAddress(sSENDERaddress)
EMail.Body = sMessage
EMail.Subject = sSubject
If sAttached <> "" Then
Dim mAttachment As New Attachment(sAttached)
EMail.Attachments.Add(mAttachment)
End If
EMail.Headers.Add("Return-Receip-To", sSENDERaddress)
EMail.Headers.Add("Disposition-Notification-To", sSENDERaddress)
EMail.Headers.Add("Return-Path", sSENDERaddress)
EMail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure Or DeliveryNotificationOptions.OnSuccess ' this will send an email to the SENDER'S email address confirming that the original email was sent. If the sender doesn't get the email, then it didn't go. Simples
SMTPServer.Host = GetMailServerAddress()
SMTPServer.Port = GetMailPort()
SMTPServer.Credentials = Authentication
Try
SMTPServer.Send(EMail)
'EMail.Dispose()
bRET = True
Catch ex As Exception
''debug.Print(ex.Message)
ExceptIt(ex.Message)
'EMail.Dispose()
bRET = False
End Try
I've found a few more posts on this subject, and it looks like you can't do it. The only Heath Robinson solution is to add a BCC to my sending address...
UPDATE: For those interested, it's not an SMTP function, but an IMAP one. I suppose, if you want to, you can spend hours working the code out for this, but I found this
https://www.example-code.com/vbnet/sendWithCopyToSentMailbox.asp
Where you simply append to the sent items...
' Now use Chilkat IMAP to save the email to Inbox.Sent
Dim imap As New Chilkat.Imap
' Connect to an IMAP server.
' Use TLS
imap.Ssl = True
imap.Port = 993
success = imap.Connect("mail.mydomain.com")
If (success <> True) Then
Console.WriteLine(imap.LastErrorText)
Exit Sub
End If
' Login
success = imap.Login("myLogin","myPassword")
If (success <> True) Then
Console.WriteLine(imap.LastErrorText)
Exit Sub
End If
' The AppendMail method uploads an email to an IMAP server
' and saves it in the mailbox specified:
success = imap.AppendMail("Inbox.Sent",email)
If (success <> True) Then
Console.WriteLine(imap.LastErrorText)
Exit Sub
End If
Console.WriteLine("Mail saved to Inbox.Sent")

VB Authenticate User In Outlook Web App

I currently have a mail system using Microsoft's exchange server (OWA). I am trying to authenticate a user and send an email using pure Visual Basic code.
I have been trying to use a library called Aspose, however; I have no idea if I'm on the right track. I can not get it to work and I am not sure (since this is a company mail server) whether it's the server or it's my code.
Currently I have,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create instance of ExchangeClient class by giving credentials
Dim client As Aspose.Email.Exchange.ExchangeClient = New Aspose.Email.Exchange.ExchangeClient("https://MAILSERVER.com/username/", "username", "password", "https://MAILSERVER.com/")
' Create instance of type MailMessage
Dim msg As Aspose.Email.Mail.MailMessage = New Aspose.Email.Mail.MailMessage()
msg.From = "username#MAILSERVER.com"
msg.To = "receivingemail#gmail.com"
msg.Subject = "test"
msg.HtmlBody = "test"
' Send the message
Try
client.Send(msg)
Console.WriteLine("made it")
Catch ex As Exception
Console.WriteLine("failed")
End Try
End Sub
I have obviously changed the username, password, and server name fields to generic ones but with (what I think is the right credentials) the output is always failed.
Can anybody help me out please?
Here is what I use:
Public Shared Function SendEMail(MailMessage As System.Net.Mail.MailMessage) As String
ErrorMess = ""
' Default the from address, just in case is was left out.
If MailMessage.From.Address = "" Then
MailMessage.From = New Net.Mail.MailAddress("donotreply#MAILSERVER.com")
End If
' Check for at least one address
If MailMessage.To.Count = 0 AndAlso MailMessage.CC.Count = 0 AndAlso MailMessage.Bcc.Count = 0 Then
ErrorMess = "No Addresses Specified"
Return ErrorMess
End If
' Create a SMTP connedction to the exchange 2010 load balancer.
Dim SMTPClient As New System.Net.Mail.SmtpClient("MAILSERVER.com")
Try
Dim ValidUserCredential As New System.Net.NetworkCredential
ValidUserCredential.Domain = "MAILSERVER.com"
ValidUserCredential.UserName = My.Resources.EmailUserName
ValidUserCredential.Password = My.Resources.EmailPassword
SMTPClient.UseDefaultCredentials = False
SMTPClient.Credentials = ValidUserCredential
SMTPClient.Send(MailMessage)
Return "Mail Sent"
Catch ex As Exception
ErrorMess = ex.Message & " " & ex.InnerException.ToString
Return ErrorMess
End Try
End Function
The ExchangeClient class is used to connect to Exchange server using the WebDav protocol and is used with Exchange Server 2003 and 2007. For OWA, you need to use the IEWSClient interface as shown in the following sample code. It has an Office365 test account that you can use to send a test email (the test account is solely for testing purpose and is not property of Aspose. I just created it for assisting you in testing the functionality). Please try it and if you face any problem, you may share the porblem on Aspose.Email forum for further assistance.
' Create instance of IEWSClient class by giving credentials
Dim client As IEWSClient = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "UserTwo#ASE1984.onmicrosoft.com", "Aspose1234", "")
' Create instance of type MailMessage
Dim msg As New MailMessage()
msg.From = "UserTwo#ASE1984.onmicrosoft.com"
msg.[To] = "receiver#gmail.com"
msg.Subject = "Sending message from exchange server"
msg.IsBodyHtml = True
msg.HtmlBody = "<h3>sending message from exchange server</h3>"
' Send the message
client.Send(msg)
I work with Aspose as Developer evangelist.

Validate email address on exchange server

I'm allowing users to manage a distribution list stored in a database. Users are only allowed to enter emails that are #mydomain.com. A web based application then takes the distribution list and sends emails. I'd like to validate that the email is valid before sending an email from the application.
To send an email I'm using this code:
Dim SendTo As String = "ThisIsNotARealEmailAddress#mydomain.com"
Dim SentFrom As String = "me#mydomain.com"
Dim MessageBody As String = "blah blah blah"
Dim MessageSubject As String = "This is the subject"
Dim mm As New MailMessage(SentFrom, SendTo)
mm.Subject = MessageSubject
mm.IsBodyHtml = False
mm.Priority = MailPriority.High
mm.Body = MessageBody
Dim smtp As New SmtpClient()
smtp.Send(mm)
If the SendTo is not a valid email address the server returns this error:
Mailbox unavailable. The server response was: 5.1.1 <ThisIsNotARealEmailAddress#mydomain.com>... User unknown
Is there anyway to validate the email when the email address is added to the database, instead of a try catch block when sending the email?
The users are only sending to your domain? And you control the domain? And its a windows domain? Just query the AD and get their email address from the AD without asking them. Would that be valid? I presume this is a windows application, not a web app.

Getting Error while sending the email in .NET

I have an application was working fine and all the email functionality was working fine.
From yesterday, I started getting below error
Error Message:Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel
My VB.net code for sendmail is given below:
Public Sub SendMessage(ByVal toAddress As String, ByVal ccAddress As String)
Try
Dim message As New MailMessage()
Dim client As New SmtpClient()
'Set the sender's address
message.From = New MailAddress(fromAddress)
If (toAddress.Trim.Length > 0) Then
For Each addr As String In toAddress.Split(";"c)
message.To.Add(New MailAddress(addr))
Next
End If
If (ccAddress.Trim.Length > 0) Then
For Each addr As String In ccAddress.Split(";"c)
message.CC.Add(New MailAddress(addr))
Next
End If
message.BodyEncoding = Encoding.UTF8
message.Subject = Subject
message.Body = Body
message.IsBodyHtml = True
client.Send(message)
Catch ex As Exception
ErrorHandler.WriteError(ex.Message)
End Try
End Sub
Please suggest what can be cause behind this error and do let me know how can I fix this issue.
There isn't anything wrong with your code. This part of the error message:
4.3.2 Service not available, closing transmission channel
Is actually coming from your mail server, and the framework is simply passing the error message on to your application, and throwing it as part of the exception.
4.x.x errors are usually temporary, and are meant to be retried. Typically mail servers are overloaded when they throw a 400 error.
Check your email and see if it starts working.
Instead of (or in addition to) smtp authentication, some email servers will allow mail to be sent for 30 minutes (or some length) after a client computer checks POP email. If this is the case, it can make an application that does not use smtp authentication appear work sometimes and not work sometimes, with no change in the code.

Email Function in VB

I need a simple email function that just sends an email (not looking to spam anyone I promise!).
Anyone, anyone? Using VB 2008
Use the SmtpClient class to do this. There's an example of sending an email asynchronously on the documentation page, but here's a basic way of doing it:
Dim client As New SmtpClient("mail.myisp.com")
Dim fromAddr As New MailAddress("jane#contoso.com")
Dim toAddr As New MailAddress("ben#contoso.com")
Dim message As New MailMessage(fromAddr, toAddr)
message.Body = "This is a test e-mail message sent by an application. "
message.Subject = "test message 1"
client.Send(message)