I am making a windows form to capture image from laptop and send it to phone through email or whatsapp or any other social networking site. Though I got the capturing image code, I couldn't find the code for sending the image. Please help.
You can send email with image with this code:
using (var client = new SmtpClient())
{
MailMessage newMail = new MailMessage();
newMail.To.Add(new MailAddress("you#your.address"));
newMail.Subject = "Test Subject";
newMail.IsBodyHtml = true;
var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
inlineLogo.ContentId = Guid.NewGuid().ToString();
string body = string.Format(#"
<img src=""cid:{0}"" />
", inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);
client.Send(newMail);
}
https://stackoverflow.com/a/11000938/5364144
Related
In an ASP/C# application I'm sending an email with 3 file attached. The 3 files are the same type, same extension and more or less same size ( but none are empty ).
The email is sent correctly. If I open it using outlook, I have no problems. I can see the body, and the 3 files attached.
But here is my issue: If I send that mail to a Gmail Adress, then on Gmail I can see only 2 attachments.
And if I click on the download all attachment icon ( on the right ), it will download the 2 visible attachment + the third one but empty.
It's a really weird issue.
Also there is a 4th attachment which is an embedded image. And this image is display correctly in the mail body.
Here is the code I'm using to send the mail:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("SMTP_IP_ADRESS", SMTP_IP_PORT);
mail.From = new MailAddress("MYEMAIL#DOMAIN.COM");
mail.To.Add("GMAIL_EMAIL");
mail.To.Add("OUTLOOK_EMAIL");
mail.Subject = "MSN "+Request.Params["nameMsn"];
Attachment imageAttachment = new
Attachment(Server.MapPath(Url.Content("~/Content/images/image.png")));
string contentID = "logoCare";
imageAttachment.ContentId = contentID;
mail.IsBodyHtml = true;
mail.Body = "<html><body>Have a good day.<br/>Best regards. <br/><br/><img width='200'
src=\"cid:"
+ contentID + "\"></body></html>";
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
var attachment = new Attachment(file.InputStream, file.FileName,
MediaTypeNames.Application.Octet);
mail.Attachments.Add(attachment);
}
mail.Attachments.Add(imageAttachment);
SmtpServer.Send(mail);
The third attachment you see empty can possibly be the CID embedded image that web based email clients (like Gmail) can't manage, meanwhile it works with desktop email clients like Outlook. Can you verify this?
Please take a look at here
I was unable to send email images, with Sendgird, to gmail using an inline/embedded method.
Here is my SO thread covering this. So I tried adding a cid attachment, but I'm not sure if I'm adding it right because the image isn't being shown in the email but being sent as an attachment at the bottom of the email.
So how do I attach the image as a cid?
Here is my code.
In the email I have this
<img alt="SpaceImage" title="Space Image" style="display: block" width="225" height="126" src="cid:spacethumbnail" />
Then in my c# I have this.
Attachment attachment = new Attachment();
attachment.ContentId = "spacethumbnail";
attachment.Content = Convert.ToBase64String(newEvent.SpaceThumbnail);
attachment.Type = "image/jpg";
attachment.Disposition = "inline"; // fixed the issue
attachment.Filename = "space-thumbnail.jpg";
and then I add this attachment to my send grid email like this
Mail mail = new Mail();
mail.Subject = message.Subject;
mail.From = new Email("Yoga#yoga.com");
mail.AddContent(new Content("text/HTML", message.Body));
foreach(Attachment attachment in attachments)
{
mail.AddAttachment(attachment);
}
// add multiple recipients if needed
Personalization personalization = new Personalization();
foreach (string emailAddress in message.Destination.Split(','))
{
personalization.AddTo(new Email(emailAddress));
}
mail.AddPersonalization(personalization);
dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
How do I add the image as a cid attachment and not a regular attachment?
Since I don't like it when answers are in comments, for better visibility, I'm posting the answer from #user1186050 that seems to have solved the question.
In the C# file, adding: attachment.Disposition = "inline"; fixed the issue.
I have a C# app that generates emails using an HTML format (AlternateView) that contains an embedded image (jpeg).
The emails can be viewed correctly on Android, iOS, Outlook (2010) on Windows, but not on the native email client on Windows 8/8.1 Phone and Tablet (although it works find in Outlook running on the Win 8.1 tablet).
However the offending email client on the Win 8 phones/tablets can correctly see emails with embedded images sent from Outlook running on a PC.
The only thing I could find was to make sure the ContentType is set on the LinkedResource -which I do in the constructor:
LinkedResource pic1 = new LinkedResource(imgPath, MediaTypeNames.Image.Jpeg);
Code below.
Any suggestions?
string picId = "Pic1-" + Guid.NewGuid().ToString();
string htmlBody = "<html><body><h1>Test Message</h1><img src=\"cid:" + picId + "\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource(imgPath, MediaTypeNames.Image.Jpeg);
pic1.ContentId = picId;
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(fromAddress, fromName);
m.To.Add(new MailAddress(toAddress));
m.Subject = subject;
SmtpClient client = new SmtpClient(AsbSmtpServer);
client.Send(m);
I have a database which contains email address of recipients and a flag column. If the flag is "YES", a mail will be sent to that user. I want to accomplish this using CDO in my ASP.NET project. So, what may be the favorable routine for doing this. I am new to CDO.
What I tried as a beginner is this:
CDO.Message oMsg = new CDO.Message();
CDO.IConfiguration iConfg;
iConfg = oMsg.Configuration;
ADODB.Fields oFields;
oFields = iConfg.Fields;
ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
oFields.Update();
oMsg.Subject = "Test CDO";
oMsg.From = "abc#xyz.com";
oMsg.To = "someone#example.com";
//oMsg.TextBody = "CDO Mail test";
oMsg.HTMLBody = "CDO Mail test";
oMsg.Send();
Response.Write("Mail Sent");
But its not helping me what I want.
I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008.
Please provide SMTP HostName, PortName for yahoo server and gmail server.
And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.
Please provide complete working code...so that i will copy into Visual studio environment and execute the same.
As i'm getting exception since morning....unable to resolve the issue.
Kindly help me in this regard.
For Gmail:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#gmail.com");
mail.To.Add("youraccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
For Yahoo:
var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("youraccount#yahoo.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#yahoo.com");
mail.To.Add("destaccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.
So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).
Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.
Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.
If you don't know how to enable logging for SNM, here is a link:
http://systemnetmail.com/faq/4.10.aspx
Thanks!
Dave
There are two ways in which We can send the mail,
1)First is using javascript link "mailTo".This will not send the mail automatically but it will just open the mail window.Refer to the below code
<a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a>
Find below the js method
function buildEmail(el) {
var emailId = Usermail#gmail.com;
var subject="Hi";
var body="Hello";
el.href = "mailto:" + emailId + "?Subject=" + escape(subject) +
"&Body=" + escape(body);
}
2)The second way is to use System.Net.Mail which will automatically send the mail to the recipients in the secured manner.
string subject="Hello";
string body="Data";
using ( MailMessage objMail = new MailMessage ( "Yourmail#gmail.com", "Usermail#gmail.com" ) )//From and To address respectively
{
objMail.IsBodyHtml = false;// Message format is plain text
objMail.Priority = MailPriority.High;// Mail Priority = High
objMail.Body = "Hello";
ArrayList CCarr = new ArrayList();//Assume we add recipients here
// populate additional recipients if specified
if ( ( CCarr != null ) && ( CCarr .Count > 0 ) )
{
foreach ( string recipient in CCarr )
{
if ( recipient != "Please update the email address" )
{
objMail.CC.Add ( new MailAddress ( recipient ) );
}
}
}
// Set the subject of the message - and make sure it is CIS Compliant
if ( !subject.StartsWith ( "SigabaSecure:" ) )
{
subject = "SigabaSecure: " + subject;
}
objMail.Subject = subject;
// setup credentials for the smpthost
string username = "Username";
string passwd = "xxxxxx";
string smtpHost = "mail.bankofamerica.com";
SmtpClient ss = new SmtpClient ();
ss.EnableSsl= true;
ss.Host = smtpHost;
ss.Credentials = new NetworkCredential ( username, passwd );
ss.Send ( objMail );
}
Sigaba Secure Email secures e-mail from client to client through the use of desktop plug-ins and Web-based authentication and decryption.