I made a mail sender in C# but I'm having trouble with the body of the mail. It only sends the texts without pictures, links and other elements. Not to mention, I used RichTextBox for that purpose.
So now my question is: what component to use to send mail with pictures, links and else?
I also enabled IsBodyHtml to true.
What I want to do is to copy the pictures, links and texts (with different colors and size) from Microsoft Word and paste it to control, and when user gets mail he gets the exact same body and layout as I send them.
You'll need to send it as html.
Save your word doc as html and use that.
For the images in your document you can either point to them via their absolute urls (publicly available via the internet).
Or you could use the LinkedResource class.
With the LinkedResource class your images have to specify a cid in the source.
var inlineLogo = new LinkedResource("path/myfile.png");
inlineLogo.ContentId = Guid.NewGuid().ToString();
var imageHtmlFragment = string.Format("<img alt='My Logo' src='cid:{0}' style='width: 250px;height: 60px;'/>",inlineLogo.ContentId);
var newMail = new MailMessage();
var view = AlternateView.CreateAlternateViewFromString(imageHtmlFragment, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);
Related
I have this little piece of code
var builder = new BodyBuilder();
var image = builder.LinkedResources.Add(logoPath);
image.ContentId = MimeUtils.GenerateMessageId();
textMsg = textMsg.Replace("{logo}", image.ContentId);
builder.HtmlBody = textMsg;
msg.Body = builder.ToMessageBody();
It works perfectly except that if I use a .png image it isn't attach to the mail but if it is an .svg it is attach. I tried to convert the svg to base64 but it didn't work.
As I show in the images below, if it is a .svg the image is attach at the top of the mail but if it is a.png it doesn't. Is it possible to do the same with .svg? I don't want the attached file.
The issue you are facing is a limitation of the receiving mail client, not anything you are doing wrong with the images in your email message.
The client you are using to receive the message does not support SVG and so has decided that whenever it encounters an SVG image, it will offer it to the user as an attachment.
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 generating a barcode image as the response from an HTTP handler, like so:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "image/Jpeg";
MemoryStream ms = new MemoryStream();
Bitmap objBitmap = GenerateBarcode(context.Request.Params["Code"]);
objBitmap.Save(ms, ImageFormat.Jpeg);
context.Response.BinaryWrite(ms.GetBuffer());
}
I go to http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678 and it works great. Likewise I stick <img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"> on my webpage and it works great. But I stick that same image tag in an HTML email and it doesn't show up (at least not in MS Outlook 2007; I haven't tested other email clients yet.)
I'm guessing this is somehow related to the fact that I'm using an HTTP handler, as other, static images in the email are showing up fine. How can I fix this so that the image shows up? (I can't just use a static image because the code is determined at the time the email is sent.)
Update:
It turns out I hadn't noticed a key detail. The image isn't just not showing up; rather the image src attribute is getting replaced with "http://portal.mxlogic.com/images/transparent.gif". I've determined that using the .aspx or .ashx extensions triggers this replacement (or probably any extension except those expected for images like .gif or .jpg), and that including a query string in the URL also triggers this, even when it's a standard image extension. I guess it's some overzealous security feature. So including an image like BarCode.aspx?code=12345678 just isn't going to work.
It occurs to me that I could do something like <img alt="" src="http://www.MyWebsite.com/MyProject/12345678/BarCode.jpg"> and then create a handler for all files named BarCode.jpg. Here 12345678/ isn't an actual path but it wouldn't matter since I'm redirecting the request to a handler, and I could scrape the code value from that phony path in the URL. But, I'd probably have to change some IIS setting to have requests for .jpg files handled by ASP.NET, and I'd want to still make sure that other JPEGs besides my BarCode.jpg loaded normally.
Honestly, I'm not sure if it's worth the hassle.
Microsoft Office Outlook 2007 uses the HTML parsing and rendering engine from Microsoft Office Word 2007 to display HTML message bodies. more
which leaves us with some hairy points
The limitations imposed by Word 2007 are described in detail linked off in the article, but here are a few highlights:
no support for background images
(HTML or CSS)
no support for forms
no support for Flash, or other
plugins
no support for CSS floats
no support for replacing bullets with
images in unordered lists
no support for CSS positioning
no support for
animated GIFs
i think your best bet would be to embed the image using the LinkedResource class, something like
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("...");
mail.To.Add("...");
//set the content
mail.Subject = "Test mail";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image." ", null, "text/html");
//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource(GenerateBarcode(Code)));
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
mail.AlternateViews.Add(htmlView);
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
I don't know why it doesn't work but if you are saying that static images from the site hosting the handler work fine here's a slightly modified version you might try:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
using (var image = GenerateBarcode(context.Request.Params["Code"]))
{
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
No need to clear the response in a generic handler as there's nothing written yet
Content-Type is image/jpeg
No need of intermediary memory stream, you could write directly to the response
Make sure to properly dispose the bitmap
You will have to put the image as an attachment, or use html in the mail. Probably your error happends due to Outlook though.
This link suggests the problem, as you suggested is a security/anti-SPAM feature causing the problem (mxlogic.com points to a McAfee product)
I am accessing mail body and fetching it in another mail.
But i am not getting original format of previous mail in new mail.
Problem i am facing in this situation are:
Not getting images in destination mail.
Font is also varying.
I am accessing mail body as follows:
NotesRichTextItem rtItem = (NotesRichTextItem)docInbox.GetFirstItem("Body");
String Body = rtItem.GetFormattedText(false , 0);
String bodyFormat = rtItem.type.ToString();
also tried this code:
NotesItem itemBody = docInbox.GetFirstItem("Body");
String bodyFormat = itemBody.type.ToString();
String Body = itemBody.Text;
But not getting solution in both case.
If you're trying to access rich text from Lotus Notes and put it into your own system, that will be very difficult and the NotesAPI is of little help to you. However, if you are trying to copy a rich text item from one NotesDocument to another, look at the AppendRTItem method of the NotesRichTextItem class.