Saturday, March 31, 2012

smtp

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 @dotnet.itags.org.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.
J. DPrivate 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
"JD" <jdnews@.dalys.us> glsD:uCHRS7NtFHA.1204@.TK2MSFTNGP15.phx.gbl...d">
> 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.
> --
> J. D
>
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
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
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.
"JD" <work@.dalyse.us> '?:eYR54BPtFHA.1372@.TK2MSFTNGP09.phx.gbl...
> 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
>
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com
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.
"JD" wrote:

> 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
>
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com
>
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" <leu_lc@.yehoo.com.hk> wrote in message
news:OWZJUMPtFHA.3908@.tk2msftngp13.phx.gbl...
> 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.
> "JD" <work@.dalyse.us> '?:eYR54BPtFHA.1372@.TK2MSFTNGP09.phx.gbl...
>
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").va
lues
= 25
.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticat
e",
"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" <Mayur Shah@.discussions.microsoft.com> wrote in message
news:6CB811CD-6041-447E-AC7C-6F1A842BB305@.microsoft.com...
> 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.
> "JD" wrote:
>
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/smtpauthenti
cate",
"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" <jdnews@.dalys.us> wrote in message
news:Oa7PQgPtFHA.3188@.TK2MSFTNGP14.phx.gbl...
> 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").value
s
> = "mail.nemo.com"
> .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").v
alues
> = 25
> .Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthentica
te",
> "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" <Mayur Shah@.discussions.microsoft.com> wrote in message
> news:6CB811CD-6041-447E-AC7C-6F1A842BB305@.microsoft.com...
>
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.
"JD" <jdnews@.dalys.us> wrote in message
news:uCHRS7NtFHA.1204@.TK2MSFTNGP15.phx.gbl...
> 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.
> --
> J. D
>
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" <jdnews@.dalys.us> wrote in message
news:OL7V3VQtFHA.3236@.TK2MSFTNGP14.phx.gbl...
> 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.
>
> "JD" <jdnews@.dalys.us> wrote in message
> news:uCHRS7NtFHA.1204@.TK2MSFTNGP15.phx.gbl...
>

0 comments:

Post a Comment