smtp

J

JD

Is it possible using the smtp class in asp.net to send emails and
authenticate to the mail server that is sending the emails out.

Currently I have a web application that works fine on my machine, will send
email to both my company email addresses and to non-company email addresses.
However, as soon as I move the app into the production server it does not
send email to non company email addresses. What I mean by company is our
domain could @domain.com and it will work fine sending emails to any one
with that email address, but anyone without that in their email address and
it crashes. Any help on this would be appreciated.
 
L

Lau Lei Cheong

Private Sub Command1_Click()
Dim Mail As CDO.Message
Dim Conf As CDO.Configuration
Set Mail = New CDO.Message
Set Conf = New CDO.Configuration
With Conf
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")
= CDO.CdoSendUsing.cdoSendUsingPort
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "your_mail_server_here"
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1") 'basic authentication
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"my_username_here") 'set your username here
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"super_secret") 'set your password here
End With
With Mail
.Configuration = Conf
.From = "your_email_addr"
.To = "target_email_addr"
.Subject = "Testing"
.HTMLBody = "As title"
.Sender = "your_email_addr"
.Send
End With
End Sub
 
J

JD

Lau Lei

Thanks for the quick reply, my bad for not pointing this out but this
was using asp.net and not visual basic.

See code down below

'Put user code to initialize the page here
Dim oMail As System.Web.Mail.SmtpMail
Dim oMailMsg As New System.Web.Mail.MailMessage
Dim sServer As String
Dim sMsgBody As New System.Text.StringBuilder
Dim sAutoMsgBody As New System.Text.StringBuilder


Try

'Check to see if there is any postback, if there is none
then send the message
If Not IsPostBack Then
With oMail
'Set the mail server name
.SmtpServer =
ConfigurationSettings.AppSettings("SmtpServer")

'Get the name of the server that this resides on
sServer = Request.ServerVariables("SERVER_NAME")
With oMailMsg

'Set the body format
.BodyFormat = Mail.MailFormat.Html
'Set the body content
With sMsgBody
.Append("Form Name:" &
Request.Form("FormName") & "<br>")
.Append("<br>")
.Append("First Name: " &
Request.Form("FirstName") & "<br>")
End With

.Body = sMsgBody.ToString() & vbCrLf & "Server:
" & sServer


''Automate response email
'With sAutoMsgBody
' .Append("<p>blah blah</p>")
' .Append("<br>")
'End With

'With oMailMsg
' 'Take it from the email address that was
entered on the contact form
' .To = Request.Form("email")
' 'Set the body format
' .BodyFormat = Mail.MailFormat.Html
' 'Subject line
' .Subject = "Automated response"
' 'From address
' .From =
ConfigurationSettings.AppSettings("CustFromSite")
' 'Body
' .Body = sAutoMsgBody.ToString()
'End With
''Send the email
'.Send(oMailMsg)

End With
'Clean up
oMail = Nothing
oMailMsg = Nothing
sMsgBody = Nothing
'Check to see if this is going to a custom thank you
page or not
If Len(Request.Form("RedirectPath")) > 0 Then
'Redirect the site to the custom thank you page
Response.Redirect(Request.Form("RedirectPath"))
End If

End If
Catch ex As Exception
'To be done yet
Response.Write(ex.Message)
Response.Write(ex.StackTrace)

End Try
 
L

Lau Lei Cheong

It doesn't really matter.

First of all you should know System.Web.Mail is just a wrapper over CDO
objects.

And then MailMessage also have property called "Fields" that you can use.

Just use the field names in the example to fill in the information.
 
G

Guest

JD,

I think the problem is : The production server requires authentication
before you can send a mail using a specific email Id.

In ASP .Net there is no direct way to achieve this, so the solution Lau has
suggested can be implemented in ASP .Net by adding a reference to the COM
object.
 
J

JD

Okay, could you give me an example of how it would look, I am trying and I
am getting some weird looking results.

CDO.CdoSendUsing.cdoSendUsingPort - Where does this come from?


.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")
= CDO.CdoSendUsing.cdoSendUsingPort
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "your_mail_server_here"
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1") 'basic authentication
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"my_username_here") 'set your username here
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"super_secret") 'set your password here


Lau Lei Cheong said:
It doesn't really matter.

First of all you should know System.Web.Mail is just a wrapper over CDO
objects.

And then MailMessage also have property called "Fields" that you can use.

Just use the field names in the example to fill in the information.
 
J

JD

It would seem that you may be right

..Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

..Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").values
= "mail.nemo.com"

..Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").values
= 25

..Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1") 'basic authentication

..Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"crush") 'set your username here

..Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"totallydude") 'set your password here

it fails on the first line above and I get the error message down below



Object variable or With block variable not set. at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object o,
Type& objType, String name, Object[] args, String[] paramnames, Boolean
OptimisticSet, CallType UseCallType) at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateSetComplex(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean
OptimisticSet, Boolean RValueBase) at
blueconnect.sendmail.Page_PreRender(Object sender, EventArgs e) in
C:\projects\Docroot\OtherClients\jci\app_includes\sendmail.aspx.vb:line 49
 
J

Jeremy S.

This works for me (and no COM wrapper stuff or CDO objects needed):

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.To = destinationAddresses;
mail.From = fromAddress;
mail.Subject = subjectLine;
mail.Body = mailBody;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
m_SMTPLoginID); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
m_SMTPLoginPW); //set your password here
System.Web.Mail.SmtpMail.SmtpServer = m_SMTPMailServer; // e.g.,
"mail.MyDomain.com"; //your smtp server goes here
System.Web.Mail.SmtpMail.Send( mail );

-HTH

JD said:
It would seem that you may be right

.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").values
= "mail.nemo.com"

.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").values
= 25

.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1") 'basic authentication

.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
"crush") 'set your username here

.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
"totallydude") 'set your password here

it fails on the first line above and I get the error message down below



Object variable or With block variable not set. at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object
o, Type& objType, String name, Object[] args, String[] paramnames, Boolean
OptimisticSet, CallType UseCallType) at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateSetComplex(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean
OptimisticSet, Boolean RValueBase) at
blueconnect.sendmail.Page_PreRender(Object sender, EventArgs e) in
C:\projects\Docroot\OtherClients\jci\app_includes\sendmail.aspx.vb:line 49



Mayur Shah said:
JD,

I think the problem is : The production server requires authentication
before you can send a mail using a specific email Id.

In ASP .Net there is no direct way to achieve this, so the solution Lau
has
suggested can be implemented in ASP .Net by adding a reference to the COM
object.
 
J

JD

Thanks everyone, I was able to get it work after I tried out Jeremy S.
suggestion, learn something new tonight or morning or whatever time it is
now. Issue closed.
 
J

Jeremy S.

Keep in mind that the method I showed to you is officially "undocumented" -
meaning it might not be supported in the future. I suspect that's why this
solution is rarely recommended.

I'm sure the MVPs around here are aware of this undocumented solution - but
they don't recommend it - and probably for good reason. I'd be interested in
knowing why. This question is posed with some regularity and they always
seem to recommend some more complicated method that relies on CDO/COM
wrapper-based solution.

-JS


JD said:
Thanks everyone, I was able to get it work after I tried out Jeremy S.
suggestion, learn something new tonight or morning or whatever time it is
now. Issue closed.
 
J

Juan T. Llibre

Hi, Jeremy.

re:
I'm sure the MVPs around here are aware of this undocumented solution - but they don't
recommend it - and probably for good reason.

The only reason to use that is if the smtp server needs authentication.

I've sent quite a few references to this FAQ page in response
to questions about sending authenticated smtp mail:

http://www.systemwebmail.com/faq/3.8.aspx

If the server doesn't need authenticacion, the standard, less complicated,
methods of sending mail ( the "wrappers" you refer to ) work just fine,
so why introduce complicated schema code if there's no need for it ?

The best reference site for system.web.mail is this FAQ :

http://www.systemwebmail.com/faq.aspx

I don't think I've come up with a problem not documented/solved there.




Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
==========================

Jeremy S. said:
Keep in mind that the method I showed to you is officially "undocumented" - meaning it
might not be supported in the future. I suspect that's why this solution is rarely
recommended.

I'm sure the MVPs around here are aware of this undocumented solution - but they don't
recommend it - and probably for good reason. I'd be interested in knowing why. This
question is posed with some regularity and they always seem to recommend some more
complicated method that relies on CDO/COM wrapper-based solution.

-JS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top