Sending qrCode in an email using C# - c#

I am generating QR code in c# using the qrcoder library. I can generate the code with the information i want, but i would like to send it also through email.
Using following code i get the email, but it won't display the image of the QR code in it. And if I change the address in which I think the code its being saved it also give an error like this:
System.Runtime.InteropServices.ExternalException occurred A generic error occurred in GDI+.
Here is the code:
public void GenerateQR(string infoReserva)
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(infoReserva, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
bitMap.Save(Server.MapPath("~/Images/qrcode.png"), ImageFormat.Png);
}
MailMessage mm = new MailMessage();
mm.From = new MailAddress("name#exampple.comr");
mm.Subject = "How to email self-generated QR code";
mm.Body = " <html><body> <p> QR code as below</p> <p> <img src='http://localhost:44362//Images/qrcode.png' alt='QR Code'/></p> </body></html> ";
mm.To.Add("name#exampple.com");
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "name#exampple.comr";
NetworkCred.Password = "-----";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
I have the code of my project organized in folder like this:
1_Entites
2_DataAcces
3_Exceptions
4_API
5_WEBAPP
The first 4 folder are the backend of the aplications, and the folder named WebApp is in which i have this code and the front end of the app, in this folder i created the folder of Images
Do you know what is the best way of sending this email with de qr code
Thanks in advance!

You’re still going to struggle no matter what you do because you’re trying to send an image and somewhere there will be a mail client that blocks it (linking to an image on your server is a classic spammer trick and nearly certain to be blocked, gmail blocks images sent as data url but accepts inline attached multipart, somewhere else you’ll find a device, probably a phone, that doesn’t accept multipart embedded but accepts data urls..); I’d ditch the idea of sending any graphical pictures at all in the email and switch to using a QR library that can output characters or html so you can form your QR using blocks of black and white (characters or html elements). Not an endorsement and no affiliation, but I know QRCoder can output character based QRs

Related

How to design SMTP mail content in c#

Software that I'm developing right now sends e-mail to responsible when a problem occurs in a production line. However while doing this email,I prefer it with a design like a card not a normal string.
I have just a little knowledge HTML and tried it but result was terrible.
So is there any API for c# to make this?
Look at this sample. May be it can help you.
MailAddress addressFrom = new MailAddress("jack.du#e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32#outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);
message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = #"<html>
<body>
<p>Dear Ms. Susan,</p>
<p>Thank you for your letter of yesterday inviting me to come for
an interview on Friday afternoon, 5th July, at 2:30.I shall be happy to be there as
requested and will bring my diploma and other papers with me.</p>
<p>Sincerely,<br>-Jack</br></p>
</body>
</html>
";
message.BodyHtml = htmlString;
SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);
Console.WriteLine("Sent Successfully!");
Console.Read();
You should use the below property in order to get your content in the Html format.
message.IsBodyHtml = true;
Hope this is helpful

Email with image via external path C#

Here is a situation.
I am sending an email that has image as html body
<img src="http://www.google.com/images/srpr/logo11w.png" alt="click me" />
After receiving email image src is changed to
<img src="https://bay179.mail.live.com/Handlers/ImageProxy.mvc?bicild=&canary=ei12UmVJE9u9hgMk5TdV12Y1X%2b9Vc365IL%2bmULwd%2bfk%3d0&url=http%3a%2f%2fwww.google.com%2fimages%2fsrpr%2flogo11w.pngf" alt="click me">
Hence does not render image. Following is the code used to send email
SmtpClient sc = new SmtpClient("smtp.live.com");
sc.Port = 587;
sc.UseDefaultCredentials = false;
sc.Credentials = new NetworkCredential("someemail#hotmail.com", "password");
sc.EnableSsl = true;
MailMessage m = new MailMessage();
m.From = new MailAddress("someemail#hotmail.com");
m.Subject = "test subject";
m.IsBodyHtml = true;
m.Body = "<img src=\"http://www.google.com/images/srpr/logo11w.png\" alt=\"click me\" /><img src=\"https://campaign-uploads.s3.amazonaws.com/newsletter/2015/19Apr_wk17/EN/images/logo.jpg\" />";
m.To.Add(new MailAddress("someemail#hotmail.com"));
sc.Send(m);
First image url gets changed and second image's url remains same. So whats the science?
I don't want to embed image.
Once you send the e-mail you have yielded control to the receiving system. Most MTAs are going to do some kind of reformatting either due to anti-virus, phishing, etc. scans. Many will rework the img source to use their own proxies. You have no way to stop them. You just get to work with them.

How to place a self created bitmap in the html body of a mail

to specify the question:
I'm creating a bitmap object, which I want to send with an email. I don't want to save it before or upload it to a webserver. Just attach it and then link the attachement in the html body of the mail.
I've searched quite a time now and all I can find are answers in which the picture is stored in the file system or on a server.
So is there any way to do this whithout saving the image before?
Thanks
Edit:
I've tried around a bit and finally came to this solution:
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("xxx#yyy.de"));
mail.From = new MailAddress("xxx#yyy.de");
SmtpClient sender = new SmtpClient
{
Host = "smtp.client",
Port = 25
};
mail.Subject = "test";
body= "blablabla<br><img alt=\"\" hspace=0 src=\"cid:ImagedId\" align=baseline border=0 ><br>blablabla";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
using (System.IO.MemoryStream image = new System.IO.MemoryStream())
{
Bitmap diagram = new Bitmap("C:\\qwer.bmp");
diagram.Save(image, System.Drawing.Imaging.ImageFormat.Jpeg);
LinkedResource resource = new LinkedResource(image, "image/jpeg");
resource.ContentId = "ImageId";
resource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(resource);
mail.AlternateViews.Add(htmlView);
sender.Send(mail);
}
But now my MailClient (Lotus Notes) doesnt open the mail with the error: "no mime data".
Any Idea how to solve this?
Try creating it in Word (either just the image or whole email) then drag over it and copy and paste into Outlook. I think that auto attaches images as well as embeds them.

Image not visible in Email, after email is sent using .Net.Mail Class

I need to send a HTML content as Email, If the HTML has any image..it dosent show uo in the Email.
I cannot attach the image to the mail
The Image will be inside HTML, which i get from a CMS
How can i solve this issue ?
Are you adding image as linked resource? Here is an example how to do this: http://www.codeproject.com/Articles/34467/Sending-Email-Using-Embedded-Images
using System.Net.Mail;
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
pic1.ContentId = "Pic1";
avHtml.LinkedResources.Add(pic1);
// Add the alternate views instead of using MailMessage.Body
MailMessage m = new MailMessage();
m.AlternateViews.Add(avHtml);
// Address and send the message
m.From = new MailAddress("rizwan#dotnetplayer.com", "Rizwan Qureshi");
m.To.Add(new MailAddress("shayan#dotnetplayer.com", "Shayan Qureshi"));
m.Subject = "A picture using alternate views";
SmtpClient client = new SmtpClient("smtp.dotnetplayer.com");
client.Send(m);
In all HTML e-mails that cannot have the images attached, all images must be placed in a server and be referenced in the HTML with their full path e.g. "http://www.yourdomain.com/images/imagename.ext". Casing IS important, and it is the most common cause for images not appearing.

how to send an email with an attachment from C#?

I'm trying to write a code, what would save the content of a picturebox ( works ) and email it ( doesn't work ).
What do you think might be the problem? Should there be anything more to the SmtpClient client = new SmtpClient("smtp.gmail.com"); ?
Also the program shouldn't freeze up while the image gets uploaded, rather then, if necessary, be able to simultaneously upload a few images.
System.Drawing.Image img = pictureBox1.Image;
string name = "" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".jpg";
img.Save(name, System.Drawing.Imaging.ImageFormat.Jpeg);
if (chb_notif.Checked == true) ////////////// SEND EMAIL!
{
MailMessage message = new MailMessage(
"do-not-reply#123.com",
tb_email.Text ,
"VIDEO FENCE",
"Your perimeter has been breeched! System name: " + Environment.MachineName + "." );
Attachment data = new Attachment(name);
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(name);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(name);
disposition.ReadDate = System.IO.File.GetLastAccessTime(name);
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}
Thanks!
for:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required"
Try use:
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl =true
};
client.Send(message);
If you dont want your app to hang while this may take some time (if the image is big, or the servers are unresponsive, you need to put it into a separate thread. (Many examples already exist)
As a few of us have also pointed out, you need to also send the Email, your code above doesnt do that. Be aware of course that if gmail thinks you're trying to relay through them, the mail probably wont send.

Categories

Resources