Click here to Skip to main content
14,336,236 members
Rate this:
Please Sign up or sign in to vote.
See more:
Does anyone know have any third party freeware can replace EASendMailObj.Mail? Or any method to use the EASendMailObjLib.Mail become freeware? Because now..I use this to send email,the system say Smtp.LicenseCode = "TryIt" has expired already.The following is my code:

Dim Smtp As New EASendMailObjLib.Mail
                    Smtp.LicenseCode = "TryIt"

                    Smtp.FromAddr = eid  '"ab@mail.com"
                    Smtp.AddRecipientEx(email, 0)

                    Smtp.Subject = "abcde"
                    Smtp.BodyText = "abcde"

                    '' Add attachment from local disk
                    If Smtp.AddAttachment(pdfFile) <> 0 Then
                        MsgBox("Failed to add attachment with error:" & Smtp.GetLastErrDescription())
                    End If

                    Smtp.ServerAddr = server '"smtp.gmail.com"
                    Smtp.ServerPort = port ' 465

                    '' detect SSL/TLS automatically
                    Smtp.SSL_init()

                    Smtp.UserName = eid '"ab@mail.com"
                    Smtp.Password = pass '"123"

                    If Smtp.SendMail() = 0 Then
                        '' MsgBox("email was sent successfully!")
                        Label1.Text = "1"
                    Else
                        MsgBox("failed to send email with the following error:" & Smtp.GetLastErrDescription())
                    End If
                Else
                End If
            Next
            If Label1.Text = "1" Then
                MsgBox("email was sent successfully!")
            End If
            Label1.Text = ""
        End If

I solve this problem about 1 week already,but also cannot solve it.Hope u guys can help me,really headache for this part.
Thanks you in advance.

[Edit]Code block formatting added[/Edit]
Posted
Updated 17-Sep-19 3:09am
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

You really don't need to use a third-party library to send an email. Just use a System.Net.Mail.SmtpClient:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx[^]
Sending an Email in C# with or without attachments: generic routine.[^] (C#)
Sending an Email in C# with or without attachments: generic routine.[^] (VB.NET)
Example:
Imports System.Net
Imports System.Net.Mail   ' add these lines at the top of your file
Dim Smtp As New SmtpClient(server, port)
Dim msg As New MailMessage()
msg.From = New MailAddress(eid)
msg.[To].Add(email)
msg.Subject = "abcde"
msg.Body = "abcde"
msg.IsBodyHtml = True
' if your email body is not HTML, change this into false or remove this line
msg.Attachments.Add(New Attachment(pdfFile))
Smtp.EnableSsl = True
Smtp.Credentials = New NetworkCredential(eid, pass)
Try
	Smtp.Send(msg)
	Label1.Text = "1"
Catch e As Exception
	MsgBox("failed to send email with the following error:" + e.Message)
End Try
' now, add other code if you want

Hope this helps.
   
v2
Comments
Adrian4263 24-Jun-13 22:37pm
   
Do not need to use third-party library?Can Show me a simple example? Or modify my code above? Because the link you given,I'm not understand so much.Thanks
Thomas Daniels 25-Jun-13 2:45am
   
I added an example.
Adrian4263 25-Jun-13 2:56am
   
Okay,I have a question.What server part should I write?Smtp?
Thomas Daniels 25-Jun-13 2:58am
   
Yes, in your case "smtp.gmail.com"
Adrian4263 25-Jun-13 3:05am
   
Okay. msg.From = New MailAddress(eid) <<The meail address is write my e-mail address is it?And Smtp.Credentials = New NetworkCredential (eid, pass)<< my id n password replace the NetworkCredential or replace in the bracket?
Thomas Daniels 25-Jun-13 3:13am
   
The msg.From address is the address from which the mail will be sent: your address.

Smtp.Credentials = New NetworkCredential(eid, pass) is the non-third party version of:
<small>
Smtp.UserName = eid '"ab@mail.com"
Smtp.Password = pass '"123"
</small>
Adrian4263 25-Jun-13 3:21am
   
That mean I no need write any address in the msg.From address,because those I want to send mail to them is read from database.

Smtp.Credentials = New NetworkCredential(eid, pass),so I want replace my e-mail to eid and pass?
Thomas Daniels 25-Jun-13 3:24am
   
What do you mean exactly?
You should always provide the password of the email address from which you send an email.
Adrian4263 25-Jun-13 3:28am
   
Yup,I know it.What I mean is where should I write the sender email address and password.
Thomas Daniels 25-Jun-13 3:30am
   
The email address in the <small>eid</small> variable and the password in the <small>pass</small> variable.
Adrian4263 25-Jun-13 3:34am
   
Okay,I got it.I go try and see,I think this is helpful and thanks you a lot :)
Thomas Daniels 25-Jun-13 3:35am
   
You're welcome!
Adrian4263 25-Jun-13 3:54am
   
adrian_leong90@hotmail.com this is my email, can you inbox me,I got some question to ask you.Thanks
Thomas Daniels 25-Jun-13 4:00am
   
Why can't ask you here?
Adrian4263 25-Jun-13 4:02am
   
Because the code is quite long. Is this reasonable?
Thomas Daniels 25-Jun-13 4:04am
   
You may ask it here, even if the code is long. But always provide the shortest snippet as possible.
Adrian4263 25-Jun-13 4:12am
   
Roger,firstly, my email button code got 2 part,the part I shown above which is Part 2,just now I try the code got error, so i gonna show you the whole email button code.Hope you can help me solve the error.
Thomas Daniels 25-Jun-13 4:15am
   
If you show me the code, I'll try to find the error.
Adrian4263 25-Jun-13 4:24am
   
The following is my email button code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dsd As New DataSet
Dim sql As String = "Select * from email"
Dim cmd As New SqlCommand(sql, connect)
dAdapter.SelectCommand = cmd
dsd.Reset()
dAdapter.Fill(dsd)

If dsd.Tables(0).Rows.Count > 0 Then
Dim type = dsd.Tables(0).Rows(0).Item("type")
Dim eid = dsd.Tables(0).Rows(0).Item("id").ToString.Trim
Dim pass = dsd.Tables(0).Rows(0).Item("pass").ToString.Trim
Dim port = dsd.Tables(0).Rows(0).Item("port").ToString.Trim
Dim server = dsd.Tables(0).Rows(0).Item("server").ToString.Trim

MsgBox("start to send email ...")

For i = 0 To dSet.Tables(0).Rows.Count - 1
Dim email As String = dSet.Tables(0).Rows(i).Item("email").ToString.Trim
If email <> "" Then
Dim id As String = dSet.Tables(0).Rows(i).Item("empid").ToString

Dim CrExportOptions As New ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim crformatTypeOptions As New PdfRtfWordFormatOptions()
CrDiskFileDestinationOptions.DiskFileName = _
ConfigurationManager.AppSettings("pypath")
CrExportOptions = obj.ExportOptions
crformatTypeOptions.FirstPageNumber = i
crformatTypeOptions.LastPageNumber = i
crformatTypeOptions.UsePageRange = True
With CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = crformatTypeOptions
End With
obj.Export()

'_________________________________________________________________________________
Dim Smtp As New EASendMailObjLib.Mail
Smtp.LicenseCode = "TryIt"

Smtp.FromAddr = eid '"ab@mail.com"
Smtp.AddRecipientEx(email, 0)

Smtp.Subject = "aabb"
Smtp.BodyText = "aabb"

'' Add attachment from local disk
If Smtp.AddAttachment(pdfFile) <> 0 Then
MsgBox("Failed to add attachment with error:" & Smtp.GetLastErrDescription())
End If

Smtp.ServerAddr = server '"smtp.gmail.com"
Smtp.ServerPort = port ' 465

'' detect SSL/TLS automatically
Smtp.SSL_init()

Smtp.UserName = eid '"ab@mail.com"
Smtp.Password = pass '"123"

If Smtp.SendMail() = 0 Then
'' MsgBox("email was sent successfully!")
Label1.Text = "1"
Else
MsgBox("failed to send email with the following error:" & Smtp.GetLastErrDescription())
End If
Else
End If
Next
If Label1.Text = "1" Then
MsgBox("email was sent successfully!")
End If
Label1.Text = ""
End If
'____________________________________________________________________________________________________________

End Sub
Thomas Daniels 25-Jun-13 4:25am
   
And which error do you get?
Adrian4263 25-Jun-13 4:35am
   
I amend part 2 code,after that I want run the program,pop up there were build error,suddenly appear 12 error come up at the other part,its under the word got red line.For example, this page code have 11 pass <<word,11 pass word got the red line under the word.
Thomas Daniels 25-Jun-13 4:37am
   
What's the error message?
Adrian4263 25-Jun-13 4:44am
   
Please wait for awhile,I go try again see whether can run.
In the bracket write email address and password right? ("myemail@.hotmail.com", "password") need to put "" <<this?
Thomas Daniels 25-Jun-13 4:51am
   
But if you use a Hotmail address, then don't use smtp.gmail.com as SMTP server. The SMTP server for Hotmail is smtp.live.com
More information about Hotmail SMTP configuration:
http://www.serversmtp.com/en/smtp-hotmail

And yes, you should write the email address and password between ( and )
Adrian4263 25-Jun-13 4:58am
   
("MyEmail@hotmail.com", "MyPassword") or ("MyEmail@hotmail.com" and "MyPassword")?

Then my server should write ("Smtp.live.com", 465) p/s:465 is the port.
Thomas Daniels 25-Jun-13 5:00am
   
Use the first: ("MyEmail@hotmail.com", "MyPassword")

And if you want to use Hotmail, then you should create your SmtpClient this way:

Dim Smtp As New SmtpClient("smtp.live.com", 465)
Adrian4263 25-Jun-13 5:05am
   
SmtpClient Smtp = new SmtpClient("smtp.live.com", 465) where the code should me put?
Under the Dim Smtp As New SmtpClient("Smtp.live.com", 465)?
Thomas Daniels 25-Jun-13 5:08am
   
No, you should replace
Dim Smtp As New SmtpClient("smtp.gmail.com", 465)
with:
Dim Smtp As New SmtpClient("smtp.live.com", 465)
Adrian4263 25-Jun-13 5:14am
   
Okay,I test the code just now.
msg.From = New MailAddress(eid)<<this line pop up msg said The parameter 'address' cannot be an empty string.
Parameter name: address.
Thomas Daniels 25-Jun-13 5:21am
   
Hi,

Change eid into a string within your email address.
Adrian4263 25-Jun-13 5:36am
   
I tried it,can't.I amend eid to ("adrian_leong90@hotmail.com") it pop up a msgbox >>failed to send email with the following error: Failure sending mail.
Thomas Daniels 25-Jun-13 5:39am
   
Have you changed your credentials into:
Smtp.Credentials = new NetworkCredential("adrian_leong90@hotmail.com", "yourPassword")
Adrian4263 25-Jun-13 5:53am
   
My credentials is
Smtp.Credentials = new NetworkCredential("adrianleong263@hotmail.com", "MyPassword"),like that can't?
Adrian4263 25-Jun-13 5:57am
   
I already put the both place same email already,but it still the same,show me a msgbox >>failed to send email with the following error: Failure sending mail.
Thomas Daniels 25-Jun-13 6:03am
   
Is your attachment added successfully?
Adrian4263 25-Jun-13 21:09pm
   
Should be add successful.My part 1 code is export and attach file,do you see any problem?
Thomas Daniels 26-Jun-13 2:14am
   
Unfortunately, I don't see a problem. I don't know the contents of your database. Use a debugger to find the error.
Adrian4263 26-Jun-13 4:13am
   
How to use debugger to find the error?Now I have 0 error,I just debug the program click email button,then pop up a MsgBox("failed to send email with the following error:" + e.Message)failure sending mail replace with the e.Message.Cannot consider as error right?
Thomas Daniels 26-Jun-13 4:18am
   
If you use Visual Studio: place a breakpoint at the first line of your button click method. Press F5. Your form will appear. Click on the button. Then you'll go to the code file and you can step through all lines of the method to see what's wrong.
Adrian4263 26-Jun-13 22:50pm
   
Okay,finally I can send the email successful.I change he port from 465 to 25.Thanks you a lot.
But I still have a problem,for example,I have 10 people can select which is A1 to A10,after I select it will come up another page,example:I selected A1 to A1,it show 1 page,if only 1 page I click the email button it can sent successful.If I select A1 to A3,then it will come up 3 page,and I go click he email button it only can send the first page go out and then come out this following error:
Obj,Export<< this line of code error "The process cannot access the file 'C:\....\...\...\py1.pdf' Because it is being used by another process.
Adrian4263 27-Jun-13 0:38am
   
I've solved the Obj.Export error already.But I still have a small problem,that is PDF file,let' say 10 people,A1 to A10.I selected A1 ,come up 1 page,after that I click the email button it will export to PDF file first and attach send it out.This no problem.
If I select A1 to A3,it come up 3 pages,when I go click the email button,it should export 1 by 1 to the PDF file and send,but it wont,it combine the 3 pages in 1 PDF file.What is the problem?
Thomas Daniels 27-Jun-13 9:41am
   
Why don't you add multiple 'To' addresses? Just use the 'Smtp.To.Add' method multiple times.
Adrian4263 28-Jun-13 2:39am
   
Where this code should I put?
Thomas Daniels 28-Jun-13 2:41am
   
Under the 'msg.From = ...' line, add:
<small>
msg.To.Add('emailAddress1@example.com');
msg.To.Add('emailAddress2@example.com');</small>

Change these email addresses into the addresses you need.
Thomas Daniels 25-Jun-13 4:30am
   
You should change:
<small>

Dim Smtp As New EASendMailObjLib.Mail
Smtp.LicenseCode = "TryIt"

Smtp.FromAddr = eid '"ab@mail.com"
Smtp.AddRecipientEx(email, 0)

Smtp.Subject = "aabb"
Smtp.BodyText = "aabb"

'' Add attachment from local disk
If Smtp.AddAttachment(pdfFile) <> 0 Then
MsgBox("Failed to add attachment with error:" & Smtp.GetLastErrDescription())
End If

Smtp.ServerAddr = server '"smtp.gmail.com"
Smtp.ServerPort = port ' 465

'' detect SSL/TLS automatically
Smtp.SSL_init()

Smtp.UserName = eid '"ab@mail.com"
Smtp.Password = pass '"123"

If Smtp.SendMail() = 0 Then
'' MsgBox("email was sent successfully!")
Label1.Text = "1"
Else
MsgBox("failed to send email with the following error:" & Smtp.GetLastErrDescription())
End If
Else
End If
Next
If Label1.Text = "1" Then
MsgBox("email was sent successfully!")
End If</small>

into:
<small>
Dim Smtp As New SmtpClient(server, port)
Dim msg As New MailMessage()
msg.From = New MailAddress(eid)
msg.[To].Add(email)
msg.Subject = "abcde"
msg.Body = "abcde"
msg.IsBodyHtml = True
' if your email body is not HTML, change this into false or remove this line
msg.Attachments.Add(New Attachment(pdfFile))
Smtp.EnableSsl = True
Smtp.Credentials = New NetworkCredential(eid, pass)
Try
Smtp.Send(msg)
Label1.Text = "1"
Catch e As Exception
MsgBox("failed to send email with the following error:" + e.Message)
End Try</small>

and add
<small>
Imports System.Net.Mail
Imports System.Net
</small>
at the top of your code file.
Adrian4263 28-Jun-13 2:51am
   
I did it,now what I mean is,for example: 10 people/10 pages,i want export the 10 pages to the 10 PDF files,I do not want 10 pages export into 1 PDF file.You get what I mean?Let say, now I have 3 pages need to send to 3 people,and each people will get 1 page,and now my problem is 3 pages export into 1 PDF file and sent to 3 people,that mean each people get it 3 pages.
Thomas Daniels 28-Jun-13 2:52am
   
Then you should export each page to a seperate PDF, and send 10 different emails with different attachments.
Adrian4263 28-Jun-13 2:57am
   
Actually I do not know the code for export separate PDF.I have to find it out now.
Thomas Daniels 28-Jun-13 2:59am
   
You should use a third-party library to create PDF files. You can use iTextSharp, a .NET library to create PDF files:
http://sourceforge.net/projects/itextsharp/
Adrian4263 28-Jun-13 3:38am
   
You know what is the code? Itextsharp I never use before.
Thomas Daniels 28-Jun-13 3:42am
   
I'm sorry, I don't know the code. Actually I never used iTextSharp, but I know you can create PDF files using iTextSharp.
Adrian4263 28-Jun-13 3:49am
   
Oh I see,actually I can create PDF files now,just cannot separate the page to each file.10 pages to 1 PDF files in 1 run.
Thomas Daniels 28-Jun-13 4:03am
   
Have a look here:
Splitting and Merging Pdf Files in C# Using iTextSharp
It's a C# article, but you can convert the C# code to VB.NET using this online tool: http://converter.telerik.com/
Adrian4263 28-Jun-13 22:39pm
   
Seen its useless for me.Still have any solution?Except itextSharp.
Thomas Daniels 29-Jun-13 3:57am
   
You can try to use PDFsharp instead of iTextSharp
Adrian4263 29-Jun-13 4:00am
   
Now is Crystal report export to PDF files, use PDFSharp it is work?
Thomas Daniels 29-Jun-13 4:03am
   
If you want to convert a Crystal Report to PDF, have a look here:
http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-export-pdf.htm
Adrian4263 2-Jul-13 5:15am
   
You know how to delete the exported files(visual basic code)? Mean I click email button,the report will export to PDF file and attach send by email.What I need is the exported files will auto deleted after my email sent out. Is that clear?You can ask me,if my question not clear.Thx
Thomas Daniels 2-Jul-13 12:18pm
   
Yes, after you sent a mail, run this code:
<small>
System.IO.File.Delete(pathOfYourPdfFile)</small>

Change pathOfYourPdfFile into the path of your PDF file, or change it into the variable that contains the path.
Adrian4263 2-Jul-13 21:28pm
   
I've tried,IO Exception was unhandled(The process cannot access the file 'C:\......py1.pdf' because it is being used by another process.)What is the problem?
Thomas Daniels 3-Jul-13 12:14pm
   
Try to change:
<small>System.IO.File.Delete(pathOfYourPdfFile)</small>
into:
<small>
msg.Dispose()
System.IO.File.Delete(pathOfYourPdfFile)</small>
Adrian4263 3-Jul-13 21:12pm
   
Under msg got blue line, is it wrong?
Adrian4263 4-Jul-13 0:18am
   
I tried already, samething.
Thomas Daniels 4-Jul-13 12:06pm
   
I'm sorry, but I've no idea what's wrong. I'm sorry.
Adrian4263 4-Jul-13 22:50pm
   
Is okay.You Know Do Loop?
Thomas Daniels 5-Jul-13 12:08pm
   
Do you mean this Do Loop?
Adrian4263 7-Jul-13 22:40pm
   
Yup
Thomas Daniels 8-Jul-13 2:30am
   
Then I know the Do Loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100