Email Connection code

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi Guys,

Have been surfing the net looking for code that would allow me to send e-mails. Found the following code in the live demo at: http://www.4guysfromrolla.com/webtech/112298-1.shtml

Now, am i right in saying that i simple just have to create an accout from which i can send the emails. But where in the code do i specify the password to the address (Know its a stupid question).

<%
If Len(Request.Form("txtEmail")) > 0 then

Dim objMail
Set objMail = Server.CreateObject("CDONTS.NewMail")

objMail.From = "info@4guysfromrolla.com (4GuysFromRolla.com)"
objMail.Subject = "Email attachment demo"
objMail.AttachFile Server.MapPath("/images/new468x60.gif")
objMail.To = Request.Form("txtEmail")
objMail.Body = "This is a demo on sending an email with an attachment."
objMail.Send

Response.write("<i>Mail was Sent</i><p>")

'You should always do this with CDONTS.
set objMail = nothing


End If
%>

<form method="post" id=form1 name=form1>
<b>Enter your email address:</b><br>
<input type="text" name="txtEmail" value="<%=Request.Form("txtEmail")%>">
<p>
<input type="submit" value="Send me an Email with an Attachment!" id=submit1 name=submit1>
</form>
 
mike55 said:
Hi Guys,

<%
If Len(Request.Form("txtEmail")) > 0 then

Dim objMail
Set objMail = Server.CreateObject("CDONTS.NewMail")

objMail.From = "info@4guysfromrolla.com (4GuysFromRolla.com)"
objMail.Subject = "Email attachment demo"
objMail.AttachFile Server.MapPath("/images/new468x60.gif")
objMail.To = Request.Form("txtEmail")
objMail.Body = "This is a demo on sending an email with an attachment."
objMail.Send

Response.write("<i>Mail was Sent</i><p>")

'You should always do this with CDONTS.
set objMail = nothing


End If
%>

<form method="post" id=form1 name=form1>
<b>Enter your email address:</b><br>
<input type="text" name="txtEmail" value="<%=Request.Form("txtEmail")%>">
<p>
<input type="submit" value="Send me an Email with an Attachment!" id=submit1 name=submit1>
</form>

This is actually classic ASP, did you want a .Net sollution (This is a .Net board)?
However it makes little difference to your question,
The answer is... you don't
As long as you have CDonts registered on your machine and have installed an SMTP server then all you have to do is write in plain text the address (yes it can be fictitious) that you are sending it from
 
Tell me if you want really to logon to a SMTP server.
You'll however need the cdosys.dll from Windows Server and I got the right code to connect yourself.
Tell me if you are interested.

(If your machine have an SMTP server on it it'll not be necessary)
 
Arch4ngel said:
Tell me if you want really to logon to a SMTP server.
You'll however need the cdosys.dll from Windows Server and I got the right code to connect yourself.
Tell me if you are interested.

(If your machine have an SMTP server on it it'll not be necessary)


Yea, would appreciate it, always looking for options.
 
Sending email

mike55 said:
Yea, would appreciate it, always looking for options.
Code:
[size=2][color=#0000ff]public[/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] SendEmployeeList( DataTable dt, [/size][size=2][color=#0000ff]string[/color][/size][size=2] HTML)
 
{
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] to = [email="mike55@hotmail.com"]mike55@hotmail.com[/email];
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] from = [email="someone@microsoft.com"]someone@microsoft.com[/email];
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] subj = "This is the subject line";
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] username = "USERNAME";
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] password = "PASSWORD";[/size]
[size=2][size=2]Message msg = [/size][size=2][color=#0000ff]new[/color][/size][size=2] MessageClass();
 
[/size][size=2]msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = m_Serveur;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = m_Port;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CdoSendUsing.cdoSendUsingPort;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = username;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = CdoProtocolsAuthentication.cdoBasic;
 [/size][size=2]msg.Configuration.Fields.Update();
[size=2]msg.To = to;
 
msg.From = from;
 
msg.Subject = subj;
[/size][size=2]msg.HTMLBody = HTML;
 [/size][size=2]msg.Send();
 
[/size][/size][/size][size=2]}
 
[/size]

Note: You must add a reference to the "cdosys.dll". You can find it on the web. I don't think I could send you the DLL through this forum. After you add the reference... a simple

using CDO;

using ADODB;

And the job's done.

N.B.: You could add a try catch if the send fails.
 
Arch4ngel said:
Code:
[size=2][color=#0000ff]public[/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] SendEmployeeList( DataTable dt, [/size][size=2][color=#0000ff]string[/color][/size][size=2] HTML)
 
{
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] to = [email="mike55@hotmail.com"]mike55@hotmail.com[/email];
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] from = [email="someone@microsoft.com"]someone@microsoft.com[/email];
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] subj = "This is the subject line";
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] username = "USERNAME";
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] password = "PASSWORD";[/size]
[size=2][size=2]Message msg = [/size][size=2][color=#0000ff]new[/color][/size][size=2] MessageClass();
 
[/size][size=2]msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = m_Serveur;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = m_Port;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CdoSendUsing.cdoSendUsingPort;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = username;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;
 
msg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = CdoProtocolsAuthentication.cdoBasic;
 [/size][size=2]msg.Configuration.Fields.Update();
[size=2]msg.To = to;
 
msg.From = from;
 
msg.Subject = subj;
[/size][size=2]msg.HTMLBody = HTML;
 [/size][size=2]msg.Send();
 
[/size][/size][/size][size=2]}
 
[/size]

Note: You must add a reference to the "cdosys.dll". You can find it on the web. I don't think I could send you the DLL through this forum. After you add the reference... a simple

using CDO;

using ADODB;

And the job's done.

N.B.: You could add a try catch if the send fails.


Ok i have taken the code and have attempted to translate it into VB.NET. I have also downloaded and added a reference to the .dll file (have also followed the instructions on how to register it), however i have been unable to convert the msg.Configuration.Fields entry to VB.NET, any suggestions.

Here is the translation so far.
Imports CDONTS
Imports ADODB
Imports System.Web.Mail

Try
Dim ReceiverAddress As String = "mikeodonnell55@netscape.net"
Dim SendersAddress As String = "admiralodonnell_cinc@hotmail.com"
Dim subject As String = "This is a test"
Dim username As String = "admiralodonnell_cinc"
Dim password As String = "hmsinvincible"

Dim msg As New NewMailClass

msg.To = ReceiverAddress
msg.From = SendersAddress
msg.Subject = subject
msg.Body = "Sorry if you receive this by accident, this is a test."
msg.Send()

Catch ex As Exception
Throw ex
End Try
 
Works for me...

Code:
Dim mail As New MailMessage
        mail.To = "someone@there.com"
        mail.From = "someone@here.com""
        mail.Subject = "Test!"
        mail.Body = "Tesing!"
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "EmailAccountUserName") 'set your username here
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "EmailAccountPass") 'set your password here
        SmtpMail.SmtpServer = "mail.here.com"
        Try
            SmtpMail.Send(mail)
        Catch ex As Exception
            'Error!!!
        End Try
 
It work only if you have a SMTP server that don't require Authentication.

But most of them now require one! At least everybody got a SMTP server on there machine it won't work.
 
Arch4ngel said:
It work only if you have a SMTP server that don't require Authentication.

But most of them now require one! At least everybody got a SMTP server on there machine it won't work.
the code does work on mail servers that require authentication.....
 
Forget it.
It work the way you said (but in C#).

For the authentication it just need some "Fields".

Hehe I've been such a moron during a day looking for this.
 
In relation to the VB.NET code supplied by MorningZ, well i put it all behind a button, filled in the to and from address spaces with proper email accounts, have set the Smtp server to hotmail ("http://services.msn.com/svcs/hotmail/httpmail.asp") filled in the correct username(username@hotmail.com) and password and built the solution. However am now getting a transport problem(transport failed to connect to the server problem)!!

Ahhh my head will explode!!!

Dim mail As New MailMessage
mail.To = "Receivers_Account@eircom.net"
mail.From = "MyAccount@hotmail.com"
mail.Subject = "Test!"
mail.Body = "Tesing!"
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "MyAccount@hotmail.com") 'set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*****") 'set your password here
SmtpMail.SmtpServer = "http://services.msn.com/svcs/hotmail/httpmail.asp"
Try
SmtpMail.Send(mail)
lblOutput.Text = "Looking good"
Catch ex As Exception
Throw ex
End Try
 
the .SmtpServer should be the address of a valid smtp server - you appear to be setting it to a web-site. Hotmail is a web based mail service and I don't think you can access as if it was a smtp based mail server.
 
PlausiblyDamp said:
the .SmtpServer should be the address of a valid smtp server - you appear to be setting it to a web-site. Hotmail is a web based mail service and I don't think you can access as if it was a smtp based mail server.


Can you suggest any alternative provider??
 
Or simply install a Smtp Server on your machine and send email from there... need a permanant connection and maybe a server if you want something good.

Otherwise : $8 for a web host account with mail

:D
 
Hi guys

Finally got the email system working. Had to use an account from oceanfree.net to get it working. By using the code supplied by MorningZ, and adapting the SMTPServer to
SmtpMail.SmtpServer = "smtp.oceanfree.net"
was able to send the email, Norton even scanned the email as it went out.

Mike55
 
Back
Top