Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. LinkedResource inline = new LinkedResource(filePath);
  2. inline.ContentId = Guid.NewGuid().ToString();
  3. MailMessage mail = new MailMessage();
  4. Attachment att = new Attachment(filePath);
  5. att.ContentDisposition.Inline = true;
  6. mail.From = from_email;
  7. mail.To.Add(data.email);
  8. mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
  9. mail.Body = String.Format(
  10. "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
  11. @"<img src=""cid:{0}"" />", inline.ContentId);
  12.  
  13. mail.IsBodyHtml = true;
  14. mail.Attachments.Add(att);
  15.  
  16. string htmlBody = "<html><body><h1>Picture</h1><br><img src="cid:filename"></body></html>";
  17. AlternateView avHtml = AlternateView.CreateAlternateViewFromString
  18. (htmlBody, null, MediaTypeNames.Text.Html);
  19.  
  20. LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
  21. inline.ContentId = Guid.NewGuid().ToString();
  22. avHtml.LinkedResources.Add(inline);
  23.  
  24. MailMessage mail = new MailMessage();
  25. mail.AlternateViews.Add(avHtml);
  26.  
  27. Attachment att = new Attachment(filePath);
  28. att.ContentDisposition.Inline = true;
  29.  
  30. mail.From = from_email;
  31. mail.To.Add(data.email);
  32. mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
  33. mail.Body = String.Format(
  34. "<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
  35. @"<img src=""cid:{0}"" />", inline.ContentId);
  36.  
  37. mail.IsBodyHtml = true;
  38. mail.Attachments.Add(att);
  39.  
  40. AlternateView alternateView = AlternateView.CreateAlternateViewFromString("<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
  41. @"<img src=""cid:{0}"" />", null, "text/html");
  42. alternateView.LinkedResources.Add(inline);
  43. mail.AlternateViews.Add(alternateView);
  44.  
  45. MailMessage mail = new MailMessage();
  46. //set the addresses
  47. mail.From = new MailAddress("userid@gmail.com");
  48. mail.To.Add("userid@gmail.com");
  49.  
  50. //set the content
  51. mail.Subject = "Sucess Fully Sent the HTML and COntent of mail";
  52.  
  53. //first we create the Plain Text part
  54. string plainText = "Non HTML Plane Text Message for Non HTML enable mode";
  55. AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainText, null, "text/plain");
  56. XmlTextReader reader = new XmlTextReader(@"E:HTMLPage.htm");
  57. string[] address = new string[30];
  58. string finalHtml = "";
  59. var i = -1;
  60. while (reader.Read())
  61. {
  62. if (reader.NodeType == XmlNodeType.Element)
  63. { // The node is an element.
  64. if (reader.AttributeCount <= 1)
  65. {
  66. if (reader.Name == "img")
  67. {
  68. finalHtml += "<" + reader.Name;
  69. while (reader.MoveToNextAttribute())
  70. {
  71. if (reader.Name == "src")
  72. {
  73. i++;
  74. address[i] = reader.Value;
  75. address[i] = address[i].Remove(0, 8);
  76. finalHtml += " " + reader.Name + "=" + "cid:chartlogo" + i.ToString();
  77. }
  78. else
  79. {
  80. finalHtml += " " + reader.Name + "='" + reader.Value + "'";
  81. }
  82. }
  83. finalHtml += ">";
  84. }
  85. else
  86. {
  87. finalHtml += "<" + reader.Name;
  88. while (reader.MoveToNextAttribute())
  89. {
  90. finalHtml += " " + reader.Name + "='" + reader.Value + "'";
  91. }
  92. finalHtml += ">";
  93. }
  94. }
  95.  
  96. }
  97. else if (reader.NodeType == XmlNodeType.Text)
  98. { //Display the text in each element.
  99. finalHtml += reader.Value;
  100. }
  101. else if (reader.NodeType == XmlNodeType.EndElement)
  102. {
  103. //Display the end of the element.
  104. finalHtml += "</" + reader.Name;
  105. finalHtml += ">";
  106. }
  107.  
  108. }
  109.  
  110. AlternateView htmlView = AlternateView.CreateAlternateViewFromString(finalHtml, null, "text/html");
  111. LinkedResource[] logo = new LinkedResource[i + 1];
  112. for (int j = 0; j <= i; j++)
  113. {
  114. logo[j] = new LinkedResource(address[j]);
  115. logo[j].ContentId = "chartlogo" + j;
  116. htmlView.LinkedResources.Add(logo[j]);
  117. }
  118. mail.AlternateViews.Add(plainView);
  119. mail.AlternateViews.Add(htmlView);
  120. SmtpClient smtp = new SmtpClient();
  121. smtp.Host = "smtp.gmail.com";
  122. smtp.Port = 587;
  123. smtp.Credentials = new NetworkCredential(
  124. "userid@gmail.com", "Password");
  125. smtp.EnableSsl = true;
  126. Console.WriteLine();
  127. smtp.Send(mail);
  128. }
  129.  
  130. protected void Page_Load(object sender, EventArgs e)
  131. {
  132. string Themessage = @"<html>
  133. <body>
  134. <table width=""100%"">
  135. <tr>
  136. <td style=""font-style:arial; color:maroon; font-weight:bold"">
  137. Hi! <br>
  138. <img src=cid:myImageID>
  139. </td>
  140. </tr>
  141. </table>
  142. </body>
  143. </html>";
  144. sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
  145. }
  146.  
  147. protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
  148. {
  149. //create an instance of new mail message
  150. MailMessage mail = new MailMessage();
  151.  
  152. //set the HTML format to true
  153. mail.IsBodyHtml = true;
  154.  
  155. //create Alrternative HTML view
  156. AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
  157.  
  158. //Add Image
  159. LinkedResource theEmailImage = new LinkedResource("E:\IMG_3332.jpg");
  160. theEmailImage.ContentId = "myImageID";
  161.  
  162. //Add the Image to the Alternate view
  163. htmlView.LinkedResources.Add(theEmailImage);
  164.  
  165. //Add view to the Email Message
  166. mail.AlternateViews.Add(htmlView);
  167.  
  168. //set the "from email" address and specify a friendly 'from' name
  169. mail.From = new MailAddress(from_Email, from_Name);
  170.  
  171. //set the "to" email address
  172. mail.To.Add(to_Email);
  173.  
  174. //set the Email subject
  175. mail.Subject = Subject;
  176.  
  177. //set the SMTP info
  178. System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
  179. SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
  180. smtp.EnableSsl = true;
  181. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  182. smtp.UseDefaultCredentials = false;
  183. smtp.Credentials = cred;
  184. //send the email
  185. smtp.Send(mail);
  186. }
  187.  
  188. protected void Page_Load(object sender, EventArgs e)
  189. {
  190. string Themessage = @"<html>
  191. <body>
  192. <table width=""100%"">
  193. <tr>
  194. <td style=""font-style:arial; color:maroon; font-weight:bold"">
  195. Hi! <br>
  196. <img src=cid:myImageID>
  197. </td>
  198. </tr>
  199. </table>
  200. </body>
  201. </html>";
  202. sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
  203. }
  204.  
  205. protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
  206. {
  207. //create an instance of new mail message
  208. MailMessage mail = new MailMessage();
  209.  
  210. //set the HTML format to true
  211. mail.IsBodyHtml = true;
  212.  
  213. //create Alrternative HTML view
  214. AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
  215.  
  216. //Add Image
  217. LinkedResource theEmailImage = new LinkedResource("E:\IMG_3332.jpg");
  218. theEmailImage.ContentId = "myImageID";
  219.  
  220. //Add the Image to the Alternate view
  221. htmlView.LinkedResources.Add(theEmailImage);
  222.  
  223. //Add view to the Email Message
  224. mail.AlternateViews.Add(htmlView);
  225.  
  226. //set the "from email" address and specify a friendly 'from' name
  227. mail.From = new MailAddress(from_Email, from_Name);
  228.  
  229. //set the "to" email address
  230. mail.To.Add(to_Email);
  231.  
  232. //set the Email subject
  233. mail.Subject = Subject;
  234.  
  235. //set the SMTP info
  236. System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
  237. SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
  238. smtp.EnableSsl = true;
  239. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  240. smtp.UseDefaultCredentials = false;
  241. smtp.Credentials = cred;
  242. //send the email
  243. smtp.Send(mail);
  244. }
  245.  
  246. private void sendInlineImg() {
  247. MailMessage mail = new MailMessage();
  248. mail.IsBodyHtml = true;
  249. mail.AlternateViews.Add(getEmbeddeImage());
  250. mail.From = new MailAddress("yourAddress@yourDomain");
  251. mail.To.Add("recipient@hisDomain");
  252. mail.Subject = "yourSubject";
  253. //YourSMTPClient.Send(mail); //* Set your SMTPClient before!
  254. }
  255. private AlternateView getEmbeddeImage() {
  256. string yourFile = "c:/header.png";
  257. LinkedResource inline = new LinkedResource(yourFile);
  258. inline.ContentId = Guid.NewGuid().ToString();
  259. string htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>";
  260. AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
  261. alternateView.LinkedResources.Add(inline);
  262. return alternateView;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement