Email Function in VB - vb.net

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)

Related

vb.ne send email from a shared mailbox

i'm supposed to send an email as customer care from a shared mailbox to several addresses of my company customers.
i'm using mailkit library and i use this code
Code:
Dim contenutomail As New MimeKit.BodyBuilder
Dim smtpclient As New MailKit.Net.Smtp.SmtpClient
Dim mail As New MimeKit.MimeMessage
smtpclient.Connect("smtp.office365.com", 587, MailKit.Security.SecureSocketOptions.StartTls)
smtpclient.Authenticate("eurostar_italia#mycompany.com", "Aprile22")
mail.From.Add(New MimeKit.MailboxAddress("", "customeroffice#mycompany.com"))
For i = 0 To mailinglist.Count - 1
mail.Bcc.Add((New MimeKit.MailboxAddress("",mailinglist(i).ToString)))
Next
contenutomail.TextBody = ""
contenutomail.HtmlBody = "hello world"
mail.Body = contenutomail.ToMessageBody
smtpclient.Send(mail)
smtpclient.Disconnect(True)
the problem is : sent mail are save in MY personal account SENT mailbox instead of shared SENT mailbox.
I don't have any Admin right to modify the office365 sharedmailbox.
can anyone help me ? i don't want to save a copy,i want the mail sent to be saved in the sent shared mailbox
i can't solve it
thanks
fabio

Forwarding OpenPOP Attachments to SMTP (System.Net.Mail)

I have an application I have built in Visual Studio using VB.NET that pulls mail messages from an Outlook mailbox and saves the message information into a database and downloads the attachments (if any) into a folder. Before saving the mail message to my database, if the message is from a certain user, I forward the message to another mailbox (and thus, I don't save the message). This is all working fine, except when I try to forward a message with attachments.
As stated in the title, I am using OpenPOP to pull the mail and SMTP to transfer the mail. When I try to create the SMTP attachment from the OpenPOP message I get the following error:
System.InvalidCastException: Conversion from type 'MessagePart' to type 'String' is not valid.
The message is thrown in the AddAttachments function (below) on the line:
myAttachment = New Attachment(attachment) (in the For Each statement)
Public Sub ForwardMessage(
ByVal msgPOP As OpenPop.Mime.Message,
toAddress As String,
fromAddress As String,
subject As String,
body As String
)
Dim smtpServer As New System.Net.Mail.SmtpClient(Me.serverName)
Dim msgSMTP As New MailMessage()
msgSMTP.Sender = New MailAddress(fromAddress)
msgSMTP.To.Add(New MailAddress(toAddress))
msgSMTP.Subject = subject
msgSMTP.Body = body
msgSMTP.IsBodyHtml = True
Dim attachments As Object
attachments = AddAttachments(msgPOP, msgSMTP)
msgSMTP.Attachments.Add(New Attachment(attachments))
smtpServer.Send(msgSMTP)
End Sub
I finally figured it out with some help from a friend. The AddAttachments function below has been edited to show the fix.
Public Function AddAttachments(
ByVal msgPOP As OpenPop.Mime.Message,
ByVal msgSMTP As MailMessage
) As MailMessage
Dim attachments As Object = msgPOP.FindAllAttachments()
Dim myAttachment As Attachment = Nothing
For Each attachment As OpenPop.Mime.MessagePart In attachments
Dim sName As String = attachment.FileName
Dim sContentType As String = attachment.ContentType.MediaType
Dim stream As MemoryStream = New MemoryStream(attachment.Body)
myAttachment = New Attachment(stream, sName, sContentType)
msgSMTP.Attachments.Add(myAttachment)
Next
Return msgSMTP
End Function
I have spent hours researching this issue and I have not found one solution yet. I tried changing the application data type to String and OpenPOP.MIME.MessagePart to no avail. I tried adding "ToString" to the attachment variable and received the following error:
System.InvalidCaseException: Operator '&' is not defined for type 'MessagePart' and string ".ToString".
I have been reading up on MIME to see if that would offer some ideas, though I have not been able to connect the dots. I am assuming this is possible and hoping someone will be able to share the solution, and I will be happy with either VB.NET or C#.NET.
Thank you very much in advance and I appreciate your time.
The solution is in the edited AddAttachments function in my original post above.

Outlook and Office 365 does not show sent items where mails were sent using a external software which is developed using vb.net

We have a mail-id in our own domain.
We configure it in outlook along with office 365.
We have using a software for our business purpose which is developed using vb.net.
Using this software, we are sending mails to our customers from the mail-id of our own (For Example user#mydomain.in).
Customers also receive the mail properly.
The problem is that we can't able to see the sent mails in our "sent items" of both outlook and office365.
Sent items does not sync.
Dim mailbody As String = ""
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("user#mydomain.in", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.office365.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress("user#mydomain.in")
Dim attachment As System.Net.Mail.Attachment
e_mail.To.Add(New_Enquiry.MAIL_ID.Text)
e_mail.Subject = "Thankyou for your Enquiry"
e_mail.IsBodyHtml = True
Smtp_Server.Send(e_mail)
These are the code which I have used in vb.net.
It works properly but unable to see the sent mails in sent items.
I had used the same code for gmail also and Gmail shows the sent mails in sent items.
Kindly spot me the error.
Gmail is special: anything you send through their SMTP server will show up in your Sent folder. This is not standard behaviour: if you want messages to show up in your Sent folder, you have to PLACE them there, generally using IMAP and the APPEND command. If you have the right access, you may be able to make some sort of server side script to do this for you, but it is not the default.

SMTP outgoing server does not send text inside the email by 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 ?!?

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.