Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LinkedResource inline = new LinkedResource(filePath);
- inline.ContentId = Guid.NewGuid().ToString();
- MailMessage mail = new MailMessage();
- Attachment att = new Attachment(filePath);
- att.ContentDisposition.Inline = true;
- mail.From = from_email;
- mail.To.Add(data.email);
- mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
- mail.Body = String.Format(
- "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
- @"<img src=""cid:{0}"" />", inline.ContentId);
- mail.IsBodyHtml = true;
- mail.Attachments.Add(att);
- string htmlBody = "<html><body><h1>Picture</h1><br><img src="cid:filename"></body></html>";
- AlternateView avHtml = AlternateView.CreateAlternateViewFromString
- (htmlBody, null, MediaTypeNames.Text.Html);
- LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
- inline.ContentId = Guid.NewGuid().ToString();
- avHtml.LinkedResources.Add(inline);
- MailMessage mail = new MailMessage();
- mail.AlternateViews.Add(avHtml);
- Attachment att = new Attachment(filePath);
- att.ContentDisposition.Inline = true;
- mail.From = from_email;
- mail.To.Add(data.email);
- mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
- mail.Body = String.Format(
- "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
- @"<img src=""cid:{0}"" />", inline.ContentId);
- mail.IsBodyHtml = true;
- mail.Attachments.Add(att);
- AlternateView alternateView = AlternateView.CreateAlternateViewFromString("<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
- @"<img src=""cid:{0}"" />", null, "text/html");
- alternateView.LinkedResources.Add(inline);
- mail.AlternateViews.Add(alternateView);
- MailMessage mail = new MailMessage();
- //set the addresses
- mail.From = new MailAddress("userid@gmail.com");
- mail.To.Add("userid@gmail.com");
- //set the content
- mail.Subject = "Sucess Fully Sent the HTML and COntent of mail";
- //first we create the Plain Text part
- string plainText = "Non HTML Plane Text Message for Non HTML enable mode";
- AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainText, null, "text/plain");
- XmlTextReader reader = new XmlTextReader(@"E:HTMLPage.htm");
- string[] address = new string[30];
- string finalHtml = "";
- var i = -1;
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element)
- { // The node is an element.
- if (reader.AttributeCount <= 1)
- {
- if (reader.Name == "img")
- {
- finalHtml += "<" + reader.Name;
- while (reader.MoveToNextAttribute())
- {
- if (reader.Name == "src")
- {
- i++;
- address[i] = reader.Value;
- address[i] = address[i].Remove(0, 8);
- finalHtml += " " + reader.Name + "=" + "cid:chartlogo" + i.ToString();
- }
- else
- {
- finalHtml += " " + reader.Name + "='" + reader.Value + "'";
- }
- }
- finalHtml += ">";
- }
- else
- {
- finalHtml += "<" + reader.Name;
- while (reader.MoveToNextAttribute())
- {
- finalHtml += " " + reader.Name + "='" + reader.Value + "'";
- }
- finalHtml += ">";
- }
- }
- }
- else if (reader.NodeType == XmlNodeType.Text)
- { //Display the text in each element.
- finalHtml += reader.Value;
- }
- else if (reader.NodeType == XmlNodeType.EndElement)
- {
- //Display the end of the element.
- finalHtml += "</" + reader.Name;
- finalHtml += ">";
- }
- }
- AlternateView htmlView = AlternateView.CreateAlternateViewFromString(finalHtml, null, "text/html");
- LinkedResource[] logo = new LinkedResource[i + 1];
- for (int j = 0; j <= i; j++)
- {
- logo[j] = new LinkedResource(address[j]);
- logo[j].ContentId = "chartlogo" + j;
- htmlView.LinkedResources.Add(logo[j]);
- }
- mail.AlternateViews.Add(plainView);
- mail.AlternateViews.Add(htmlView);
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.Port = 587;
- smtp.Credentials = new NetworkCredential(
- "userid@gmail.com", "Password");
- smtp.EnableSsl = true;
- Console.WriteLine();
- smtp.Send(mail);
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- string Themessage = @"<html>
- <body>
- <table width=""100%"">
- <tr>
- <td style=""font-style:arial; color:maroon; font-weight:bold"">
- Hi! <br>
- <img src=cid:myImageID>
- </td>
- </tr>
- </table>
- </body>
- </html>";
- sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
- }
- protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
- {
- //create an instance of new mail message
- MailMessage mail = new MailMessage();
- //set the HTML format to true
- mail.IsBodyHtml = true;
- //create Alrternative HTML view
- AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
- //Add Image
- LinkedResource theEmailImage = new LinkedResource("E:\IMG_3332.jpg");
- theEmailImage.ContentId = "myImageID";
- //Add the Image to the Alternate view
- htmlView.LinkedResources.Add(theEmailImage);
- //Add view to the Email Message
- mail.AlternateViews.Add(htmlView);
- //set the "from email" address and specify a friendly 'from' name
- mail.From = new MailAddress(from_Email, from_Name);
- //set the "to" email address
- mail.To.Add(to_Email);
- //set the Email subject
- mail.Subject = Subject;
- //set the SMTP info
- System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
- SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
- smtp.EnableSsl = true;
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = cred;
- //send the email
- smtp.Send(mail);
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- string Themessage = @"<html>
- <body>
- <table width=""100%"">
- <tr>
- <td style=""font-style:arial; color:maroon; font-weight:bold"">
- Hi! <br>
- <img src=cid:myImageID>
- </td>
- </tr>
- </table>
- </body>
- </html>";
- sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
- }
- protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
- {
- //create an instance of new mail message
- MailMessage mail = new MailMessage();
- //set the HTML format to true
- mail.IsBodyHtml = true;
- //create Alrternative HTML view
- AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
- //Add Image
- LinkedResource theEmailImage = new LinkedResource("E:\IMG_3332.jpg");
- theEmailImage.ContentId = "myImageID";
- //Add the Image to the Alternate view
- htmlView.LinkedResources.Add(theEmailImage);
- //Add view to the Email Message
- mail.AlternateViews.Add(htmlView);
- //set the "from email" address and specify a friendly 'from' name
- mail.From = new MailAddress(from_Email, from_Name);
- //set the "to" email address
- mail.To.Add(to_Email);
- //set the Email subject
- mail.Subject = Subject;
- //set the SMTP info
- System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
- SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
- smtp.EnableSsl = true;
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = cred;
- //send the email
- smtp.Send(mail);
- }
- private void sendInlineImg() {
- MailMessage mail = new MailMessage();
- mail.IsBodyHtml = true;
- mail.AlternateViews.Add(getEmbeddeImage());
- mail.From = new MailAddress("yourAddress@yourDomain");
- mail.To.Add("recipient@hisDomain");
- mail.Subject = "yourSubject";
- //YourSMTPClient.Send(mail); //* Set your SMTPClient before!
- }
- private AlternateView getEmbeddeImage() {
- string yourFile = "c:/header.png";
- LinkedResource inline = new LinkedResource(yourFile);
- inline.ContentId = Guid.NewGuid().ToString();
- string htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>";
- AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
- alternateView.LinkedResources.Add(inline);
- return alternateView;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement