Question send a mail via smtp

yakir

Member
Joined
Jul 25, 2010
Messages
5
Programming Experience
Beginner
I use in vb.net 2008 and outlook 2007
I want to send mail in my work via the work net to other workers by smtp/outlook


this is very importent to me and im appreciate your help

thanks
yakir
 
Last edited:

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
Welcome to the forum.

If what you want to do is send an email through SMTP (this code doesn't integrate with Outlook), then I would suggest having a look at my website or more specifically the class that I have created to make it easier for people to send emails via SMTP which is available at Sending an email from a .Net application.
You can this post that explains how to use the class.

Anyway I hope that this helps

Satal :D
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Import the System.Web.Mail namespace and all you need is 2 objects: the mail message and the smtp client. MailMessage is the class that holds the message and there's an Smtp class that you can send the message.

Edit, or just follow Satal Keto's advice since he beat me to it and has a more detailed description of what to do.
 

yakir

Member
Joined
Jul 25, 2010
Messages
5
Programming Experience
Beginner
im very appreciate your help and your quickly anser, when i saw the long answer i was in shock for few days :)
i have no idea how to use in class above,following my try:
Dim email As Email = New Email("subject", "user", "password", "message", "host")
email.Tos("******@hotmail.com", "*******@gmail.com")
email.CCs("******@yahoo.com", "*******@gmail.com")
email.sendEmail()
i wrote my try in windows form and your answer in item of class
i need help , i have waiting for instuction
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
A good resource for .Net mail classes: System.Net.Mail, OH MY!

Though that will not help you send mail via Outlook, to do that you have to do Office automation (restricted by security measures), or use the mailto protocol or commandline arguments to the Outlook .exe, latter both which can create a new message that you have to manually click 'send' button on. The .Net SmtpClient and MailMessage classes act as an independent mail client, but you have to configure to a valid Smtp server to send.
 

yakir

Member
Joined
Jul 25, 2010
Messages
5
Programming Experience
Beginner
sorry but i know that im missing something
i copy your first link to one form like this:
Imports System.Net.Mail
Namespace Email
Public Class Email
rest data...
end class

i copy your last link to a new form:
Imports System.Net.Mail
Public Class sub_capa
Dim em As New Email(...
Dim res As Email.sendEmailResult
rest your data...
end class

my errors:
Dim em As New Email - type expected
Dim res As Email.sendEmailResult - not define
sendEmailResult - not member of mail

i thing my imports or somthing else doesn't work
what do you think?
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
You need to create a new class in your project called Email, remove all the automatically generated code and then paste the code from my website into that class.
Then in your form you would put this code;
Code:
        Dim em As New Email("SMTP Host", "SMTP User", "SMTP Password")
        Dim res As Satal.Email.Email.sendEmailResult

        em.SMTPPort = 587
        em.UseEncryption = True
        em.Tos.Add("target email address") 'You can add many of these if you want
        em.subject = "This is a test subject"
        em.message = "This is a test message" & Environment.NewLine & "Yay new line"
        If em.addAttachment("C:\Users\Satal\Desktop\test.txt") Then
            res = em.sendEmail()

            Select Case res
                Case Email.sendEmailResult.attachmentNotAvailable
                    MsgBox("The attachment can't be found")
                Case Email.sendEmailResult.noMessage
                    MsgBox("Whats the point of sending an email with no message?")
                Case Email.sendEmailResult.noSMTPDetails
                    MsgBox("You need to supply the details for your SMTP server")
                Case Email.sendEmailResult.noSubject
                    MsgBox("You need to specify a subject")
                Case Email.sendEmailResult.noToEmails
                    MsgBox("You have to specify someone to send to")
                Case Email.sendEmailResult.successful
                    MsgBox("You've got spam")
                Case Email.sendEmailResult.unableToConnect
                    MsgBox("Unable to connect to SMTP server")
                Case Email.sendEmailResult.unknownError
                    MsgBox("Ow no, what went wrong?")
            End Select
        End If
You will need to make some adjustments to the code that you have put in the form but the code that you have put into the class is fine and does not require you to change anything.

Satal
 

yakir

Member
Joined
Jul 25, 2010
Messages
5
Programming Experience
Beginner
my last try,if it will not work i'll leave it.

my adjustments(without attachment)--it's the only one option i dont get any warning
Dim em As New Email.Email("mail.****.co.il", "yakit@****.co.il", "sisma") ' i add additional email
Dim res As Email.Email.sendEmailResult 'add additional email instead satal
em.SMTPPort = 587
em.UseEncryption = True
em.Tos.Add("miki@****.co.il")
em.subject = "the task is allocated until 30/9/2010"
res = em.sendEmail()

its running and it stuck in the class in this part:
Catch smtpEx As SmtpException
emailSent = False
mailClient.Send(mail) ----> this get failure in this part
sorry about all the questions and thank you anyway
 
Last edited:

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
When you're posting code please can you use the code tags, it makes it much easier for everyone else to read.

Are you sure that the port that you're using is the correct port? You would only be getting an SmtpException if the class is unable to connect to the SMTP server. So either it cannot connect to the server itself (by either the host being wrong, the port being wrong or there being some firewall stopping the code from connecting) or the user details provided to login aren't correct, but as I'm sure you've checked that you have the right username and password the first option seems the most likely.

Satal
 

yakir

Member
Joined
Jul 25, 2010
Messages
5
Programming Experience
Beginner
I will try your suggests, anyway i'm appreciate your help.
thanks a lot and hope i didn't waste your time

yakir
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
No worries, I'm always happy to try and help people out with using my code as then if there is something wrong with the code I can make adjustments to it appropriately.
For example someone pointed out to me that apparently the username for the SMTP doesn't have to be the email address that you're sending from, so at some point I will be able to make that adjustment to my code (although can't do it yet as I'm currently doing some last minute coursework :p).
 
Top Bottom