For those new to message boards please try to follow a few simple rules when posting your question.
Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
Keep the subject line brief, but descriptive. eg "File Serialization problem"
Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
No advertising or soliciting.
We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
To send emails from an (old) asp.net website I use the following code :
Protected Sub SendMail(sender As Object, e As System.EventArgs)
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Dim smtp As New SmtpClient
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Port = smtpSection.Network.Port
'solution 1 does not work !
'Dim networkCred As NetworkCredential = New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
'smtp.Credentials = networkCred
'solution 2 does not work !
'Dim networkCred As New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
'smtp.Credentials = networkCred
'solution 3 does work !!!
smtp.Credentials = New Net.NetworkCredential("firstname.lastname@example.org", "zzzzzz")
Using mm As New MailMessage(smtpSection.From, txtTo.Text.Trim())
mm.Subject = txtSubject.Text.Trim()
mm.Body = txtBody.Text.Trim()
mm.IsBodyHtml = False
TextBox2.Text = smtpSection.Network.UserName
TextBox3.Text = smtpSection.Network.Password
TextBox4.Text = smtpSection.Network.Host
TextBox5.Text = smtpSection.Network.Port
TextBox6.Text = smtpSection.Network.EnableSsl
TextBox7.Text = smtpSection.Network.DefaultCredentials
TextBox8.Text = smtpSection.From
TextBox9.Text = txtTo.Text.Trim()
Catch error_t As Exception
TextBox1.Text = error_t.ToString
The web.config section contains this :
<smtp deliveryMethod="Network" from="email@example.com" >
The namespaces loaded are :
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Net.Configuration" %>
Solution 3 works fine but I don't like to show passwords in a page.
When using solution 1 or 2 the mailserver replies it does not relay, so apparently the credentials are not retrieved work properly.
All the other parameters do show properly in the textboxes I designed to test this sub.
Does anyone have a suggestion ?
Two things. ASP.NET code always executes on the server, never the client. That code will never be shown as part of "a page".
The first two attempts you show as "does not work" are because you're making assumptions about what those smtpSection.Network.UserName and smtpSection.Network.Password properties are returning. I would be willing to bet they don't return anything, so you're creating a NetworkCredential with blank username and password.
How do you tell? One word: debugger. Set a breakpoint on the line that creates the NetworkCredential and examine the values of UserName and Password. Chances are good those values are not what you think they are.
The error in IIS simply means that the user your application pool is running as doesn't have access to the resource you're trying to load. It works when you debug the code in Visual Studio because it's running under your user account at that point.
Change the SSRS permissions to give your App Pool user access to run the reports; change your App Pool to run as a user with the relevant permissions; or change your code to pass the credentials of a user with the relevant permissions instead of relying on the default credentials.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer