Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. Dim EM As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text)
  2. Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text)
  3. Dim RGen As Random = New Random()
  4. A.ContentId = RGen.Next(100000, 9999999).ToString()
  5. EM.Attachments.Add(A)
  6. EM.Subject = txtSubject.Text
  7. EM.Body = "<body>" + txtBody.Text + "<br><img src='cid:" + A.ContentId +"'></body>"
  8. EM.IsBodyHtml = True
  9. Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text)
  10. SC.Send(EM)
  11.  
  12. string html = @"<html><body><img src=""cid:YourPictureId""></body></html>";
  13. AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
  14.  
  15. LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
  16. yourPictureRes.ContentId = "YourPictureId";
  17. altView.LinkedResources.Add(yourPictureRes);
  18.  
  19. MailMessage mail = new MailMessage();
  20. mail.AlternateViews.Add(altView);
  21.  
  22. <div>
  23. <asp:LinkButton ID="emailTestLnkBtn" runat="server" OnClick="sendHTMLEmail">testemail</asp:LinkButton>
  24. </div>
  25.  
  26. protected void sendHTMLEmail(object s, EventArgs e)
  27. {
  28. /* adapted from http://stackoverflow.com/questions/1113345/sending-mail-along-with-embedded-image-using-asp-net
  29. and http://stackoverflow.com/questions/886728/generating-html-email-body-in-c-sharp */
  30.  
  31. string myTestReceivingEmail = "yourEmail@address.com"; // your Email address for testing or the person who you are sending the text to.
  32. string subject = "This is the subject line";
  33. string firstName = "John";
  34. string mobileNo = "07711 111111";
  35.  
  36. // Create the message.
  37. var from = new MailAddress("emailFrom@address.co.uk", "displayed from Name");
  38. var to = new MailAddress(myTestReceivingEmail, "person emailing to's displayed Name");
  39. var mail = new MailMessage(from, to);
  40. mail.Subject = subject;
  41.  
  42. // Perform replacements on the HTML file (which you're using as a template).
  43. var reader = new StreamReader(@"c:TempHTMLfile.htm");
  44. string body = reader.ReadToEnd().Replace("%TEMPLATE_TOKEN1%", firstName).Replace("%TEMPLATE_TOKEN2%", mobileNo); // and so on as needed...
  45.  
  46. // replaced this line with imported reader so can use a templete ....
  47. //string html = body; //"<html><body>Text here <br/>- picture here <br /><br /><img src=""cid:SACP_logo_sml.jpg""></body></html>";
  48.  
  49. // Create an alternate view and add it to the email. Can implement an if statement to decide which view to add //
  50. AlternateView altView = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
  51.  
  52. // Logo 1 //
  53. string imageSource = (Server.MapPath("") + "\logo_sml.jpg");
  54. LinkedResource PictureRes = new LinkedResource(imageSource, MediaTypeNames.Image.Jpeg);
  55. PictureRes.ContentId = "logo_sml.jpg";
  56. altView.LinkedResources.Add(PictureRes);
  57.  
  58. // Logo 2 //
  59. string imageSource2 = (Server.MapPath("") + "\booking_btn.jpg");
  60. LinkedResource PictureRes2 = new LinkedResource(imageSource2, MediaTypeNames.Image.Jpeg);
  61. PictureRes2.ContentId = "booking_btn.jpg";
  62. altView.LinkedResources.Add(PictureRes2);
  63.  
  64. mail.AlternateViews.Add(altView);
  65.  
  66. // Send the email (using Web.Config file to store email Network link, etc.)
  67. SmtpClient mySmtpClient = new SmtpClient();
  68. mySmtpClient.Send(mail);
  69. }
  70.  
  71. <html>
  72. <body>
  73. <img src="cid:logo_sml.jpg">
  74. <br />
  75. Hi %TEMPLATE_TOKEN1% .
  76. <br />
  77. <br/>
  78. Your mobile no is %TEMPLATE_TOKEN2%
  79. <br />
  80. <br />
  81. <img src="cid:booking_btn.jpg">
  82. </body>
  83. </html>
  84.  
  85. <system.net>
  86. <mailSettings>
  87. <smtp deliveryMethod="SpecifiedPickupDirectory" from="madeupEmail@address.com">
  88. <specifiedPickupDirectory pickupDirectoryLocation="C:TempMail"/>
  89. </smtp>
  90. </mailSettings>
  91. </system.net>
  92.  
  93. using System.Net.Mail;
  94. using System.Text;
  95. using System.Reflection;
  96. using System.Net.Mime; // need for mail message and text encoding
  97. using System.IO;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement