I want to mail an asp.net page from c#. well it is questioned widely and I saw bulk of questions like that on stackoverflow too. But I have few problems that I'm not getting the solutions
What Itried
many example. below are few
using (System.IO.StreamReader reader = System.IO.File.OpenText( Server.MapPath("~/About.aspx"))) // Path to your
{ // HTML file
string fromAddress = "from#yahoo.com";
string toAddress = "to#yahoo.com";
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage(fromAddress, toAddress);
myMail.Subject = "HTML Message";
myMail.IsBodyHtml = true;
myMail.Body = reader.ReadToEnd(); // Load the content from your file...
//...
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.mail.yahoo.com");
smtp.Credentials = new System.Net.NetworkCredential("from#yahoo.com", "password");
smtp.Send(myMail);
}
But this is giving me this output.
Well you noticed that it is without css. Can I mail an entire asp.net page or do I need to write my code in c# with inline css? Or do I need to create a control with a patern and send it?
You are trying to send unprocessed aspx file. This cannot be successful. You need to process this page (I dont remember what method to use), and dont forget about inline css. So bassicaly you need a new page. And if you need a new page, you can do it with pure html, not in asp.
Related
I have a project requirement that we need to attach an HTML formatted log sheet to an email that gets sent to a user. I don't want the log sheet to be part of the body. I'd rather not use HTMLTextWriter or StringBuilder because the log sheet is quite complex.
Is there another method that I'm not mentioning or a tool that would make this easier?
Note: I've worked with the MailDefinition class and created a template but I haven't found a way to make this an attachment if that's even possible.
Since you're using WebForms, I would recommend rendering your log sheet in a Control as a string, and then attaching that to a MailMessage.
The rendering part would look a bit like this:
public static string GetRenderedHtml(this Control control)
{
StringBuilder sbHtml = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sbHtml))
using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
{
control.RenderControl(textWriter);
}
return sbHtml.ToString();
}
If you have editable controls (TextBox, DropDownList, etc), you'll need to replace them with Labels or Literals before calling GetRenderedHtml(). See this blog post for a complete example.
Here's the MSDN example for attachments:
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = "data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane#contoso.com",
"ben#contoso.com",
"Quarterly data report.",
"See the attached spreadsheet.");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
You can use Razor for email templates. RazorEngine or MvcMailer might do the job for you
Use Razor views as email templates inside a Web Forms app
Razor views as email templates
http://www.codeproject.com/Articles/145629/Announcing-MvcMailer-Send-Emails-Using-ASP-NET-MVC
http://kazimanzurrashid.com/posts/use-razor-for-email-template-outside-asp-dot-net-mvc
string to = "email#hotmail.co.uk";
string body = "Test";
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
MailMessage mailObj = new MailMessage(urEmail, to, subject, body);
SMTPServer.Send(mailObj);
This is how i am currently sending a test email.
How do i make this html and be able to make the email sent out look better by adding images etc?
Thanks
On the MailMessage set the property IsBodyHtml to true.
string to = "email#hotmail.co.uk";
string body = "Test";
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
MailMessage mailObj = new MailMessage(urEmail, to, subject, body);
mailObj.IsBodyHtml = true; // This line
SMTPServer.Send(mailObj);
You have to set mailObj .IsBodyHtml = true;
you can use the following idea to take an ASPX page and render it to a string:
StringWriter writer = new StringWriter();
Server.Execute("Login.aspx", writer);
string html = writer.ToString();
If you then set the MailMessage.IsBodyHtml to true you can send an HTML message. If you want to use images and other stuff make sure that the receiver of the email can access those images.
There are two ways of doing this:
Embed the images inside your mail. (see this question)
Link to the images through your src attribute of the image tag inside your HTML mail. This needs you to host the image files somewhere on a webserver which the recipients can access.
In both cases you will need to send the mail with a html body.
mailObj.IsBodyHtml = true;
For your question about adding Image to your email, if your asking for embedding then you can use Anchor tags of HTML or else attach the image file to the mail by using mailObj.Attachments.Add() method i guess.
But the best way is to send the images as attachments because some firewalls just blocks the embedded images but allows attachments. So that way your better safer in delivering the content, though its not a perfect way.
I want to send a mail with embeded image in ASP.NET
How can i do that?
Regards
Soner
There are generally two ways of doing this, whichever is preferred is up to you.
To literally "embed" the image in the email message itself, you'll want to add it as a Linked Resource and reference the attached resource in the email's HTML.
Alternatively, and more simply, if the image is hosted in a public location then you can just reference that location in the HTML of the email.
Based on the question, it sounds like you are preferring the former approach, but the latter is available as well.
MailAddress sendFrom = new MailAddress(txtFrom.Text);
MailAddress sendTo = new MailAddress(txtTo.Text);
MailMessage myMessage = new MailMessage(sendFrom, sendTo);
MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;
Attachment attachFile = new Attachment(txtAttachmentPath.Text);
MyMessage.Attachments.Add(attachFile);
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(myMessage);
I believe you can either attach the files and refer them, or alternatively, like in regular HTML, embed them encoded in Base64.
You can go through this link
http://www.dotnetspider.com/resources/41465-Send-Formatted-outlook-email-from-NET-C.aspx
Sample project is also attached.
It shows how to put the link of the image in the application in the html template and send emails.
I'm using the code below in a ASP.NET page to send a file via email from our users home computer to a mailbox that is used for receiving work that needs photocopying. The code below works fine when sending a file within our network but fails when our users are at home and connected via our SSL VPN, there appears to be a bug in our VPN where it doesn't allow the file to be temporarily saved on the webserver before being sent via email. Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? Many thanks Jane.
MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = txtTo.Text;
mail.Cc = txtFrom.Text;
mail.Subject = txtSubject.Text;
mail.Body = "test"
mail.BodyFormat = MailFormat.Html;
string strdir = "E:\\TEMPforReprographics\\"; //<-------PROBLEM AREA
string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);
try
{
txtFile.PostedFile.SaveAs(strdir + strfilename);
string strAttachment = strdir + strfilename;
mail.Attachments.Add(new MailAttachment(strdir + strfilename));
SmtpMail.SmtpServer = "172.16.0.88";
SmtpMail.Send(mail);
Response.Redirect("Thanks.aspx", true);
}
catch
{
Response.Write("An error has occured sending the email or uplocading the file.");
}
finally
{
}
If you use the classes in the System.Net.Mail namespace, the Attachment class in there supports streams, so assuming you can read it in to memory as a stream first you can then add that to the attachment, that way you never have to store any files.
More information (and a sample) here:
http://msdn.microsoft.com/en-us/library/6sdktyws.aspx
Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? Many thanks Jane.
That's impossible. A web server hosting an ASPX page has to receive the file from the client before processing it any further.
Use string strdir = Path.GetTempPath();?
Off the top of my head, create the attachment like:
txtFile.PostedFile.InputStream.Position = 0
mail.Attachments.Add(new MailAttachment(txtFile.PostedFile.InputStream, strfilename ));
That should allow you to create the attachment, without saving it to disk.
I previously used CDO.Message and CDO.Configuration in ASP Classic to create HTML emails which was VERY simple to do. In .NET, it appears that you have to give the System.Net.Mail.Message object an HTML string for the content and then somehow embed the required images. Is there an easy way to do this in .NET? I'm pretty new to .NET MVC and would most appreciate any help.
This is how it looks in ASP Classic:
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.To = someone#somthing.com
objCDO.From = me#myaddress.com
objCDO.CreateMHTMLBody "http://www.example.com/somepage.html"
objCDO.Subject = sSubject
'the following are for advanced CDO schematics
'for authentication and external SMTP
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort '2 - send using port
.Item(cdoSMTPServer) = mail.myaddress.com
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsername) = "myusername"
.Item(cdoSendPassword) = "mypassword"
.Update
End With
Set objCDO.Configuration = cdoConfig
objCDO.Send
Basically I would like to send one of my views (minus site.master) as an email, images embedded.
I don't know of a simple way right off, but you could use WebClient to get your page, then pass the response as the body.
Example:
var webClient = new WebClient();
byte[] returnFromPost = webClient.UploadValues(Url, Inputs);
var utf = new UTF8Encoding();
string returnValue = utf.GetString(returnFromPost);
return returnValue;
Note: Inputs is just a dictionary of post variables.
One problem I think you'll run into right off is that I don't think you'd get the images. You could parse the HTML you get and then make the images absolute back to your server.
Thank you both for your help - here is a very clean and comprehensive tutorial posted by a .NET MVP
http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx