Can anyone sort out the following code to make the adding an attachment to an email work please.
Thanks
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="ISO-8859-1" Debug="true" %>
<% #Import Namespace="System.Web.Mail" %>
<% #Import Namespace="IO" %>
<script language="vb" runat="server">
Sub btnSendEmail_Click(sender as Object, e as EventArgs)
Dim objMM as New MailMessage()
objMM.To = "my#email.co.uk"
objMM.From = "their#email.co.uk"
objMM.BodyFormat = MailFormat.HTML
objMM.Priority = MailPriority.Normal
objMM.Subject = "Attachment test"
objMM.Body = "There should be an attachment with this email"
objMM.Attachments.Add(new MailAttachment("myimage.jpg"))
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(objMM)
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button runat="server" id="btnSendEmail" Text="Send email" OnClick="btnSendEmail_Click" />
</form>
</body>
</html>
I'm suspicious about
new MailAttachment("myimage.jpg")
I suspect you might want to get the full path e.g.
new MailAttachment(Server.MapPath("Myimage.jpg"))
The file path needs to be a full path but that aside, System.Web.Mail is deprecated/obsolete. You should be using the System.Net.Mail API, see here for examples.
Related
I have an aspx page with this tag in the <body>:
<asp:ScriptManager ID="scriptManager1" runat="server" />
Now elsewhere in the aspx page I have some VB code (embedded in the page using <% %>, not in a code behind vb file):
scriptManager1.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
Problem is, I get a compile error on that line saying that scriptManager1 is not declared. I thought all controls with runat="server" are accessible in code? Why is this not working?
edit:
Oh, I figured it out - sort of. RegisterClientScriptBlock is a shared method of ScriptManager for some reason, so I need to call it like so:
ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
Calling it as an instance method won't work.
However - now I am getting an error saying that the ScriptManager class itself doesn't exist! I am referencing the appropriate namespace like so at the top of the page:
<%# Import Namespace="System.Web.UI" %>
Why is ScriptManager not being found? I even get an error if I explicitly reference the class by namespace:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
edit: here is a simple page which should reproduce the error:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<html>
<head>
<title>FRED</title>
</head>
<body>
<asp:ScriptManager ID="scriptManager1" runat="server" />
<%
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');")
%>
</body>
</html>
edit: tried this as suggested, same error:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');")
End Sub
</script>
<html>
<head>
<title>FRED</title>
</head>
<body>
<asp:ScriptManager ID="scriptManager1" runat="server" />
</body>
</html>
Based on the arguments you are trying to use in the method call, it looks a bit like you are mixing up two separate classes:
System.Web.UI.ScriptManager
System.Web.UI.ScriptManager docs link
and
System.Web.UI.ClientScriptManager
System.Web.UI.ClientScriptManager docs link
The first one of these two lives in the System.Web.Extensions.dll assembly. Have you checked that you are referencing this assembly?
The second one is exposed through the ClientScript property on the Page class. I think what you should be doing is this (note C# syntax, should be easy to change to VB though):
<%
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');");
%>
Check this example here, it looks like you may need to call it in Page_PreRender - the example is in C# rather than VB but the premise should be the same.
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerclientscriptblock?view=netframework-4.8
I took your sample with the call in Page_PreRender and with a few adjustments (different parameters for RegisterClientScriptBlock, putting a form with runat=server around the script manager) I was able to get it to work for me.
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
System.Web.UI.ScriptManager.RegisterClientScriptBlock(scriptManager1, Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');", True)
End Sub
</script>
<html>
<head>
<title>FRED</title>
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server" />
</form>
</body>
</html>
I'm not sure why your parameters for RegisterClientScriptBlock were different though, so I'm wondering if there's a difference in .NET version? Still, hopefully this helps.
This is my default.aspx file
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome to sample website:)
</div>
<p>
</p>
<p>
<asp:LoginName id="LoginName1" runat="server" FormatString="Welcome,{0}" />
</p>
<p>
</p>
</form>
</body>
</html>
I am able to display the doaminname\username but I need to display only the username . i am confused how do I go ahead with this.Can anyone help me with this?
Add the below code in your page_load
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity == null)
throw new InvalidOperationException("WindowsIdentity is null");
string nameWithoutDomain = windowsIdentity.Name.Split('\\').Last();
LoginName1.Text = String.Format("Welcome,{0}", nameWithoutDomain)
And if you want a helper class you can refer to this Built-in helper to parse User.Identity.Name into Domain\Username
In visual Studio I've added a WSDL web reference which loads a variety of "methods" for calling a SOAP api. I have a blank aspx page. How do I call these methods loaded by the web reference? I've called the web reference mywsdl.
Here's my code so far:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%# Import Namespace="WebApplication1" %>
<%
End Sub
Private Sub Call_Web_Service_Method()
Dim output
Dim CallWebService As New ServerName.mywsdl()
Dim sGetValue As String = CallWebService.GetSomeValue()
output = sGetValue
End Sub
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=output%>
</div>
</form>
</body>
</html>
Add the web service as a reference at the top of your code behind web page, instantiate a new instance of it where you need it and use it. The methods should already be exposed as you've successfully imported the .asmx I believe, however if you're not getting data back it might require authentication.
using mywsdl;
mywsdl my = new mywsdl();
or
Imports mywsdl
Dim my = new mywsdl()
I'm on VS 2008, Windows Xp.
My page:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%= WriteReport()%>
<%= "Hellooo" %>
</asp:Content>
Code-behind:
public string WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
return swriter.ToString();
}
The funny thing is the "Heloo" goes in the contentPlace, but the method's return does not. It's place below the div of the ContentPlace (ends up in the footer div).
Thank you for your help.
EDIT----------------------
Temporary solution
<%= WriteReport()%>
<asp:Table runat="server" ID="testeme">
</asp:Table>
I added a bogus table (id=testeme), it does nothing and has nothing in it. But it works now, what the hell. Only works when below the method.
EDIT2--------------------
I tried to guess the relevant parts from the MasterPage and placed them here.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyAppV2.Views.MasterPage" %>
<div id="general">
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="main">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</div>
</form>
</div>
<div id="footer">
<p>MYFOOTER</p>
</div>
First line of the view using the master page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MP5._Default" MasterPageFile="~/MasterPage.Master" %>
Instead of injecting code into your page using <%= ... %>, you can use an <asp:literal> tag:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:Literal id="MyReport" runat="server" />
</asp:Content>
Then you can write to it from your code-behind using the text property. You can call the WriteReport() method from within Page_Load.
public void WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
MyReport.Text = swriter.ToString();
}
That should solve any oddities of where the code ends up on the page.
I've got an ASP.NET upload form on one page, where the user can upload an image. heres the code for that one:
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
<html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
If imageupload1.HasFile Then
imageupload1.SaveAs(Server.MapPath(".") + "/uploadedimages/" & imageupload1.FileName)
Label1.Text = "Received " & imageupload1.FileName & " Content Type " & imageupload1.PostedFile.ContentType & " Length " & imageupload1.PostedFile.ContentLength
Else
Label1.Text = "No uploaded file"
End If
end sub
</script>
</head>
<body>
<form id="imguplad" runat=server>
<asp:FileUpLoad id="imageupload1" AlternateText="You cannot upload files" runat="server" />
<asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server" />
<asp:Label id="Label1" runat="server" />
<input type="button" value="Click here when image is uploaded"
onClick="location.href='imageloadtest.aspx';">
</form>
</body>
</html>
Then I am trying to retrieve it on another page, using the following code:
<%# Page Language="C#" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
...blah
<body>
<img id="image" src="Server.MapPath(".")+"/uploadedimages/"+Request.Form["imageupload1.FileName"]";">
</body>
...blah
Can't seem to get it to show up? I must have some path slightly wrong.
EDIT: By the way, the image uploads perfectly, it just won't show up on the next page.
Try with
<img id="image" src="<%=Server.MapPath("~")%>/uploadedimages/<%=Request.Form["imageupload1.FileName"]%>">
You also may have to set manually the image name in session because you won't have access to imageupload1.FileName in another page, where the control imageupload1 doesn't exist.
So in your click event add Session["ImagePath"] = imageupload1.FileName
And the img tag replace Request.Form["imageupload1.FileName"] by Session["ImagePath"]